-
Notifications
You must be signed in to change notification settings - Fork 1
/
MiningPerturbation_sock.py
199 lines (153 loc) · 5.9 KB
/
MiningPerturbation_sock.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
from tabnanny import verbose
from scapy.all import *
from random import randint
from netfilterqueue import NetfilterQueue
import os
import sys
import socket
import threading
import time
HEX_FILTER = ''.join(
[(len(repr(chr(i))) == 3) and chr(i) or '.' for i in range(256)])
def hexdump(src, length = 16, show = True):
if isinstance(src, bytes):
src = src.decode()
results = list()
for i in range(0, len(src), length):
word = str(src[i:i+length])
printable = word.translate(HEX_FILTER)
hexa = ' '.join([f'{ord(c):02X}' for c in word])
hexwidth = length * 3
results.append(f'{i:04x} {hexa:<{hexwidth}} {printable}')
if show:
for line in results:
print(line)
else:
return results
def receive_from(connection):
buffer = b""
connection.settimeout(5)
try:
while True:
data = connection.recv(4096)
if not data:
break
buffer += data
except Exception as e:
pass
return buffer
def request_handler(local_buffer, remote_host, remote_port):
# perform packet modifications
class Request(object):
def __init__(self, verb, *args):
self.verb = verb
self.args = [str(x) for x in args]
# dummy packet
# send(IP(dst = remote_host)/ICMP())
# send(Ether()/IP(dst = local_buffer.getsockname()[0], ttl =(1,1)), iface="eth0")
send(IP(dst = remote_host, ttl = (1,1))/TCP(flags = "S", sport = RandShort(), dport = remote_port)/Raw(""))
# splitting
fragsize = 500
# bufsize = local_buffer.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
buffsize = len(local_buffer)
fragsize = (fragsize + 7) // 8 * 8
if(buffsize > fragsize):
print(local_buffer)
s = [local_buffer.decode("utf-8")[i:i+1] for i in range(0, len(local_buffer), 1)]
if len(s) == 2:
req_str = s[0]
local_buffer = s[1]
req_lst = [req_str[j:j+1] for j in range(0, len(req_lst), 1)]
local_buffer = Request(req_lst[0], *req_lst[1:])
# data = local_buffer
# local_buffer += data
return local_buffer
def response_handler(buffer):
# perform packet modifications
return buffer
def proxy_handler(client_socket, remote_host, remote_port, receive_first):
remote_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
remote_socket.connect((remote_host, remote_port))
# dum_host, dum_port = client_socket.getsockname()
if receive_first:
remote_buffer = receive_from(remote_socket)
hexdump(remote_buffer)
remote_buffer = response_handler(remote_buffer)
if len(remote_buffer):
print("[<==] Sending %d bytes to localhost." % len(remote_buffer))
client_socket.send(remote_buffer)
while True:
local_buffer = receive_from(client_socket)
if len(local_buffer):
line = "[==>] Received %d bytes from localhost." % len(local_buffer)
print(line)
hexdump(local_buffer)
local_buffer_list = []
local_buffer_list = request_handler(local_buffer, remote_host, remote_port)
remote_socket.send(bytes(local_buffer_list))
print("[==>] Sent to remote %d bytes." % len(local_buffer_list))
remote_buffer = receive_from(remote_socket)
if len(remote_buffer):
print("[<==] Received %d bytes from remote." % len(remote_buffer))
hexdump(remote_buffer)
remote_buffer = response_handler(remote_buffer)
client_socket.send(remote_buffer)
print("[<==] Sent to localhost.")
if not len(local_buffer) or not len(remote_buffer):
client_socket.close()
remote_socket.close()
print("[*] No more data. Closing connections.")
break
def print_and_accept(pkt):
print(pkt)
pkt.accept()
def server_loop(local_host, local_port, remote_host, remote_port, receive_first):
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
server.bind((local_host, local_port))
except Exception as e:
print('problem on bind: %r' % e)
print("[!!] Failed to listen on %s:%d" % (local_host, local_port))
print("[!!] Check for other listening sockets or correct permissions.")
sys.exit()
print("[*] Listening on %s:%d" % (local_host, local_port))
server.listen(30)
# nfqueue.listen(30)
while True:
client_socket, addr = server.accept()
#print out the local connection information
line = "> Received incoming connection from %s:%d" % (addr[0], addr[1])
print(line)
#start a thread to talk to the remote hosst
proxy_thread = threading.Thread(
target = proxy_handler,
args=(client_socket, remote_host, remote_port, receive_first))
proxy_thread.start()
def main():
if len(sys.argv[1:]) != 5:
print("Usage: ./proxy.py [localhost] [localport]", end = '')
print("[remotehost] [remoteport] [receive_first]")
print("Example: ./proxy.py 127.0.0.1 9000 10.12.132.1 9000 True")
sys.exit(0)
local_host = sys.argv[1]
local_port = int(sys.argv[2])
remote_host = sys.argv[3]
remote_port = int(sys.argv[4])
receive_first = sys.argv[5]
if "True" in receive_first:
receive_first = True
else:
receive_first = False
try:
server_loop(local_host, local_port, remote_host, remote_port, receive_first)
except KeyboardInterrupt:
print('')
# print("Flushing iptables")
# os.system('iptables -F')
# os.system('iptables -X')
# iptables = "iptables -I INPUT -d 192.168.0.0/24 -j NFQUEUE --queue-num 1"
# print("Adding iptable rules : ")
# print(iptables)
# os.system(iptables)
if __name__ == '__main__':
main()