-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunnat64.py
executable file
·331 lines (285 loc) · 8.8 KB
/
tunnat64.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
#!/usr/bin/env python
import os,sys,struct,time,random
from fcntl import ioctl
from subprocess import call
from select import select
from construct import *
from construct.protocols.layer3.ipv4 import ipv4_header
from construct.protocols.layer3.icmpv4 import icmp_header
from construct.protocols.layer3.ipv6 import ipv6_header,Ipv6Address,Ipv6AddressAdapter
from construct.protocols.layer3.icmpv6 import icmpv6_header
from construct.protocols.layer4.tcp import tcp_header
tun_header = Struct("tun_header",
UBInt16("flags"),
Enum(UBInt16("type"),
IPv4 = 0x0800,
ARP = 0x0806,
RARP = 0x8035,
X25 = 0x0805,
IPX = 0x8137,
IPv6 = 0x86DD,
_default_ = Pass,
),
)
layer4_tcp = Struct("layer4_tcp",
Rename("header", tcp_header),
HexDumpAdapter(
Field("next", lambda ctx:
ctx["_"]["header"].payload_length - ctx["header"].header_length
)
),
)
layer3_payload = Switch("next", lambda ctx: ctx["header"].protocol,
{
"ICMP" : icmp_header,
"ICMPv6" : icmpv6_header,
"TCP" : layer4_tcp,
},
default = Pass
)
layer3_ipv4 = Struct("layer3_ipv4",
Rename("header", ipv4_header),
layer3_payload,
)
layer3_ipv6 = Struct("layer3_ipv6",
Rename("header", ipv6_header),
layer3_payload,
)
layer2_tun = Struct("layer2_tun",
Rename("header", tun_header),
Switch("next", lambda ctx: ctx["header"].type,
{
"IPv4" : layer3_ipv4,
"IPv6" : layer3_ipv6,
},
default = Pass,
)
)
TUNSETIFF = 0x400454ca
IFF_TUN = 0x0001
IFF_TAP = 0x0002
TUN_NOCHECKSUM = 0x0020
class NoMapping(Exception):
pass
class BindingInformationBase:
def __init__(self,src_ips):
self.src_ip_pool=src_ips
self.bibs={
'TCP':{},
'UDP':{},
'ICMP':{}
}
def get_free_src(self,proto):
while True:
src_ip=random.choice(self.src_ip_pool)
src_port=random.randint(1024,65535)
if not (src_ip,src_port) in self.bibs[proto]:
return (src_ip,src_port)
def get_map(self,proto,src_ip,src_port,no_create=False):
t=time.time()
if (src_ip,src_port) in self.bibs[proto]:
nat_ip=self.bibs[proto][(src_ip,src_port)][0]
nat_port=self.bibs[proto][(src_ip,src_port)][1]
else:
if no_create:
raise NoMapping()
(nat_ip,nat_port)=self.get_free_src(proto)
self.bibs[proto][(src_ip,src_port)]=(nat_ip,nat_port,t)
self.bibs[proto][(nat_ip,nat_port)]=(src_ip,src_port,t)
return (nat_ip,nat_port)
class Skel:
pass
def build_h4_from_h6(h6):
# RFC 6145 5.1
x=Skel()
x.version=4
x.header_length=20
x.tos=h6.traffic_class
x.total_length=h6.payload_length+x.header_length
x.payload_length=h6.payload_length
x.identification=0
x.flags=Skel()
x.flags.dont_fragment=1
x.flags.more_fragments=0
x.frame_offset=0
x.ttl=h6.hoplimit
x.protocol=h6.protocol # FIXME
x.checksum=0 # FIXME
x.source="0.0.0.0" # FIXME
dst="".join(part.decode("hex") for part in h6.destination.split(":"))[-4:]
x.destination=".".join(str(ord(b)) for b in dst)
x.options=''
return x # FIXME
def build_h6_from_h4(h4,v6prefix):
# RFC 6145 4.1
x=Skel()
x.version=6
x.traffic_class=h4.tos
x.flow_label=0
x.payload_length=h4.payload_length
x.protocol=h4.protocol # FIXME
x.hoplimit=h4.ttl
x.ttl=h4.ttl
a6=[("%x" % int(t)) for t in h4.source.split(".")]
#x.source=v6prefix+a6[0]+a6[1]+":"+a6[2]+a6[3] - FIXME - tak jest poprawnie, ale jest zly adapter
x.source=v6prefix+":".join(a6)
return x
def fix_checksum(data,start,stop,cs_loc):
checksum=0
csdata=data
if (len(csdata) & 1) == 1:
csdata+=chr(0)
for i in range(start,stop,2):
checksum += (ord(csdata[i]) << 8)+ord(csdata[i+1])
while (checksum>>16) > 0 :
checksum = (checksum & 0xffff) + (checksum >> 16);
checksum = (~checksum & 0xffff);
return data[:cs_loc]+chr(checksum >> 8)+chr(checksum & 0xff)+data[(cs_loc+2):]
def fix_ipv4_checksum(data,ip_offset=4):
return fix_checksum(data,start=ip_offset,stop=ip_offset+20,cs_loc=ip_offset+10)
def fix_tcpv4_checksum(data,ip_offset=4,ip_hl=20):
tl=len(data[ip_offset+ip_hl:])
tcpph=data[ip_offset+12:ip_offset+20]+chr(0)+data[ip_offset+9]+chr( (tl>>8) & 0xff)+chr(tl & 0xff)+data[ip_offset+ip_hl:]
# print "tcp cs=%x" % ( (ord(tcpph[28])<<8) + ord(tcpph[29]) )
tcpph=fix_checksum(tcpph,0,len(tcpph),28)
# print "tcp cs=%x" % ( (ord(tcpph[28])<<8) + ord(tcpph[29]) )
return data[:ip_offset+ip_hl+16]+tcpph[28]+tcpph[29]+data[ip_offset+ip_hl+18:]
def fix_tcpv6_checksum(data,ip_offset=4,tcp_offset=40):
tl=len(data[ip_offset+tcp_offset:])
# print "tl=%d" % tl
tcpph=data[ip_offset+8:ip_offset+40]+ \
chr( (tl>>24) & 0xff)+chr( (tl>>16) & 0xff)+chr( (tl>>8) & 0xff)+chr(tl & 0xff)+ \
chr(0)+chr(0)+chr(0)+chr(6)+ \
data[ip_offset+tcp_offset:]
# print "tcp cs=%x" % ( (ord(tcpph[56])<<8) + ord(tcpph[57]) )
tcpph=fix_checksum(tcpph,0,len(tcpph),56)
# print "tcp cs=%x" % ( (ord(tcpph[56])<<8) + ord(tcpph[57]) )
return data[:ip_offset+tcp_offset+16]+tcpph[56]+tcpph[57]+data[ip_offset+tcp_offset+18:]
def print_packet(packet,prefix="\t"):
pt=packet.header.type
proto=packet.next.header.protocol
src='['+packet.next.header.source+']'
dst='['+packet.next.header.destination+']'
add_info=''
if pt == 'IPv4':
add_info+=' TTL=%d' % packet.next.header.ttl
add_info+=' IPCS=%x' % packet.next.header.checksum
elif pt == 'IPv6':
add_info+=' HL=%d' % packet.next.header.hoplimit
if proto == 'TCP':
src+=':%d' % packet.next.next.header.source
dst+=':%d' % packet.next.next.header.destination
add_info+=' TCPCS=%x' % packet.next.next.header.checksum
print "%s[%s] %s -> %s%s" % (prefix, packet.header.type, src, dst, add_info)
f4 = os.open("/dev/net/tun", os.O_RDWR)
ifs = ioctl(f4, TUNSETIFF, struct.pack("16sH", "nat64_v4_%d", IFF_TUN|TUN_NOCHECKSUM))
if4name = ifs[:16].strip("\x00")
f6 = os.open("/dev/net/tun", os.O_RDWR)
ifs = ioctl(f6, TUNSETIFF, struct.pack("16sH", "nat64_v6_%d", IFF_TUN|TUN_NOCHECKSUM))
if6name = ifs[:16].strip("\x00")
src_ips=[ "173.255.206.201" ]
print "Uzywam interfejsu v4: %s, v6: %s." % (if4name, if6name)
call("ip link set dev %s up" % if4name, shell=True)
call("ip link set dev %s up" % if6name, shell=True)
for src_ip in src_ips:
call("ip route add %s dev %s" % (src_ip,if4name), shell=True)
call("ip route add 2001:470:1f0f:4dc:0:1::/96 dev %s" % if6name, shell=True)
bib=BindingInformationBase(src_ips)
while (1):
r = select([f4,f6],[],[],1)[0]
if f4 in r:
print "New packet on v4 interface"
buf4=os.read(f4,1600)
p4=layer2_tun.parse(buf4)
print_packet(p4,"\t ORIG ")
if p4.header.type != 'IPv4':
print "\t[IPv4] DROP: Wrong protocol (%s)" % p4.header.type
continue
p4.next.header.ttl-=1
if p4.next.header.ttl == 0:
print "\t[IPv4] DROP: hoplimit==0"
continue
#print p4
src_ip=p4.next.header.source
dst_ip=p4.next.header.destination
proto=p4.next.header.protocol
src_port=0
dst_port=0
if proto == 'TCP':
src_port=p4.next.next.header.source
dst_port=p4.next.next.header.destination
else:
print "\t[IPv4] DROP: Unknow transport protocol (%s)" % proto
continue
#print bib.bibs
#print "src_ip=%s, dst_ip=%s, proto=%s, src_port=%d, dst_port=%d" % (src_ip,dst_ip,proto,src_port,dst_port)
try:
(nat_ip,nat_port)=bib.get_map(proto,dst_ip,dst_port,True)
except NoMapping as e:
print "\t[IPv4] DROP: No mapping in BIB"
continue
#print "nat_ip=%s, nat_port=%d" % (nat_ip,nat_port)
h6=build_h6_from_h4(p4.next.header,"20:01:04:70:1f:0f:04:dc:00:00:00:01:")
p6=Skel()
p6.header=Skel()
p6.header.flags=0
p6.header.type='IPv6'
p6.next=Skel()
p6.next.header=h6
p6.next.header.destination=nat_ip
p6.next.next=p4.next.next
if proto == 'TCP':
p4.next.next.header.destination=nat_port
p4.next.next.header.checksum=0
tmp=layer2_tun.build(p6)
if proto == 'TCP':
tmp=fix_tcpv6_checksum(tmp)
os.write(f6,tmp)
tmp6=layer2_tun.parse(tmp)
#print tmp6
print_packet(tmp6,"\t NAT ")
if f6 in r:
print "New packet on v6 interface"
buf6=os.read(f6,1600)
p6=layer2_tun.parse(buf6)
print_packet(p6,"\t ORIG ")
if p6.header.type != 'IPv6':
print "\t[IPv6] DROP: Wrong protocol (%s)" % p6.header.type
continue
p6.next.header.hoplimit-=1
if p6.next.header.hoplimit == 0:
print "\t[IPv6] DROP: hoplimit==0"
continue
#print p6
src_ip=p6.next.header.source
proto=p6.next.header.protocol
src_port=0
if proto == 'TCP':
src_port=p6.next.next.header.source
else:
print "\t[IPv6] DROP: Unknow transport protocol (%s)" % proto
continue
(nat_ip,nat_port)=bib.get_map(proto,src_ip,src_port)
#print nat_ip,nat_port
#print src_port
h4=build_h4_from_h6(p6.next.header)
p4=Skel()
p4.header=Skel()
p4.header.flags=0
p4.header.type='IPv4'
p4.next=Skel()
p4.next.header=h4
p4.next.header.source=nat_ip
p4.next.next=p6.next.next
if proto == 'TCP':
p4.next.next.header.source=nat_port
p4.next.next.header.checksum=0
tmp=layer2_tun.build(p4)
tmp=fix_ipv4_checksum(tmp)
if proto == 'TCP':
tmp=fix_tcpv4_checksum(tmp)
os.write(f4,tmp)
tmp4=layer2_tun.parse(tmp)
#print tmp4
print_packet(tmp4,"\t NAT ")
#print bib.bibs