-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturl_parse.py
182 lines (158 loc) · 6.82 KB
/
turl_parse.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
# -*- coding: utf-8 -*-
import os
import json
work_dir = "./result/"
p_module = {}
format = False
def json_format(filepath):
try:
f = fopen(filepath, "r", "utf-8")
j_file = json.load(f)
dumps_str = json.dumps(j_file, sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ': '))
f.close()
f = fopen(filepath, "w", "utf-8")
f.write(dumps_str)
f.close()
except Exception as err:
print("json format error, %s" % err)
def ip_api(filepath):
f = fopen(filepath, "r", "utf-8")
jfile = json.load(f)
if jfile.get("status") == "success":
ip = jfile.get("query")
country = jfile.get("country")
region = jfile.get("regionName")
city = jfile.get("city")
lat = jfile.get("lat")
lon = jfile.get("lon")
isp = jfile.get("isp")
as_org = jfile.get("org")
asn = jfile.get("as")
if len(asn) > 2 and (asn[0:2] == "AS" or asn[0:2] == "as"):
space_index = asn.index(" ")
asn = asn[0:space_index]
# ip, country, region, city, asn, as_org, isp
print("ip, country, region, city, asn, as_org, isp")
print("%s , %s, %s, %s, %s, %s, %s" % (ip , country, region, city, asn, as_org, isp))
f.close()
def fofa(filepath):
f = fopen(filepath, "r", "utf-8")
jfile = json.load(f)
if jfile.get("error"):
print(jfile.get("errmsg"))
return
mode = jfile.get("mode")
size = jfile.get("size")
page = jfile.get("page")
if jfile.get("results") == None:
jfile["results"] = []
print("get results: %d, size: %s, page: %s, mode: %s" % (len(jfile.get("results")), size, page, mode))
print("ip:port(protocol), country, province, city, as_number, as_organization, isp")
for obj in jfile.get("results"):
ip = obj[0]
port = obj[1]
country = obj[2].replace("'", "\"")
host = obj[3]
title = obj[4].replace("'", "\"")
domain = obj[5].replace("'", "\"")
province = obj[6].replace("'", "\"")
country_name = obj[7].replace("'", "\"")
header = obj[8].replace("'", "\"")
server = obj[9].replace("'", "\"")
protocol = obj[10].replace("'", "\"")
banner = obj[11].replace("'", "\"")
as_number = "AS"+ str(obj[12])
as_organization = obj[13].replace("'", "\"")
latitude = obj[14]
longitude = obj[15]
isp = obj[16].replace("'", "\"")
city = obj[17].replace("'", "\"")
m_type = "fofa"
m_status = "0"
#ip:port(protocol), country, province, city, as_number, as_organization, isp
print("%s:%s(%s), %s, %s, %s, %s, %s, %s" % (ip,port, protocol, country, province, city, as_number, as_organization, isp))
f.close()
def censys(filepath):
f = fopen(filepath, "r", "utf-8")
jfile = json.load(f)
if jfile.get("status") != "ok":
print(jfile.get("error"))
return
print(jfile.get("metadata"))
print("ip:port/protocols, country, province, city, autonomous_system_asn, autonomous_system_name, autonomous_system_routed_prefix")
for obj in jfile.get("results"):
ip = obj.get("ip")
updated_at = obj.get("updated_at")
city = obj.get("location.city")
country = obj.get("location.country")
province = obj.get("location.province")
protocols = obj.get("protocols")
longitude = obj.get("location.longitude")
latitude = obj.get("location.latitude")
autonomous_system_routed_prefix = obj.get("autonomous_system.routed_prefix")
autonomous_system_asn = "AS%d" % obj.get("autonomous_system.asn")
autonomous_system_name = obj.get("autonomous_system.name")
if len(protocols)<=0:
port = "0"
proto = ""
host = "%s:%s" % (ip, port)
print("%s:%s/%s, %s, %s, %s, %s, %s, %s" % (ip, port, proto, country, province, city, autonomous_system_asn, autonomous_system_name, autonomous_system_routed_prefix))
else:
for i in protocols:
port = i.split("/")[0]
proto = i.split("/")[1]
host = "%s:%s" % (ip, port)
print("%s:%s/%s, %s, %s, %s, %s, %s, %s" % (ip, port, proto, country, province, city, autonomous_system_asn, autonomous_system_name, autonomous_system_routed_prefix))
#ip:port/protocols, country, province, city, autonomous_system_asn, autonomous_system_name, autonomous_system_routed_prefix
#print("%s:%s/%s, %s, %s, %s, %s, %s, %s" % (ip, port, protocols, country, province, city, autonomous_system_asn, autonomous_system_name, autonomous_system_routed_prefix))
f.close()
def shodan(filepath):
f = fopen(filepath, "r", "utf-8")
jfile = json.load(f)
if int(jfile.get("total")) <= 0:
print("shodan search not found")
return
for obj in jfile.get("matches"):
ip = obj.get("ip_str")
port = obj.get("port")
host = "%s:%s" % (ip, port)
asn = obj.get("asn")
protocol = obj.get("product")
isp = obj.get("isp")
asn_org = obj.get("org")
utime = obj.get("timestamp")
longitude = obj.get("location.longitude")
latitude = obj.get("location.latitude")
city = obj.get("location.city")
country = obj.get("location.country_name")
region = obj.get("location.region_code")
#ip, port, protocol, country, region, city, asn, asn_org, isp
print("ip, port, protocol, country, region, city, asn, asn_org, isp")
print("%s, %s, %s, %s, %s, %s, %s, %s, %s" % (ip, port, protocol, country, region, city, asn, asn_org, isp))
f.close()
def init():
p_module["ipinfo-ip-api"] = ip_api
p_module["censys"] = censys
p_module["fofa"] = fofa
p_module["shodan-search"] = shodan
def fopen(filepath, mode, encoding):
import sys
if sys.version_info < (3, 0):
return open(filepath, mode)
else:
return open(filepath, mode, encoding=encoding)
if __name__ == "__main__":
init()
for fpathe, dirs, fs in os.walk(work_dir):
for finfo in fs:
path = os.path.join(fpathe,finfo)
if format:
json_format(path)
for name in p_module:
if name in path:
try:
print("%s%s%s" % ('*' * 20, path, '*' * 20))
pfunc = p_module[name]
pfunc(path)
except Exception as err:
print("execute func error, %s" % err)