forked from killswitch-GUI/PenTesting-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multiprocessing-Threading-IP-Geo-Lookup.py
137 lines (123 loc) · 4.05 KB
/
Multiprocessing-Threading-IP-Geo-Lookup.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
#!/usr/bin/env python
import os
import sys
import threading
import multiprocessing
import Queue
import json
import requests
global IP_List
def file_handle():
try:
IP_output = open('iplist_output.txt', 'w+')
IP_output.close()
print "[*] Output File created"
except:
print "[!] Couldnt create file check permissions"
sys.exit(0)
try:
with open("iplist2.txt") as f:
IP_List = f.readlines()
f.close()
# Caculate the ammount of IP's loaded
with open("iplist2.txt") as myfile:
count = sum(1 for line in myfile)
print '[*] IP List loaded with:', count, " IP's"
except:
print "[!] Couldnt open file check file path!"
sys.exit(0)
return IP_List
def whois2(ip_queue, results_queue):
while True:
cont = True
connect_timeout = float(6.05)
read_timeout = 5
value = "alex"
#Simple whois query for location
ip = ip_queue.get()
if ip is None:
# Break out of the while loop to terminate Sub-Procs
break
try:
agent = (requests.post(url='http://www.telize.com/geoip/'+ ip.rstrip() +'', timeout=(connect_timeout, read_timeout)).json())
# ex United States
country = str(agent['country'])
# State for US
region = str(agent['region'])
# City whithin state
city = str(agent['city'])
except:
cont = False
try:
if cont:
geo_data = {'country':country, 'region':region, 'city':city}
output = str(ip.rstrip())
output += ' (' + geo_data["country"] + ':' + geo_data["region"] + ':' + geo_data["city"] + ')' + '\n'
print ("{0} ({1}:{2}:{3})").format(str(ip.strip()), geo_data["country"], geo_data["region"], geo_data["city"])
#print str(ip.rstrip()) + ' ' + ' (' + geo_data["country"] + ':' + geo_data["region"] + ':' + geo_data["city"] + ')'
results_queue.put(output)
except:
pass
return
def whois_geo_lookup(ip_queue, results_queue):
total_threads = 50
for thread in range(total_threads):
t3 = threading.Thread(target=whois2, args=(ip_queue,results_queue))
t3.daemon = True
t3.start()
t3.join()
def printer(results_queue):
while True:
# Get item an print to output file
try:
# Must set time out due to blocking,
item = results_queue.get(timeout=2)
with open('iplist_output2.txt', "a") as myfile:
myfile.write(item)
except Exception as e:
print e
break
#results_queue.task_done()
return
def main():
# Build Queue
script_queue = multiprocessing.Queue()
results_queue = multiprocessing.Queue()
#lock = multiprocessing.Lock()
#with lock:
# Set time out for join method
timeout = float(0.1)
# Define max Threads and IP list
total_proc = 8
IP_List = file_handle()
# Places all the IP's in the list into the Queue
for IP in IP_List:
script_queue.put(IP)
for i in xrange(total_proc):
script_queue.put(None)
# Generate threads for worker
procs = []
for thread in range(total_proc):
procs.append(multiprocessing.Process(target=whois_geo_lookup, args=(script_queue,results_queue,)))
for p in procs:
p.daemon = True
p.start()
# Removed for loop due to time and uneeded function, Set Float to reduce time of clossing, TESTING NEEDED!
for p in procs:
p.join(timeout)
#Launches a single thread to output results
t2 = threading.Thread(target=printer, args=(results_queue,))
t2.daemon = True
t2.start()
t2.join()
#Wait for queue to empty
print "[*] Scan Complete!"
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print 'Interrupted'
try:
sys.exit(0)
except SystemExit:
os._exit(0)