-
Notifications
You must be signed in to change notification settings - Fork 27
/
netzobparse.py
172 lines (147 loc) · 6.64 KB
/
netzobparse.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
#!/usr/bin/env python
# Copyright (C) 2013 Buttinsky Developers.
# See 'COPYING' for copying permission.
"""
- Parses a Netzob project exported as XML.
- Handles state machine updates based on protocol model and input to handleInput.
- Botnet mimicking (symbol output) is done when a state transition occurs.
Currently a PoC.
The following protocol shows model of 'behaviors/models/customNetzobIRCModel.xml'
and can be used for similar text based protocols such as IRC and HTTP.
On channel join:
Bot: im alive
Botherder: hail the master
Bot: hello world, my name is settings($nick)
Botherder: ddos $domain
Bot: ddosing $domain
TODO:
- Support for several variables in protocol messages
- Output symbols based on probability and add handle delay of certain response
- Save unsupported protocol messages in delimiter separated logs (for further modeling
of the unkown messages in Netzob).
- Support binary protocols
"""
import binascii
from xml.dom import minidom
class NetzobModelParser(object):
def __init__(self, path):
self.STATES = []
self.SYMBOLS = []
self.TRANSITIONS = {}
self.state = None
self.__parse_model(path)
def __findSymbol(self, id):
for s in self.SYMBOLS:
if s["id"] == id:
return s
def __findRef(self, id):
for s in self.SYMBOLS:
for f in s["fields"]:
if f["varId"] == id:
return s
def handleInput(self, input, settings, init=False):
msg = ""
if self.state == None:
initialState = self.STATES[0]["id"]
trans = self.TRANSITIONS[initialState]
if trans["input"] == None and init:
symbol = trans["outputs"][0]
found = self.__findSymbol(symbol)
for i in found["fields"]:
msg = msg + i["value"]
self.state = trans["endState"]
return msg
return ""
trans = self.TRANSITIONS[self.state]
found = self.__findSymbol(trans["input"])
inputSymbol = ""
for i in found["fields"]:
inputSymbol = inputSymbol + i["value"]
if input == inputSymbol:
found = self.__findSymbol(trans["outputs"][0])
outputSymbol = ""
for i in found["fields"]:
outputSymbol = outputSymbol + i["value"]
if "settings" in outputSymbol:
setting = outputSymbol.split("settings($")[1].split(")")[0]
msg = outputSymbol.replace("settings($" + setting + ")", settings[setting].encode("ascii"))
elif inputSymbol in input:
found = self.__findSymbol(trans["input"])
sp = input.split(inputSymbol)
found = self.__findSymbol(trans["outputs"][0])
for i in found["fields"]:
if len(i["ref"]) > 0:
for j in sp:
if len(j) > 0:
msg = msg + j
else:
msg = msg + i["value"]
if msg != "":
self.state = trans["endState"]
return msg
def __bin2Ascii(self, bin_text):
return ''.join(chr(int(bin_text[i:i+8], 2)) for i in xrange(0, len(bin_text), 8))
def __parse_model(self, path):
xmldoc = minidom.parse(path)
grammar = xmldoc.getElementsByTagName('netzob:grammar')
automata = grammar[0].getElementsByTagName('netzob:automata')
states = grammar[0].getElementsByTagName('netzob:states')
state = grammar[0].getElementsByTagName('netzob:state')
transitions = grammar[0].getElementsByTagName('netzob:transitions')
transition = grammar[0].getElementsByTagName('netzob:transition')
symbols = xmldoc.getElementsByTagName('netzob:symbols')
symbol = symbols[0].getElementsByTagName('netzob:symbol')
symbolID = 0
for s in symbol:
symbolID = s.getAttribute("id").encode("ascii")
f = s.getElementsByTagName("netzob:field")
fields = s.getElementsByTagName("netzob:fields")
string = ""
symbol = {}
symbol["id"] = symbolID
symbol["fields"] = list()
for field in fields:
f = field.getElementsByTagName("netzob:field")
for entry in f:
format = entry.getElementsByTagName("netzob:format")
var = entry.getElementsByTagName("netzob:variable")
for v in var:
id = v.getAttribute("id").encode("ascii")
type = var[0].getElementsByTagName("netzob:type")
mutable = var[0].getAttribute("mutable").encode("ascii")
type = type[0].firstChild.data.encode("ascii")
ref = ""
try:
ref = var[0].getElementsByTagName("netzob:ref")
ref = ref[0].firstChild.data.encode("ascii")
except:
pass
try:
value = var[0].getElementsByTagName("netzob:originalValue")
value = value[0].firstChild.data.encode("ascii")
except:
value = ""
symbol["fields"].append({"varId": id, "value": value,"ref":ref, "mutable":mutable})
self.SYMBOLS.append(symbol)
for node in state:
stateID = node.getAttribute("id").encode("ascii")
stateName = node.getAttribute("name").encode("ascii")
self.STATES.append({"id": stateID, "name": stateName})
for node in transition:
start = node.getElementsByTagName("netzob:startState")
startState = start[0].firstChild.data.encode("ascii")
end = node.getElementsByTagName("netzob:endState")
endState = end[0].firstChild.data.encode("ascii")
input = node.getElementsByTagName("netzob:input")
stateInput = input[0].getAttribute("symbol").encode("ascii")
if stateInput == "EmptySymbol":
stateInput = None
outputs = node.getElementsByTagName("netzob:outputs")
for outs in outputs:
out = outs.getElementsByTagName("netzob:output")
outSymbol = list()
for o in out:
outSymbol.append(o.getAttribute("symbol").encode('ascii'))
if len(outSymbol) == 0:
outSymbol = None
self.TRANSITIONS[startState] = {"endState": endState, "input": stateInput, "outputs": outSymbol}