Commit
·
cb8b12f
1
Parent(s):
21c1379
Upload model
Browse files- config.json +159 -0
- configuration.py +9 -0
- model.py +88 -2
- pytorch_model.bin +2 -2
config.json
CHANGED
|
@@ -2,13 +2,172 @@
|
|
| 2 |
"architectures": [
|
| 3 |
"CybersecurityKnowledgeGraphModel"
|
| 4 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"auto_map": {
|
| 6 |
"AutoConfig": "configuration.CybersecurityKnowledgeGraphConfig",
|
| 7 |
"AutoModelForTokenClassification": "model.CybersecurityKnowledgeGraphModel"
|
| 8 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"event_argument_model_path": "cybersecurity_knowledge_graph/argument_model_state_dict.pth",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
"event_nugget_model_path": "cybersecurity_knowledge_graph/nugget_model_state_dict.pth",
|
| 11 |
"event_realis_model_path": "cybersecurity_knowledge_graph/realis_model_state_dict.pth",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
"torch_dtype": "float32",
|
| 13 |
"transformers_version": "4.33.2"
|
| 14 |
}
|
|
|
|
| 2 |
"architectures": [
|
| 3 |
"CybersecurityKnowledgeGraphModel"
|
| 4 |
],
|
| 5 |
+
"arg_2_role": {
|
| 6 |
+
"CVE": [
|
| 7 |
+
"CVE"
|
| 8 |
+
],
|
| 9 |
+
"Capabilities": [
|
| 10 |
+
"Attack-Pattern",
|
| 11 |
+
"Capabilities",
|
| 12 |
+
"Issues-Addressed"
|
| 13 |
+
],
|
| 14 |
+
"Data": [
|
| 15 |
+
"Compromised-Data",
|
| 16 |
+
"Trusted-Entity"
|
| 17 |
+
],
|
| 18 |
+
"Device": [
|
| 19 |
+
"Vulnerable_System",
|
| 20 |
+
"Victim",
|
| 21 |
+
"Supported_Platform"
|
| 22 |
+
],
|
| 23 |
+
"File": [
|
| 24 |
+
"Tool",
|
| 25 |
+
"Trusted-Entity"
|
| 26 |
+
],
|
| 27 |
+
"GPE": [
|
| 28 |
+
"Place"
|
| 29 |
+
],
|
| 30 |
+
"Malware": [
|
| 31 |
+
"Tool"
|
| 32 |
+
],
|
| 33 |
+
"Money": [
|
| 34 |
+
"Price",
|
| 35 |
+
"Damage-Amount"
|
| 36 |
+
],
|
| 37 |
+
"Number": [
|
| 38 |
+
"Number-of-Data",
|
| 39 |
+
"Number-of-Victim"
|
| 40 |
+
],
|
| 41 |
+
"Organization": [
|
| 42 |
+
"Victim",
|
| 43 |
+
"Releaser",
|
| 44 |
+
"Discoverer",
|
| 45 |
+
"Attacker",
|
| 46 |
+
"Vulnerable_System_Owner",
|
| 47 |
+
"Trusted-Entity"
|
| 48 |
+
],
|
| 49 |
+
"PII": [
|
| 50 |
+
"Compromised-Data",
|
| 51 |
+
"Trusted-Entity"
|
| 52 |
+
],
|
| 53 |
+
"Patch": [
|
| 54 |
+
"Patch"
|
| 55 |
+
],
|
| 56 |
+
"PaymentMethod": [
|
| 57 |
+
"Payment-Method"
|
| 58 |
+
],
|
| 59 |
+
"Person": [
|
| 60 |
+
"Victim",
|
| 61 |
+
"Attacker",
|
| 62 |
+
"Discoverer",
|
| 63 |
+
"Releaser",
|
| 64 |
+
"Trusted-Entity",
|
| 65 |
+
"Vulnerable_System_Owner"
|
| 66 |
+
],
|
| 67 |
+
"Purpose": [
|
| 68 |
+
"Purpose"
|
| 69 |
+
],
|
| 70 |
+
"Software": [
|
| 71 |
+
"Vulnerable_System",
|
| 72 |
+
"Victim",
|
| 73 |
+
"Trusted-Entity",
|
| 74 |
+
"Supported_Platform"
|
| 75 |
+
],
|
| 76 |
+
"System": [
|
| 77 |
+
"Victim",
|
| 78 |
+
"Supported_Platform",
|
| 79 |
+
"Vulnerable_System",
|
| 80 |
+
"Trusted-Entity"
|
| 81 |
+
],
|
| 82 |
+
"Time": [
|
| 83 |
+
"Time"
|
| 84 |
+
],
|
| 85 |
+
"Version": [
|
| 86 |
+
"Patch-Number",
|
| 87 |
+
"Vulnerable_System_Version"
|
| 88 |
+
],
|
| 89 |
+
"Vulnerability": [
|
| 90 |
+
"Vulnerability"
|
| 91 |
+
],
|
| 92 |
+
"Website": [
|
| 93 |
+
"Trusted-Entity",
|
| 94 |
+
"Tool",
|
| 95 |
+
"Vulnerable_System",
|
| 96 |
+
"Victim",
|
| 97 |
+
"Supported_Platform"
|
| 98 |
+
]
|
| 99 |
+
},
|
| 100 |
"auto_map": {
|
| 101 |
"AutoConfig": "configuration.CybersecurityKnowledgeGraphConfig",
|
| 102 |
"AutoModelForTokenClassification": "model.CybersecurityKnowledgeGraphModel"
|
| 103 |
},
|
| 104 |
+
"event_args_list": [
|
| 105 |
+
"O",
|
| 106 |
+
"B-System",
|
| 107 |
+
"I-System",
|
| 108 |
+
"B-Organization",
|
| 109 |
+
"B-Money",
|
| 110 |
+
"I-Money",
|
| 111 |
+
"B-Device",
|
| 112 |
+
"B-Person",
|
| 113 |
+
"I-Person",
|
| 114 |
+
"B-Vulnerability",
|
| 115 |
+
"I-Vulnerability",
|
| 116 |
+
"B-Capabilities",
|
| 117 |
+
"I-Capabilities",
|
| 118 |
+
"I-Organization",
|
| 119 |
+
"B-PaymentMethod",
|
| 120 |
+
"I-PaymentMethod",
|
| 121 |
+
"B-Data",
|
| 122 |
+
"I-Data",
|
| 123 |
+
"B-Number",
|
| 124 |
+
"I-Number",
|
| 125 |
+
"B-Malware",
|
| 126 |
+
"I-Malware",
|
| 127 |
+
"B-PII",
|
| 128 |
+
"I-PII",
|
| 129 |
+
"B-CVE",
|
| 130 |
+
"I-CVE",
|
| 131 |
+
"B-Purpose",
|
| 132 |
+
"I-Purpose",
|
| 133 |
+
"B-File",
|
| 134 |
+
"I-File",
|
| 135 |
+
"I-Device",
|
| 136 |
+
"B-Time",
|
| 137 |
+
"I-Time",
|
| 138 |
+
"B-Software",
|
| 139 |
+
"I-Software",
|
| 140 |
+
"B-Patch",
|
| 141 |
+
"I-Patch",
|
| 142 |
+
"B-Version",
|
| 143 |
+
"I-Version",
|
| 144 |
+
"B-Website",
|
| 145 |
+
"I-Website",
|
| 146 |
+
"B-GPE",
|
| 147 |
+
"I-GPE"
|
| 148 |
+
],
|
| 149 |
"event_argument_model_path": "cybersecurity_knowledge_graph/argument_model_state_dict.pth",
|
| 150 |
+
"event_nugget_list": [
|
| 151 |
+
"O",
|
| 152 |
+
"B-Ransom",
|
| 153 |
+
"I-Ransom",
|
| 154 |
+
"B-DiscoverVulnerability",
|
| 155 |
+
"I-DiscoverVulnerability",
|
| 156 |
+
"B-PatchVulnerability",
|
| 157 |
+
"I-PatchVulnerability",
|
| 158 |
+
"B-Databreach",
|
| 159 |
+
"I-Databreach",
|
| 160 |
+
"B-Phishing",
|
| 161 |
+
"I-Phishing"
|
| 162 |
+
],
|
| 163 |
"event_nugget_model_path": "cybersecurity_knowledge_graph/nugget_model_state_dict.pth",
|
| 164 |
"event_realis_model_path": "cybersecurity_knowledge_graph/realis_model_state_dict.pth",
|
| 165 |
+
"realis_list": [
|
| 166 |
+
"O",
|
| 167 |
+
"Generic",
|
| 168 |
+
"Other",
|
| 169 |
+
"Actual"
|
| 170 |
+
],
|
| 171 |
"torch_dtype": "float32",
|
| 172 |
"transformers_version": "4.33.2"
|
| 173 |
}
|
configuration.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
from transformers import PretrainedConfig
|
| 2 |
import torch
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
class CybersecurityKnowledgeGraphConfig(PretrainedConfig):
|
| 5 |
|
| 6 |
def __init__(
|
|
@@ -13,4 +16,10 @@ class CybersecurityKnowledgeGraphConfig(PretrainedConfig):
|
|
| 13 |
self.event_nugget_model_path = event_nugget_model_path
|
| 14 |
self.event_argument_model_path = event_argument_model_path
|
| 15 |
self.event_realis_model_path = event_realis_model_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
super().__init__(**kwargs)
|
|
|
|
| 1 |
from transformers import PretrainedConfig
|
| 2 |
import torch
|
| 3 |
|
| 4 |
+
from cybersecurity_knowledge_graph.utils import event_args_list, event_nugget_list, realis_list, arg_2_role
|
| 5 |
+
|
| 6 |
+
|
| 7 |
class CybersecurityKnowledgeGraphConfig(PretrainedConfig):
|
| 8 |
|
| 9 |
def __init__(
|
|
|
|
| 16 |
self.event_nugget_model_path = event_nugget_model_path
|
| 17 |
self.event_argument_model_path = event_argument_model_path
|
| 18 |
self.event_realis_model_path = event_realis_model_path
|
| 19 |
+
|
| 20 |
+
self.event_nugget_list = event_nugget_list
|
| 21 |
+
self.event_args_list = event_args_list
|
| 22 |
+
self.realis_list = realis_list
|
| 23 |
+
self.arg_2_role = arg_2_role
|
| 24 |
+
|
| 25 |
super().__init__(**kwargs)
|
model.py
CHANGED
|
@@ -1,5 +1,10 @@
|
|
| 1 |
from transformers import PreTrainedModel
|
| 2 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
from cybersecurity_knowledge_graph.nugget_model_utils import CustomRobertaWithPOS as NuggetModel
|
| 5 |
from cybersecurity_knowledge_graph.args_model_utils import CustomRobertaWithPOS as ArgumentModel
|
|
@@ -16,6 +21,8 @@ class CybersecurityKnowledgeGraphModel(PreTrainedModel):
|
|
| 16 |
|
| 17 |
def __init__(self, config):
|
| 18 |
super().__init__(config)
|
|
|
|
|
|
|
| 19 |
self.event_nugget_model_path = config.event_nugget_model_path
|
| 20 |
self.event_argument_model_path = config.event_argument_model_path
|
| 21 |
self.event_realis_model_path = config.event_realis_model_path
|
|
@@ -32,6 +39,25 @@ class CybersecurityKnowledgeGraphModel(PreTrainedModel):
|
|
| 32 |
self.event_realis_model.load_state_dict(torch.load(self.event_realis_model_path))
|
| 33 |
self.event_argument_model.load_state_dict(torch.load(self.event_argument_model_path))
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def forward(self, text):
|
| 37 |
nugget_dataloader, _ = self.event_nugget_dataloader(text)
|
|
@@ -51,15 +77,75 @@ class CybersecurityKnowledgeGraphModel(PreTrainedModel):
|
|
| 51 |
realis_pred = self.forward_model(self.event_realis_model, realis_dataloader)
|
| 52 |
argument_preds[idx] = argument_pred
|
| 53 |
realis_preds[idx] = realis_pred
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
def forward_model(self, model, dataloader):
|
| 58 |
predicted_label = []
|
| 59 |
for batch in dataloader:
|
| 60 |
with torch.no_grad():
|
| 61 |
logits = model(**batch)
|
| 62 |
-
|
| 63 |
batch_predicted_label = logits.argmax(-1)
|
| 64 |
predicted_label.append(batch_predicted_label)
|
| 65 |
return torch.cat(predicted_label, dim=-1)
|
|
|
|
| 1 |
from transformers import PreTrainedModel
|
| 2 |
import torch
|
| 3 |
+
import joblib, os
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
+
from transformers import AutoTokenizer
|
| 7 |
+
|
| 8 |
|
| 9 |
from cybersecurity_knowledge_graph.nugget_model_utils import CustomRobertaWithPOS as NuggetModel
|
| 10 |
from cybersecurity_knowledge_graph.args_model_utils import CustomRobertaWithPOS as ArgumentModel
|
|
|
|
| 21 |
|
| 22 |
def __init__(self, config):
|
| 23 |
super().__init__(config)
|
| 24 |
+
self.tokenizer = AutoTokenizer.from_pretrained("ehsanaghaei/SecureBERT")
|
| 25 |
+
|
| 26 |
self.event_nugget_model_path = config.event_nugget_model_path
|
| 27 |
self.event_argument_model_path = config.event_argument_model_path
|
| 28 |
self.event_realis_model_path = config.event_realis_model_path
|
|
|
|
| 39 |
self.event_realis_model.load_state_dict(torch.load(self.event_realis_model_path))
|
| 40 |
self.event_argument_model.load_state_dict(torch.load(self.event_argument_model_path))
|
| 41 |
|
| 42 |
+
role_classifiers = {}
|
| 43 |
+
folder_path = '/cybersecurity_knowledge_graph/arg_role_models'
|
| 44 |
+
|
| 45 |
+
for filename in os.listdir(os.getcwd() + folder_path):
|
| 46 |
+
if filename.endswith('.joblib'):
|
| 47 |
+
file_path = os.getcwd() + os.path.join(folder_path, filename)
|
| 48 |
+
clf = joblib.load(file_path)
|
| 49 |
+
arg = filename.split(".")[0]
|
| 50 |
+
role_classifiers[arg] = clf
|
| 51 |
+
|
| 52 |
+
self.role_classifiers = role_classifiers
|
| 53 |
+
self.embed_model = SentenceTransformer('sentence_transformer')
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
self.event_nugget_list = config.event_nugget_list
|
| 57 |
+
self.event_args_list = config.event_args_list
|
| 58 |
+
self.realis_list = config.realis_list
|
| 59 |
+
self.arg_2_role = config.arg_2_role
|
| 60 |
+
|
| 61 |
|
| 62 |
def forward(self, text):
|
| 63 |
nugget_dataloader, _ = self.event_nugget_dataloader(text)
|
|
|
|
| 77 |
realis_pred = self.forward_model(self.event_realis_model, realis_dataloader)
|
| 78 |
argument_preds[idx] = argument_pred
|
| 79 |
realis_preds[idx] = realis_pred
|
| 80 |
+
|
| 81 |
+
attention_mask = [batch["attention_mask"] for batch in nugget_dataloader]
|
| 82 |
+
attention_mask = torch.cat(attention_mask, dim=-1)
|
| 83 |
+
|
| 84 |
+
input_ids = [batch["input_ids"] for batch in nugget_dataloader]
|
| 85 |
+
input_ids = torch.cat(input_ids, dim=-1)
|
| 86 |
+
|
| 87 |
+
output = {"nugget" : nugget_pred, "argument" : argument_preds, "realis" : realis_preds, "input_ids" : input_ids, "attention_mask" : attention_mask}
|
| 88 |
+
no_of_batch = output['input_ids'].shape[0]
|
| 89 |
|
| 90 |
+
structured_output = []
|
| 91 |
+
for b in range(no_of_batch):
|
| 92 |
+
token_mask = [True if self.tokenizer.decode(token) not in self.tokenizer.all_special_tokens else False for token in output['input_ids'][b]]
|
| 93 |
+
filtered_ids = output['input_ids'][b][token_mask]
|
| 94 |
+
filtered_tokens = [self.tokenizer.decode(token) for token in filtered_ids]
|
| 95 |
+
|
| 96 |
+
filtered_nuggets = output['nugget'][b][token_mask]
|
| 97 |
+
filtered_args = output['argument'][b][token_mask]
|
| 98 |
+
filtered_realis = output['realis'][b][token_mask]
|
| 99 |
+
|
| 100 |
+
batch_output = [{"id" : id.item(), "token" : token, "nugget" : self.event_nugget_list[int(nugget.item())], "argument" : self.event_args_list[int(arg.item())], "realis" : self.realis_list[int(realis.item())]}
|
| 101 |
+
for id, token, nugget, arg, realis in zip(filtered_ids, filtered_tokens, filtered_nuggets, filtered_args, filtered_realis)]
|
| 102 |
+
structured_output.extend(batch_output)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
args = [(idx, item["argument"], item["token"]) for idx, item in enumerate(structured_output) if item["argument"]!= "O"]
|
| 106 |
+
|
| 107 |
+
entities = []
|
| 108 |
+
current_entity = None
|
| 109 |
+
for position, label, token in args:
|
| 110 |
+
if label.startswith('B-'):
|
| 111 |
+
if current_entity is not None:
|
| 112 |
+
entities.append(current_entity)
|
| 113 |
+
current_entity = {'label': label[2:], 'text': token.replace(" ", ""), 'start': position, 'end': position}
|
| 114 |
+
elif label.startswith('I-'):
|
| 115 |
+
if current_entity is not None:
|
| 116 |
+
current_entity['text'] += ' ' + token.replace(" ", "")
|
| 117 |
+
current_entity['end'] = position
|
| 118 |
+
|
| 119 |
+
for entity in entities:
|
| 120 |
+
context = self.tokenizer.decode([item["id"] for item in structured_output[max(0, entity["start"] - 15) : min(len(structured_output), entity["end"] + 15)]])
|
| 121 |
+
entity["context"] = context
|
| 122 |
+
|
| 123 |
+
for entity in entities:
|
| 124 |
+
if len(self.arg_2_role[entity["label"]]) > 1:
|
| 125 |
+
sent_embed = self.embed_model.encode(entity["context"])
|
| 126 |
+
arg_embed = self.embed_model.encode(entity["text"])
|
| 127 |
+
embed = np.concatenate((sent_embed, arg_embed))
|
| 128 |
+
|
| 129 |
+
arg_clf = self.role_classifiers[entity["label"]]
|
| 130 |
+
role_id = arg_clf.predict(embed.reshape(1, -1))
|
| 131 |
+
role = self.arg_2_role[entity["label"]][role_id[0]]
|
| 132 |
+
|
| 133 |
+
entity["role"] = role
|
| 134 |
+
else:
|
| 135 |
+
entity["role"] = self.arg_2_role[entity["label"]][0]
|
| 136 |
+
|
| 137 |
+
for item in structured_output:
|
| 138 |
+
item["role"] = "O"
|
| 139 |
+
for entity in entities:
|
| 140 |
+
for i in range(entity["start"], entity["end"] + 1):
|
| 141 |
+
structured_output[i]["role"] = entity["role"]
|
| 142 |
+
return structured_output
|
| 143 |
|
| 144 |
def forward_model(self, model, dataloader):
|
| 145 |
predicted_label = []
|
| 146 |
for batch in dataloader:
|
| 147 |
with torch.no_grad():
|
| 148 |
logits = model(**batch)
|
|
|
|
| 149 |
batch_predicted_label = logits.argmax(-1)
|
| 150 |
predicted_label.append(batch_predicted_label)
|
| 151 |
return torch.cat(predicted_label, dim=-1)
|
pytorch_model.bin
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5b14783587eb16be4dac27e2bc3b5d738ee1772b44cf48d0240edef88aaee6e9
|
| 3 |
+
size 1587052173
|