forked from cudeso/misp2sentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestObject.py
129 lines (116 loc) · 5.24 KB
/
RequestObject.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from distutils.command.config import config
import config
from constants import *
class RequestObject:
"""A class that parses attribute from misp to the format consumable by MS Graph API
to use the class:
request_object = RequestObject(attr) # this reads in the attr and parses it
# then use request.__dict__ to get the parsed dict
"""
def __init__(self, attr):
mapping = ATTR_MAPPING.get(attr['type'])
if mapping is not None:
setattr(self, mapping, attr['value'])
if attr['type'] in MISP_SPECIAL_CASE_TYPES:
self._handle_special_cases(attr)
# self.tags = [tag['name'].strip() for tag in attr.get("Tag", [])]
# Tags on attribute level
self.tags = []
tags_remove = []
for tag in attr.get("Tag", []):
if config.misp_ignore_localtags:
if tag["local"] != 1:
self.tags.append(tag['name'].strip())
for tag in self.tags:
if 'diamond-model:' in tag:
self.diamondModel = tag.split(':')[1]
tags_remove.append(tag)
if 'kill-chain:' in tag:
kill_chain = tag.split(':')[1]
# Fix some Azure quirks
if kill_chain == "Command and Control":
kill_chain = "C2"
elif kill_chain == "Actions on Objectives":
kill_chain = "Actions"
self.killChain = [kill_chain]
tags_remove.append(tag)
if 'sentinel-threattype' in tag: # Override with attribute value
self.threatType = tag.split(':')[1]
tags_remove.append(tag)
for tag in tags_remove:
self.tags.remove(tag)
self.additionalInformation = attr['comment']
def _handle_ip(self, attr, attr_type, graph_v4_name, graph_v6_name):
if attr['type'] == attr_type:
if '.' in attr['value']:
setattr(self, graph_v4_name, attr['value'])
else:
setattr(self, graph_v6_name, attr['value'])
def _aggregated_handle_ip(self, attr):
self._handle_ip(attr, 'ip-dst', 'networkDestinationIPv4', 'networkDestinationIPv6')
self._handle_ip(attr, 'ip-src', 'networkSourceIPv4', 'networkSourceIPv6')
if config.network_ignore_direction:
self._handle_ip(attr, 'ip-dst', 'networkIPv4', 'networkIPv6')
self._handle_ip(attr, 'ip-src', 'networkIPv4', 'networkIPv6')
def _handle_file_hash(self, attr):
if attr['type'] in MISP_HASH_TYPES:
if 'filename|' in attr['type']:
self.fileHashType = attr['type'].split('|')[1]
self.fileName, self.fileHashValue = attr['value'].split('|')
else:
self.fileHashType = attr['type']
self.fileHashValue = attr['value']
if self.fileHashType not in ['sha1', 'sha256', 'md5', 'authenticodeHash256', 'lsHash', 'ctph']:
self.fileHashType = "unknown"
def _handle_email_src(self, attr):
if attr['type'] == 'email-src':
self.emailSenderAddress = attr['value']
self.emailSourceDomain = attr['value'].split('@')[1]
def _handle_ip_port(self, attr):
if attr['type'] == 'ip-dst|port' or attr['type'] == 'ip-src|port':
ip = attr['value'].split('|')[0]
port = attr['value'].split('|')[1]
if attr['type'] == 'ip-dst|port':
self.networkDestinationPort = port
if '.' in attr['value']:
self.networkDestinationIPv4 = ip
if config.network_ignore_direction:
self.networkIPv4 = ip
self.networkPort = port
else:
self.networkDestinationIPv6 = ip
if config.network_ignore_direction:
self.networkIPv6 = ip
self.networkPort = port
elif attr['type'] == 'ip-src|port':
self.networkSourcePort = port
if '.' in attr['value']:
self.networkSourceIPv4 = ip
if config.network_ignore_direction:
self.networkIPv4 = ip
self.networkPort = port
else:
self.networkSourceIPv6 = ip
if config.network_ignore_direction:
self.networkIPv6 = ip
self.networkPort = port
def _handle_special_cases(self, attr):
self._aggregated_handle_ip(attr)
self._handle_domain_ip(attr)
self._handle_email_src(attr)
self._handle_ip_port(attr)
self._handle_file_hash(attr)
self._handle_url(attr)
def _handle_url(self, attr):
if attr['type'] == 'url':
if not attr['value'].startswith(('http://', 'https://')):
self.url = "http://{}".format(attr['value'])
else:
self.url = attr['value']
def _handle_domain_ip(self, attr):
if attr['type'] == 'domain|ip':
self.domainName, ip = attr['value'].split('|')
if '.' in ip:
self.networkIPv4 = ip
else:
self.networkIPv6 = ip