- Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathipip.py
More file actions
Latest commit
107 lines (89 loc) · 3.04 KB
/
Copy pathipip.py
File metadata and controls
107 lines (89 loc) · 3.04 KB
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
#!/usr/bin/env python
# coding: utf-8
# author: frk
importstruct
fromsocketimportinet_aton
importos
_unpack_V=lambdab: struct.unpack("<L", b)
_unpack_N=lambdab: struct.unpack(">L", b)
_unpack_C=lambdab: struct.unpack("B", b)
classIP:
offset=0
index=0
binary=""
@staticmethod
defload(file):
try:
path=os.path.abspath(file)
withopen(path, "rb") asf:
IP.binary=f.read()
IP.offset, =_unpack_N(IP.binary[:4])
IP.index=IP.binary[4:IP.offset]
exceptExceptionasex:
print"cannot open file %s"%file
printex.message
exit(0)
@staticmethod
deffind(ip):
index=IP.index
offset=IP.offset
binary=IP.binary
nip=inet_aton(ip)
ipdot=ip.split('.')
ifint(ipdot[0]) <0orint(ipdot[0]) >255orlen(ipdot) !=4:
return"N/A"
tmp_offset=int(ipdot[0]) *4
start, =_unpack_V(index[tmp_offset:tmp_offset+4])
index_offset=index_length=0
max_comp_len=offset-1028
start=start*8+1024
whilestart<max_comp_len:
ifindex[start:start+4] >=nip:
index_offset, =_unpack_V(index[start+4:start+7] +chr(0).encode('utf-8'))
index_length, =_unpack_C(index[start+7])
break
start+=8
ifindex_offset==0:
return"N/A"
res_offset=offset+index_offset-1024
returnbinary[res_offset:res_offset+index_length].decode('utf-8')
classIPX:
binary=""
index=0
offset=0
@staticmethod
defload(file):
try:
path=os.path.abspath(file)
withopen(path, "rb") asf:
IPX.binary=f.read()
IPX.offset, =_unpack_N(IPX.binary[:4])
IPX.index=IPX.binary[4:IPX.offset]
exceptExceptionasex:
print"cannot open file %s"%file
printex.message
exit(0)
@staticmethod
deffind(ip):
index=IPX.index
offset=IPX.offset
binary=IPX.binary
nip=inet_aton(ip)
ipdot=ip.split('.')
ifint(ipdot[0]) <0orint(ipdot[0]) >255orlen(ipdot) !=4:
return"N/A"
tmp_offset= (int(ipdot[0]) *256+int(ipdot[1])) *4
start, =_unpack_V(index[tmp_offset:tmp_offset+4])
index_offset=index_length=-1
max_comp_len=offset-262144-4
start=start*9+262144
whilestart<max_comp_len:
ifindex[start:start+4] >=nip:
index_offset, =_unpack_V(index[start+4:start+7] +chr(0).encode('utf-8'))
index_length, =_unpack_C(index[start+8:start+9])
break
start+=9
ifindex_offset==0:
return"N/A"
res_offset=offset+index_offset-262144
returnbinary[res_offset:res_offset+index_length].decode('utf-8')