-
Notifications
You must be signed in to change notification settings - Fork 1
/
MaltegoTransform.py
258 lines (221 loc) · 8.11 KB
/
MaltegoTransform.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#
# Updated Maltego Python library
# 2013/03/30
# See TRX documentation
#
# RT
import sys
from xml.dom import minidom
BOOKMARK_COLOR_NONE = "-1"
BOOKMARK_COLOR_BLUE = "0"
BOOKMARK_COLOR_GREEN = "1"
BOOKMARK_COLOR_YELLOW = "2"
BOOKMARK_COLOR_ORANGE = "3"
BOOKMARK_COLOR_RED = "4"
LINK_STYLE_NORMAL = "0"
LINK_STYLE_DASHED = "1"
LINK_STYLE_DOTTED = "2"
LINK_STYLE_DASHDOT = "3"
UIM_FATAL = "FatalError"
UIM_PARTIAL = "PartialError"
UIM_INFORM = "Inform"
UIM_DEBUG = "Debug"
class MaltegoEntity(object):
value = ""
weight = 100
displayInformation = []
additionalFields = []
iconURL = ""
entityType = "Phrase"
def __init__(self, eT=None, v=None, additionFields=[]):
if eT is not None:
self.entityType = eT
if v is not None:
self.value = v
self.additionalFields = additionFields
self.weight = 100
self.displayInformation = []
self.iconURL = ""
def setType(self, eT=None):
if eT is not None:
self.entityType = eT
def setValue(self, eV=None):
if eV is not None:
self.value = eV
def setWeight(self, w=None):
if w is not None:
self.weight = w
def addDisplayInformation(self, di=None, dl="Info"):
if di is not None:
self.displayInformation.append([dl, di])
def addProperty(
self, fieldName=None, displayName=None, matchingRule=False, value=None
):
self.additionalFields.append([fieldName, displayName, matchingRule, value])
def setIconURL(self, iU=None):
if iU is not None:
self.iconURL = iU
def setLinkColor(self, color):
self.addProperty("link#maltego.link.color", "LinkColor", "", color)
def setLinkStyle(self, style):
self.addProperty("link#maltego.link.style", "LinkStyle", "", style)
def setLinkThickness(self, thick):
self.addProperty("link#maltego.link.thickness", "Thickness", "", str(thick))
def setLinkLabel(self, label):
self.addProperty("link#maltego.link.label", "Label", "", label)
def setBookmark(self, bookmark):
self.addProperty("bookmark#", "Bookmark", "", bookmark)
def setNote(self, note):
self.addProperty("notes#", "Notes", "", note)
def returnEntity(self):
r = ""
r += '<Entity Type="' + str(self.entityType) + '">'
r += "<Value>" + str(self.value) + "</Value>"
r += "<Weight>" + str(self.weight) + "</Weight>"
if len(self.displayInformation) > 0:
r += "<DisplayInformation>"
for i in range(len(self.displayInformation)):
r += (
'<Label Name="'
+ self.displayInformation[i][0]
+ '" Type="text/html"><![CDATA['
+ str(self.displayInformation[i][1])
+ "]]></Label>"
)
r += "</DisplayInformation>"
if len(self.additionalFields) > 0:
r += "<AdditionalFields>"
for i in range(len(self.additionalFields)):
if str(self.additionalFields[i][2]) != "strict":
r += (
'<Field Name="'
+ str(self.additionalFields[i][0])
+ '" DisplayName="'
+ str(self.additionalFields[i][1])
+ '">'
+ str(self.additionalFields[i][3])
+ "</Field>"
)
else:
r += (
'<Field MatchingRule="'
+ str(self.additionalFields[i][2])
+ '" Name="'
+ str(self.additionalFields[i][0])
+ '" DisplayName="'
+ str(self.additionalFields[i][1])
+ '">'
+ str(self.additionalFields[i][3])
+ "</Field>"
)
r += "</AdditionalFields>"
if len(self.iconURL) > 0:
r += "<IconURL>" + self.iconURL + "</IconURL>"
r += "</Entity>"
return r
class MaltegoTransform(object):
# We were lazy to use a proper XML library to generate
# our XML. Thus - encode data before you insert!
# ..Sorry - RT
entities = []
exceptions = []
UIMessages = []
def __init__(self):
self.entities = []
self.exceptions = []
self.UIMessages = []
self = None
def addEntity(self, enType=None, enValue=None):
me = MaltegoEntity(enType, enValue)
self.entities.append(me)
return me
def addUIMessage(self, message, messageType="Inform"):
self.UIMessages.append([messageType, message])
def addException(self, exceptionString):
self.exceptions.append(exceptionString)
def throwExceptions(self):
r = ""
r += "<MaltegoMessage>"
r += "<MaltegoTransformExceptionMessage>"
r += "<Exceptions>"
for i in range(len(self.exceptions)):
r += "<Exception>" + self.exceptions[i] + "</Exceptions>"
r += "</Exceptions>"
r += "</MaltegoTransformExceptionMessage>"
r += "</MaltegoMessage>"
return r
def returnOutput(self):
r = ""
r += "<MaltegoMessage>"
r += "<MaltegoTransformResponseMessage>"
r += "<Entities>"
for i in range(len(self.entities)):
r += self.entities[i].returnEntity()
r += "</Entities>"
r += "<UIMessages>"
for i in range(len(self.UIMessages)):
r += (
'<UIMessage MessageType="'
+ self.UIMessages[i][0]
+ '">'
+ self.UIMessages[i][1]
+ "</UIMessage>"
)
r += "</UIMessages>"
r += "</MaltegoTransformResponseMessage>"
r += "</MaltegoMessage>"
return r
class MaltegoMsg:
def __init__(self, MaltegoXML=""):
xmldoc = minidom.parseString(MaltegoXML)
# read the easy stuff like value, limits etc
self.Value = self.i_getNodeValue(xmldoc, "Value")
self.Weight = self.i_getNodeValue(xmldoc, "Weight")
self.Slider = int(self.i_getNodeAttributeValue(xmldoc, "Limits", "SoftLimit"))
self.Type = self.i_getNodeAttributeValue(xmldoc, "Entity", "Type")
# read additional fields
Properties = {}
try:
AFNodes = xmldoc.getElementsByTagName("AdditionalFields")[0]
Settings = AFNodes.getElementsByTagName("Field")
for node in Settings:
AFName = node.attributes["Name"].value
AFValue = self.i_getText(node.childNodes)
Properties[AFName] = AFValue
except:
# sure this is not the right way...;)
dontcare = 1
# parse transform settings
TransformSettings = {}
try:
TSNodes = xmldoc.getElementsByTagName("TransformFields")[0]
Settings = TSNodes.getElementsByTagName("Field")
for node in Settings:
TSName = node.attributes["Name"].value
TSValue = self.i_getText(node.childNodes)
TransformSettings[TSName] = TSValue
except:
dontcare = 1
# load back into object
self.Properties = Properties
self.TransformSettings = TransformSettings
def i_getText(self, nodelist):
rc = []
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc.append(node.data)
return "".join(rc)
def i_getNodeValue(self, node, Tag):
return self.i_getText(node.getElementsByTagName(Tag)[0].childNodes)
def i_getNodeAttributeValue(self, node, Tag, Attribute):
return node.getElementsByTagName(Tag)[0].attributes[Attribute].value
def getProperty(self, skey):
if skey in self.Properties.keys():
return self.Properties[skey]
else:
return None
def getTransformSetting(self, skey):
if skey in self.TransformSettings.keys():
return self.TransformSettings[skey]
else:
return None