-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
doxycannon.py
executable file
·577 lines (467 loc) · 16.2 KB
/
doxycannon.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
#!/usr/bin/env python3
import argparse
import os
import re
import signal
import sys
from pathlib import Path
from threading import Thread
from queue import Queue
import docker
VERSION = '0.5.1'
IMAGE = 'audibleblink/doxycannon'
TOR = 'audibleblink/tor'
DOXY = 'audibleblink/doxyproxy'
NET = 'doxy_network'
THREADS = 10
START_PORT = 9000
HAPORT = 1337
PROXYCHAINS_CONF = './proxychains.conf'
PROXYCHAINS_TEMPLATE = """
# This file is automatically generated by doxycannon. If you need changes,
# make them to the template string in doxycannon.py
random_chain
quiet_mode
proxy_dns
remote_dns_subnet 224
tcp_read_time_out 15000
tcp_connect_time_out 8000
[ProxyList]
"""
HAPROXY_CONF = './haproxy/haproxy.cfg'
HAPROXY_TEMPLATE = """
# This file is automatically generated by doxycannon. If you need changes,
# make them to the template string in doxycannon.py
global
daemon
user root
group root
defaults
mode tcp
maxconn 3000
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
listen funnel_proxy
bind *:1337
mode tcp
balance roundrobin
default_backend doxycannon
backend doxycannon
"""
def build(image, path='.'):
"""Builds the image with the given name"""
try:
print(f"[+] Building image: {image}.")
doxy.images.build(path=path, tag=image, forcerm=True)
except Exception as err:
print(err)
raise
def vpn_file_list(folder):
files = Path(folder).rglob('*.ovpn')
return [re.sub(".ovpn", "", c.name) for c in files]
def vpn_file_queue(folder):
"""Returns a Queue of files from the given directory"""
files = Path(folder).rglob('*.ovpn')
# files = glob.glob(folder + '/**/*.ovpn')
jobs = Queue(maxsize=0)
for f in files:
jobs.put(f)
return jobs
def write_config(filename, data, conf_type):
""" Write data to a given filename
The `type` argument determines what template gets written
at the beginning of the config file. Types are either
'haproxy' or 'proxychains'
"""
with open(filename, 'w') as f:
if conf_type == 'haproxy':
f.write(HAPROXY_TEMPLATE)
elif conf_type == 'proxychains':
f.write(PROXYCHAINS_TEMPLATE)
for line in data:
f.write(line + "\n")
def write_haproxy_conf(names, port_range):
"""Generates HAProxy config based on # of ovpn files"""
print("[+] Writing HAProxy configuration")
conf_line = "\tserver doxy{0} {1}:1080 check"
data = list(map(lambda x: conf_line.format(x[0], x[1]), zip(port_range, names)))
write_config(HAPROXY_CONF, data, 'haproxy')
def write_proxychains_conf(port_range):
"""Generates Proxychains4 config based on # of ovpn files"""
print("[+] Writing Proxychains4 configuration")
conf_line = "socks5 127.0.0.1 {}"
data = list(map(lambda x: conf_line.format(x), port_range))
write_config(PROXYCHAINS_CONF, data, 'proxychains')
def containers_from_image(image, list_all=False):
"""Returns a Queue of containers whose source image match image"""
jobs = Queue(maxsize=0)
containers = list(
filter(
lambda x: image in x.attrs['Config']['Image'],
doxy.containers.list(all=list_all)
)
)
for container in containers:
jobs.put(container)
return jobs
def running_containers(image):
return [c.name for c in list(
filter(
lambda x: image in x.attrs['Config']['Image'],
doxy.containers.list(all=False)
)
)]
def multikill(jobs, to_filter):
"""Handler to job killer. Called by the Thread worker function."""
while True:
container = jobs.get()
if to_filter:
if container.name in to_filter:
print(f"Stopping: {container.name}")
container.kill(9)
else:
print(f"Stopping: {container.name}")
container.kill(9)
jobs.task_done()
def delete_container(jobs):
"""Handler to clean task. Called by the Thread worker function."""
while True:
container = jobs.get()
print(f"Deleting: {container.name}")
container.remove(force=True)
jobs.task_done()
def clean(image):
"""Find all containers with <image> in the imagename and
delete them.
"""
container_queue = containers_from_image(image, list_all=True)
for _ in range(THREADS):
worker = Thread(target=delete_container, args=(container_queue,))
worker.setDaemon(True)
worker.start()
container_queue.join()
print(f"[+] Delete request sent to daemon for all containers based on image {image}")
def down(image, conf):
"""Find all containers from an image name and start workers for them.
The workers are tasked with running the job killer function
"""
container_queue = containers_from_image(image)
if conf:
to_filter = vpn_file_list(conf)
else:
to_filter = None
for _ in range(THREADS):
worker = Thread(target=multikill, args=(container_queue,to_filter))
worker.setDaemon(True)
worker.start()
container_queue.join()
try:
doxy.networks.get(NET).remove()
except:
print(f"[?] Cannot remove network, either it has already been removed or containers are still running")
print(f"[+] All containers based on {image} have been issued a kill command")
def multistart(image, jobs, ports, running_container_list):
"""Handler for starting containers. Called by Thread worker function."""
while True:
port = ports.get()
config = jobs.get()
# if config is str, then multistart was called from tor command. Else, config is PosixPath
if isinstance(config, str):
container_name = config
parent = 'tor'
else:
container_name = re.sub(".ovpn", "", config.name)
parent = config.parent
if container_name in running_container_list:
print(f"Already running: {container_name}, path is {parent}")
ports.task_done()
jobs.task_done()
return
else:
print(f"Starting: {container_name} on port {port}, path is {parent}")
try:
doxy.containers.run(
image,
auto_remove=True,
privileged=True,
sysctls={"net.ipv6.conf.all.disable_ipv6": 0},
ports={'1080/tcp': ('127.0.0.1', port)},
network=NET,
environment=[f"VPN={container_name}", f"VPNPATH=/{parent}"],
name=container_name,
detach=True)
except docker.errors.APIError as err:
print(f"[!] Failed to start {container_name}: {err.explanation}")
# port = port + 1
ports.task_done()
jobs.task_done()
def start_containers(image, ovpn_queue, port_range, running_container_list):
"""Starts workers that call the container creation function"""
port_queue = Queue(maxsize=0)
for p in port_range:
port_queue.put(p)
for _ in range(THREADS):
worker = Thread(
target=multistart,
args=(image, ovpn_queue, port_queue,running_container_list,))
worker.setDaemon(True)
worker.start()
ovpn_queue.join()
print('[+] All containers have been issued a start command')
def up(image, conf, port):
"""Kick off the `up` process that starts all the containers
Writes the configuration files and starts starts container based
on the number of *.ovpn files in the VPN folder
"""
try:
doxy.networks.get(NET)
print("[?] Network already exists")
except docker.errors.NotFound:
doxy.networks.create(NET, driver="bridge", attachable=True)
if not doxy.images.list(name=image):
build(image)
ovpn_file_queue = vpn_file_queue(conf)
print("[+] List of VPN files:")
for p in list(ovpn_file_queue.queue):
print("\t[?]", str(p))
ovpn_file_count = len(list(ovpn_file_queue.queue))
running_container_list = running_containers(image)
nb_running = len(running_container_list)
names = [re.sub(".ovpn", "", name.name) for name in ovpn_file_queue.queue]
if port:
port_range = range(port, port + ovpn_file_count)
port_range2 = range(port + nb_running, port + ovpn_file_count + nb_running)
else:
port_range = range(START_PORT + nb_running, START_PORT + ovpn_file_count + nb_running)
port_range2 = range(START_PORT, START_PORT + ovpn_file_count + nb_running)
# port_range2 is without already running containers
write_haproxy_conf(names, port_range2)
write_proxychains_conf(port_range2)
start_containers(image, ovpn_file_queue, port_range, running_container_list)
def tor(count):
"""Start <count> tor nodes to proxy through
Will take the given number of tor nodes and start a proxy
rotator that cycles through the tor nodes
"""
if not doxy.images.list(name=TOR):
build(TOR, path='./tor/')
try:
doxy.networks.get(NET)
print("[?] Network already exists")
except docker.errors.NotFound:
doxy.networks.create(NET, driver="bridge", attachable=True)
running_container_list = running_containers(TOR)
nb_running = len(running_container_list)
port_range = range(START_PORT + nb_running, START_PORT + count + nb_running)
name_queue = Queue(maxsize=0)
names = []
for port in port_range:
name_queue.put(f"tor_{port}")
names.append(f"tor_{port}")
write_haproxy_conf(names, port_range)
start_containers(TOR, name_queue, port_range, running_container_list)
def rotate():
"""Creates a proxy rotator, HAProxy, based on the port range provided"""
try:
build(DOXY, path='./haproxy')
print('[*] Staring single-port mode...')
print(f"[*] Proxy rotator listening on port {HAPORT}. Ctrl-c to quit")
signal.signal(signal.SIGINT, signal_handler)
cname = DOXY.split("/")[1]
doxy.containers.run(DOXY, name=cname, auto_remove=True, network=NET, ports={'1337/tcp': ('127.0.0.1', HAPORT)})
except Exception as err:
print(err)
raise
def single(image, conf=None, nodes=None):
"""Starts an HAProxy rotator.
Builds and starts the HAProxy container in the haproxy folder
This will create a local socks5 proxy on port $HAPORT that will
allow one to configure applications with SOCKS proxy options.
Ex: Firefox, BurpSuite, etc.
"""
if not list(containers_from_image(image).queue):
if nodes:
tor(args.nodes)
elif conf:
up(image, conf)
rotate()
def interactive(image, conf):
"""Starts the interactive process. Requires Proxychains4
Creates a shell session where network connections are routed through
proxychains. Started GUI application from here rarely works
"""
try:
if not list(containers_from_image(image).queue):
up(image, conf)
else:
ovpn_file_count = len(list(vpn_file_queue(args.dir).queue))
port_range = range(START_PORT, START_PORT + ovpn_file_count)
write_proxychains_conf(port_range)
os.system("proxychains4 bash")
except Exception as err:
print(err)
raise
def signal_handler(*args):
"""Traps ctrl+c for cleanup, then exits"""
sys.stdout = open(os.devnull, 'w')
if list(containers_from_image(DOXY).queue):
down(DOXY, None)
sys.stdout = sys.__stdout__
print(f"\n[*] {DOXY} was issued a stop command")
print('[*] Your proxies are still running.')
sys.exit(0)
def get_parsed():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command")
tor_cmd = subparsers.add_parser('tor', help="tor --help")
tor_cmd.add_argument(
'--nodes',
type=int,
default=3,
dest="nodes",
# required=True,
help="Number of tor nodes to rotate through. Default: 3")
tor_cmd.add_argument(
'--up',
action='store_true',
default=False,
dest='up',
help='Brings up tor containers. 1 for each [--nodes]')
tor_cmd.add_argument(
'--down',
action='store_true',
default=False,
dest='down',
help='Bring down all tor containers')
tor_cmd.add_argument(
'--single',
action='store_true',
default=False,
dest='single',
help='Start an HAProxy rotator on a single port. Useful for Burpsuite')
tor_cmd.add_argument(
'--build',
action='store_true',
default=False,
dest='build',
help='Build tor image.')
tor_cmd.add_argument(
'--clean',
action='store_true',
default=False,
dest='clean',
help='Delete all dangling tor containers. Useful for duplicate container errors')
vpn_cmd = subparsers.add_parser('vpn', help="vpn --help")
vpn_cmd.add_argument(
"--port",
action="store",
type=int,
dest="port",
default=None,
required=False,
help="Choose custom port for containers. Port is not checked, if containers are already running, doxycannon will crash")
vpn_group = vpn_cmd.add_mutually_exclusive_group()
vpn_group.add_argument(
'--up',
action='store_true',
default=False,
dest='up',
help='Brings up containers. 1 for each VPN file in [dir]')
vpn_cmd.add_argument(
'--down',
action='store_true',
default=False,
dest='down',
help='Bring down all the containers')
vpn_cmd.add_argument(
'--single',
action='store_true',
default=False,
dest='single',
help='Start an HAProxy rotator on a single port. Useful for Burpsuite')
vpn_cmd.add_argument(
'--clean',
action='store_true',
default=False,
dest='clean',
help='Delete all dangling VPN containers. Useful for duplicate container errors')
vpn_cmd.add_argument(
'--dir',
default="VPN",
dest='dir',
help='Specify a directory to use for VPN config')
vpn_cmd.add_argument(
'--interactive',
action='store_true',
default=False,
dest='interactive',
help="Starts an interactive bash session where network connections" +
" are routed through proxychains. Requires proxychainvs v4+")
vpn_cmd.add_argument(
'--build',
action='store_true',
default=False,
dest='build',
help='Build doxyproxy image.')
parser.add_argument(
'--nuke',
action='store_true',
default=False,
dest='nuke',
help='Delete all dangling vpn, tor, doxyproxy containers.')
parser.add_argument(
'--version',
action='version',
version="%(prog)s {VERSION}")
return parser.parse_args()
def handle_tor(args):
if args.clean:
clean(TOR)
elif args.build:
build(TOR, path="./tor/")
elif args.up:
tor(args.nodes)
elif args.down:
down(TOR, None)
elif args.single:
single(TOR, nodes=args.nodes)
def handle_vpn(args):
if args.clean:
clean(IMAGE)
elif args.build:
build(IMAGE)
elif args.up:
up(IMAGE, args.dir, args.port)
elif args.down:
down(IMAGE, args.dir)
elif args.single:
single(IMAGE, conf=args.dir)
elif args.interactive:
interactive(IMAGE, args.dir)
def main(args):
if args.command == "tor":
handle_tor(args)
elif args.command == "vpn":
handle_vpn(args)
elif args.nuke:
try:
network = doxy.networks.get(NET)
network.remove()
except docker.errors.APIError as err:
print(f"[+] {err.explanation}")
for i in [IMAGE, TOR, DOXY]:
clean(i)
try:
doxy.images.remove(i)
print(f"[+] Image {i} deleted")
except docker.errors.APIError as err:
print(f"[!] {err.explanation}")
if __name__ == "__main__":
try:
doxy = docker.from_env()
except Exception:
print("Unable to contact local Docker daemon. Is it running?")
sys.exit(1)
args = get_parsed()
main(args)