-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathipip.py
107 lines (89 loc) · 3.04 KB
/
ipip.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
#!/usr/bin/env python
# coding: utf-8
# author: frk
import struct
from socket import inet_aton
import os
_unpack_V = lambda b: struct.unpack("<L", b)
_unpack_N = lambda b: struct.unpack(">L", b)
_unpack_C = lambda b: struct.unpack("B", b)
class IP:
offset = 0
index = 0
binary = ""
@staticmethod
def load(file):
try:
path = os.path.abspath(file)
with open(path, "rb") as f:
IP.binary = f.read()
IP.offset, = _unpack_N(IP.binary[:4])
IP.index = IP.binary[4:IP.offset]
except Exception as ex:
print "cannot open file %s" % file
print ex.message
exit(0)
@staticmethod
def find(ip):
index = IP.index
offset = IP.offset
binary = IP.binary
nip = inet_aton(ip)
ipdot = ip.split('.')
if int(ipdot[0]) < 0 or int(ipdot[0]) > 255 or len(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
while start < max_comp_len:
if index[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
if index_offset == 0:
return "N/A"
res_offset = offset + index_offset - 1024
return binary[res_offset:res_offset + index_length].decode('utf-8')
class IPX:
binary = ""
index = 0
offset = 0
@staticmethod
def load(file):
try:
path = os.path.abspath(file)
with open(path, "rb") as f:
IPX.binary = f.read()
IPX.offset, = _unpack_N(IPX.binary[:4])
IPX.index = IPX.binary[4:IPX.offset]
except Exception as ex:
print "cannot open file %s" % file
print ex.message
exit(0)
@staticmethod
def find(ip):
index = IPX.index
offset = IPX.offset
binary = IPX.binary
nip = inet_aton(ip)
ipdot = ip.split('.')
if int(ipdot[0]) < 0 or int(ipdot[0]) > 255 or len(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
while start < max_comp_len:
if index[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
if index_offset == 0:
return "N/A"
res_offset = offset + index_offset - 262144
return binary[res_offset:res_offset + index_length].decode('utf-8')