-
Notifications
You must be signed in to change notification settings - Fork 63
/
bingC.py
109 lines (93 loc) · 2.63 KB
/
bingC.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
# coding:utf-8
"""
Usage:
python bingC.py IP/MASK [output_path]
Example:
python bingC.py 132.12.43.0/24 result.txt
python bingC.py 132.12.43.0/24
"""
import requests
import Queue
import threading
import re
import sys
import time
from IPy import IPy
from api import BingSearch
from config import ENABLE_API
queue = Queue.Queue()
ips = set()
lock = threading.Lock()
thread_num = 20
def scan():
global thread_num
while 1:
if queue.qsize() > 0:
ip = queue.get()
if ENABLE_API:
print "[*]api enabled!"
ans_obj = BingSearch("ip:" + ip)
for each in ans_obj['d']['results']:
ips.add(ip + ' -> ' + each['Url'].split('://')[-1].split('/')[0] + " | " + each['Title'])
else:
q = "https://www.bing.com/search?q=ip%3A" + ip
c = requests.get(q, headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0'}).content
p = re.compile(r'<cite>(.*?)</cite>')
l = re.findall(p, c)
for each in l:
domain = each.split('://')[-1].split('/')[0]
msg = ip + ' -> ' + domain
ips.add(msg)
else:
thread_num -= 1
break
def api_scan():
global thread_num
while 1:
if queue.qsize() > 0:
BingSearch(queue.get())
def setThreadDaemon(thread):
# Reference: http://stackoverflow.com/questions/190010/daemon-threads-explanation
PYVERSION = sys.version.split()[0]
if PYVERSION >= "2.6":
thread.daemon = True
else:
thread.setDaemon(True)
def runThreads():
print "Running..."
for i in range(thread_num):
t = threading.Thread(target=scan, name=str(i))
setThreadDaemon(t)
t.start()
# It can quit with Ctrl-C
while 1:
if thread_num > 0:
time.sleep(0.01)
else:
break
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.exit(__doc__)
try:
_list = IPy.IP(sys.argv[1])
except Exception, e:
sys.exit('Invalid IP/MASK, %s' % e)
for each in _list:
queue.put(str(each))
runThreads()
for each in ips:
try:
print each
except UnicodeEncodeError:
pass
print "Total: " + str(len(ips))
if len(sys.argv) is 3:
path = sys.argv[2]
try:
f = open(path, 'w')
for each in ips:
f.write(each + '\n')
f.close()
except IOError, e:
sys.exit('Invalid file path')