-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproxy_wrap.py
100 lines (87 loc) · 2.56 KB
/
proxy_wrap.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
#!/usr/bin/env python
##
# class wrapper for proxy configurations
# original code by fitblip (http://www.talesofacoldadmin.com/)
# all i did was a little rewriting and wrapping it in a class
# for easier usage.
#
# https://github.com/ohdae/misc-snippets
##
from socket import *
import socks
import requests
class Proxy(object):
def __init__(self):
self.orig_sock = socket
self.running = False
def getaddrinfo(self, *args):
"""
simple monkey patch for DNS proxying
"""
return [(AF_INET, SOCK_STREAM, 6, '', (args[0], args[1]))]
getaddrinfo = self.getaddrinfo
def set(self, type=socks.PROXY_TYPE_SOCKS5, host='127.0.0.1', port='9050'):
"""
configure and active current proxy. change arguments as needed
"""
socks.setdefaultproxy(type, host, port)
self.socket = socks.socksocket
self.running = True
def unset(self):
"""
returns socket to original state (no proxy)
"""
self.socket = self.orig_sock
self.running = False
def is_tor(self):
"""
checks if tor is properly enabled
"""
if "Sorry" in requests.get('https://check.torproject.org/').content():
return False
return True
def tor_newnym(self, control_host='127.0.0.1', control_port=9051, password):
self.s = socket()
self.s.connect((control_host, control_port))
self.s.send('Authenticate %s\r\n' % password)
if not '250' in self.s.recv(1024):
return False
self.s.send('signal newnym\r\n')
if not '250' in self.s.recv(1024):
return False
self.s.close()
return True
def get_ip(self):
"""
gather and return current ip address
"""
self.current_ip = requests.get('http://ifconfig.me/ip').content().strip()
return self.current_ip
if __name__ == '__main__':
proxy = Proxy()
"""
setup TOR SOCKS5 proxy =>
print('[*] setting socks5 proxy 127.0.0.1:9050')
proxy.set(type=socks.PROXY_TYPE_SOCKS5, host='127.0.0.1', port=9050)
if proxy.is_tor:
print('[+] tor proxy running!')
print('[*] triggering newnym in 30 seconds ...')
import time; time.sleep(30)
if not proxy.tor_newnym('mypassword'):
print('[!] failed to get newnym!')
print('[+] newnym sent successfully!')
else:
print('[!] tor proxy is not running correctly!')
print('IP Address: %s' % proxy.get_ip)
setup SOCKS5 proxy on port 4444 =>
print('[*] setting socks5 proxy 127.0.0.1:4444')
proxy.set(type=socks.PROXY_TYPE_SOCKS5, host='127.0.0.1', port=4444)
print('IP Address: %s' % proxy.get_ip)
disable currently running proxy =>
print('[*] disabling proxy')
if proxy.running:
proxy.unset()
else:
print('[!] proxy does not appear to be running!')
print('IP Address: %s' % proxy.get_ip)
"""