forked from cssaheel/dissectors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smtp.py
executable file
·395 lines (354 loc) · 13.8 KB
/
smtp.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import base64
import base64
import os
import string
import random
from scapy.packet import *
from scapy.fields import *
from scapy.ansmachine import *
from scapy.layers.inet import *
import dissector
def name_generator(size=9, chars=string.ascii_uppercase + string.digits):
"""
this method is for generating a randndom name for the downloaded files
@param size: number of random characters
@param chars: type of the random characters
"""
return ''.join(random.choice(chars) for x in range(size))
src = ""
dst = ""
sport = ""
dport = ""
seq = ""
# holds smtp sessions
bounded = []
def get_tcp_ip():
"""
this method is for retrieving the ip and tcp values
"""
return src, dst, sport, dport, seq
def set_tcp_ip(srcp, dstp, sportp, dportp, seqp):
"""
this method is for set values in the global variables for tcp/ip
@param srcp: source ip address
@param dstp: destination ip address
@param sportp: source port number
@param dPortp: destination port number
@param seqp: sequence number
"""
global src, dst, sport, dport, seq
src = srcp
dst = dstp
sport = sportp
dport = dportp
seq = seqp
def bind(Src, Dst, Port):
"""
method for creating smtp data sessions
@param Src: source ip address
@param Dst: destination ip address
@param Port: source port number
"""
bounded.append([Src, Dst, Port])
def unbind(Src, Dst, Port):
"""
do the opposite of bind()
"""
if [Src, Dst, Port] in bounded:
bounded.remove([Src, Dst, Port])
def is_bounded(Src, Dst, Port):
"""
returns true if the session is already bounded
@param Src: source ip address
@param Dst: destination ip address
@param Port: source port number
"""
if [Src, Dst, Port] in bounded:
return True
return False
class SMTPDataField(XByteField):
"""
this is a field class for handling the smtp data
@attention: this class inherets XByteField
"""
holds_packets = 1
name = "SMTPDataField"
myresult = ""
def __init__(self, name, default):
"""
class constructor, for initializing instance variables
@param name: name of the field
@param default: Scapy has many formats to represent the data
internal, human and machine. anyways you may sit this param to None.
"""
self.name = name
self.fmt = "!B"
Field.__init__(self, name, default, "!B")
def getfield(self, pkt, s):
"""
this method will get the packet, takes what does need to be
taken and let the remaining go, so it returns two values.
first value which belongs to this field and the second is
the remaining which does need to be dissected with
other "field classes".
@param pkt: holds the whole packet
@param s: holds only the remaining data which is not dissected yet.
"""
src, dst, sport, dport, seq = get_tcp_ip()
cstream = -1
cstream = dissector.check_stream(src, dst, sport, dport, seq, s)
if not cstream == -1:
s = cstream
if cstream == -1:
return "", ""
name = name_generator()
if not dissector.Dissector.default_download_folder_changed:
cwd = os.getcwd() + "/downloaded/"
try:
os.mkdir("downloaded")
except:
None
f = open(cwd + name, "wb")
else:
f = open(dissector.Dissector.path + name, "wb")
f.write(s)
f.close()
self.myresult = ""
for c in s:
self.myresult = self.myresult + base64.standard_b64encode(c)
return "", self.myresult
class SMTPResField(StrField):
"""
this is a field class for handling the smtp data
@attention: this class inherets StrField
"""
holds_packets = 1
name = "SMTPReField"
def get_code_msg(self, cn):
"""
method returns a message for every a specific code number
@param cn: code number
"""
codes = {
"500": "Syntax error, command unrecognized",
"501": "Syntax error in parameters or arguments",
"502": "Command not implemented",
"503": "Bad sequence of commands",
"504": "Command parameter not implemented",
"211": "System status, or system help reply",
"214": "Help message",
"220": "<domain> Service ready",
"221": "<domain> Service closing transmission channel",
"421": "<domain> Service not available,\
closing transmission channel",
"250": "Requested mail action okay, completed",
"251": "User not local; will forward to <forward-path>",
"450": "Requested mail action not taken: mailbox unavailable",
"550": "Requested action not taken: mailbox unavailable",
"451": "Requested action aborted: error in processing",
"551": "User not local; please try <forward-path>",
"452": "Requested action not taken: insufficient system\
storage",
"552": "Requested mail action aborted: exceeded storage\
allocation",
"553": "Requested action not taken: mailbox name not allowed",
"354": "Start mail input; end with <CRLF>.<CRLF>",
"554": "Transaction failed",
"211": "System status, or system help reply",
"214": "Help message",
"220": "<domain> Service ready",
"221": "<domain> Service closing transmission channel",
"250": "Requested mail action okay, completed",
"251": "User not local; will forward to <forward-path>",
"354": "Start mail input; end with <CRLF>.<CRLF>",
"421": "<domain> Service not available, closing \
transmission channel",
"450": "Requested mail action not taken: mailbox unavailable",
"451": "Requested action aborted: local error in processing",
"452": "Requested action not taken: insufficient system\
storage",
"500": "Syntax error, command unrecognized",
"501": "Syntax error in parameters or arguments",
"502": "Command not implemented",
"503": "Bad sequence of commands",
"504": "Command parameter not implemented",
"550": "Requested action not taken: mailbox unavailable",
"551": "User not local; please try <forward-path>",
"552": "Requested mail action aborted: exceeded storage\
allocation",
"553": "Requested action not taken: mailbox name not allowed",
"554": "Transaction failed"}
if cn in codes:
return codes[cn]
return "Unknown Response Code"
def getfield(self, pkt, s):
"""
this method will get the packet, takes what does need to be
taken and let the remaining go, so it returns two values.
first value which belongs to this field and the second is
the remaining which does need to be dissected with
other "field classes".
@param pkt: holds the whole packet
@param s: holds only the remaining data which is not dissected yet.
"""
cstream = -1
if pkt.underlayer.name == "TCP":
cstream = dissector.check_stream(\
pkt.underlayer.underlayer.fields["src"],\
pkt.underlayer.underlayer.fields["dst"],\
pkt.underlayer.fields["sport"],\
pkt.underlayer.fields["dport"],\
pkt.underlayer.fields["seq"], s)
if not cstream == -1:
s = cstream
remain = ""
value = ""
ls = s.splitlines()
length = len(ls)
if length == 1:
value = ls[0]
arguments = ""
first = True
res = value.split(" ")
for arg in res:
if not first:
arguments = arguments + arg + " "
first = False
if "-" in res[0]:
value = "(" + res[0][:3] + ") " +\
self.get_code_msg(res[0][:3]) + " " + res[0][3:]
else:
value = "(" + res[0] + ") " + self.get_code_msg(res[0])
return arguments[:-1], [value]
if length > 1:
reponses = []
for element in ls:
element = element.split(" ")
arguments = ""
first = True
for arg in element:
if not first:
arguments = arguments + arg + " "
first = False
if "-" in element[0]:
reponses.append(["(" + element[0][:3] + ") " +
self.get_code_msg(element[0][:3]) +
" " + element[0][3:], arguments[:-1]])
else:
reponses.append(["(" + element[0] + ") " +
self.get_code_msg(element[0][:-1]),
arguments])
return "", reponses
return "", ""
def __init__(self, name, default, fmt, remain=0):
"""
class constructor for initializing the instance variables
@param name: name of the field
@param default: Scapy has many formats to represent the data
internal, human and machine. anyways you may sit this param to None.
@param fmt: specifying the format, this has been set to "H"
@param remain: this parameter specifies the size of the remaining
data so make it 0 to handle all of the data.
"""
self.name = name
StrField.__init__(self, name, default, fmt, remain)
class SMTPReqField(StrField):
holds_packets = 1
name = "SMTPReqField"
def getfield(self, pkt, s):
"""
this method will get the packet, takes what does need to be
taken and let the remaining go, so it returns two values.
first value which belongs to this field and the second is
the remaining which does need to be dissected with
other "field classes".
@param pkt: holds the whole packet
@param s: holds only the remaining data which is not dissected yet.
"""
cstream = -1
if pkt.underlayer.name == "TCP":
cstream = dissector.check_stream(\
pkt.underlayer.underlayer.fields["src"],\
pkt.underlayer.underlayer.fields["dst"],\
pkt.underlayer.fields["sport"],\
pkt.underlayer.fields["dport"],\
pkt.underlayer.fields["seq"], s)
if not cstream == -1:
s = cstream
remain = ""
value = ""
ls = s.split()
length = len(ls)
if ls[0].upper() == "DATA":
bind(pkt.underlayer.underlayer.fields["src"],
pkt.underlayer.underlayer.fields["dst"],
pkt.underlayer.fields["sport"])
return "", "DATA"
if ls[0].upper() == "QUIT":
unbind(pkt.underlayer.underlayer.fields["src"],
pkt.underlayer.underlayer.fields["dst"],
pkt.underlayer.fields["sport"])
return "", "QUIT"
if is_bounded(pkt.underlayer.underlayer.fields["src"],
pkt.underlayer.underlayer.fields["dst"],
pkt.underlayer.fields["sport"]):
set_tcp_ip(pkt.underlayer.underlayer.fields["src"],
pkt.underlayer.underlayer.fields["dst"],
pkt.underlayer.fields["sport"],\
pkt.underlayer.fields["dport"],\
pkt.underlayer.fields["seq"])
smtpd = SMTPData(s).fields["data"]
return "", ["DATA", smtpd]
if length > 1:
value = ls[0]
if length == 2:
remain = ls[1]
return remain, value
else:
i = 1
remain = ' '
while i < length:
remain = remain + ls[i] + ' '
i = i + 1
return remain[:-1], value
else:
return "", ls[0]
def __init__(self, name, default, fmt, remain=0):
"""
class constructor for initializing the instance variables
@param name: name of the field
@param default: Scapy has many formats to represent the data
internal, human and machine. anyways you may sit this param to None.
@param fmt: specifying the format, this has been set to "H"
@param remain: this parameter specifies the size of the remaining
data so make it 0 to handle all of the data.
"""
self.name = name
StrField.__init__(self, name, default, fmt, remain)
class SMTPData(Packet):
"""
class for handling the smtp data
@attention: this class inherets Packet
"""
name = "smtp"
fields_desc = [SMTPDataField("data", "")]
class SMTPResponse(Packet):
"""
class for handling the smtp responses
@attention: this class inherets Packet
"""
name = "smtp"
fields_desc = [SMTPResField("response", "", "H"),
StrField("argument", "", "H")]
class SMTPRequest(Packet):
"""
class for handling the smtp requests
@attention: this class inherets Packet
"""
name = "smtp"
fields_desc = [SMTPReqField("command", '', "H"),
StrField("argument", '', "H")]
bind_layers(TCP, SMTPResponse, sport=25)
bind_layers(TCP, SMTPRequest, dport=25)
bind_layers(TCP, SMTPResponse, sport=587)
bind_layers(TCP, SMTPRequest, dport=587)