-
Notifications
You must be signed in to change notification settings - Fork 76
/
LNScan.py
200 lines (173 loc) · 6.37 KB
/
LNScan.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# usr/bin/env python
# author: wps2015
# coding:utf-8
import requests
import re
import ipaddress
import argparse
import sys
import time
import socket
import multiprocessing
from libs.result import report
from libs.bbscan import batch_scan
ip_info = {}
next_ips = []
start_time = time.time()
def parse_args():
parser = argparse.ArgumentParser(prog='LNScan',
description="A WebScanner to scan local network.\nBy wps2015(http://wps2015.org)",
formatter_class=argparse.RawTextHelpFormatter,
usage='LNScan [options]')
parser.add_argument('-v', action='version', version='%(prog)s 1.5 By wps2015')
parser.add_argument('-f', type=str, help="import the file of ip/domain list")
parser.add_argument('--ip', type=str, help='ip addresses like 192.168.1.1/24')
parser.add_argument('--port', type=str, default='', help='user single quotes to split the ports,\
like 80,21, default 8 ports')
parser.add_argument('--extend', type=str, default='', help='extend the given ips by ip masks ,such as "30"')
if len(sys.argv) == 1:
sys.argv.append('-h')
_args = parser.parse_args()
_check_args(_args)
return _args
def _check_args(_args):
if not _args.ip and not _args.f:
msg = 'Use --ip or -f to set the ip address range'
raise Exception(msg)
if _args.f and _args.ip:
raise Exception("one of -f and --ip is available")
def ip_parse(ip):
_ips = ip.strip()
ips = ipaddress.IPv4Network(u'%s' % _ips, strict=False)
return ips
def ip_revive(path):
ip_ls = []
with open(path, 'r') as f:
for ip in f.readlines():
ip_ls.append(ip.strip())
return ip_ls
class WeakScan:
def __init__(self, _host, _ports, _lock):
self._host = _host
self.lock = _lock
self.ports = _ports
self.ip_result = {}
self.next_ip = []
def http_scan(self, url):
self.ip_result[url] = {}
try:
req = requests.get("http://"+url, timeout=3)
res_title = re.search(r'<title>([\s\S]*?)</title>', req.text, re.IGNORECASE)
res_charset = re.search(r'charset=[\"]+?(\w*?)[\"]+', req.text, re.IGNORECASE)
res_h1 = re.search(r'<h1>([\s\S]*?)</h1>', req.text, re.IGNORECASE)
if res_title:
title = res_title.group(1).strip()
elif res_h1:
title = res_h1.group(1).strip()
else:
title = "Null"
try:
if res_charset:
coding = res_charset.group(1).strip().lower()
self.ip_result[url]['title'] = title.encode(coding)
else:
self.ip_result[url]['title'] = title.encode('utf-8', 'ignore')
except Exception, e:
self.ip_result[url]['title'] = url
except Exception, e:
self.ip_result[url]['title'] = ''
def port_scan(self, url, _ports):
http_port = [80, 81, 443, 8080, 8081, 8090]
if _ports:
ip_port = _ports
else:
ip_port = [80, 81, 443, 6379, 7001, 7002, 8080, 8081, 11211, 27017]
port_exist = ''
self.lock.acquire()
print "connecting ", url
self.lock.release()
for port in ip_port:
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(3)
sock.connect((url, int(port)))
if int(port) in http_port:
_host = url+':'+str(port)
if _host not in self.next_ip:
self.next_ip.append(_host)
port_exist = port_exist+str(port)+'|'
sock.close()
except Exception, e:
sock.close()
continue
if port_exist:
self.ip_result[url]['port'] = port_exist
else:
self.ip_result[url]['port'] = ''
def s_scan(self):
self.http_scan(self._host)
self.port_scan(self._host, self.ports)
return self.ip_result, self.next_ip
def scan(url, s_results, _ports, _lock):
b = WeakScan(url, _ports, _lock)
_results, _hosts = b.s_scan()
if _results:
for key in _results.keys():
_lock.acquire()
print key, _results[key]['port']
_lock.release()
s_results.put((_results, _hosts))
def ip_into_int(ip):
# (((((192 * 256) + 168) * 256) + 1) * 256) + 13
return reduce(lambda x, y: (x << 8)+y, map(int, ip.split('.')))
def is_internal_ip(ip):
if ip == '127.0.0.1':
return True
ip = ip_into_int(ip)
net_a = ip_into_int('10.255.255.255') >> 24
net_b = ip_into_int('172.31.255.255') >> 20
net_c = ip_into_int('192.168.255.255') >> 16
return ip >> 24 == net_a or ip >> 20 == net_b or ip >> 16 == net_c
def ip_extend(ini_list, extend):
print "domains are been extended..."
extend_all_ips = []
for _domain in ini_list:
ip = socket.gethostbyname(_domain)
if is_internal_ip(ip):
continue
__ip = ip+"/"+extend
extend_ips = ip_parse(__ip)
extend_all_ips += extend_ips
extend_last_ips = list(set(ini_list + extend_all_ips))
return extend_last_ips
if __name__ == '__main__':
args = parse_args()
if args.ip:
ip_lists = ip_parse(args.ip)
else:
ip_lists = ip_revive(args.f)
if args.extend:
ip_lists = ip_extend(ip_lists, args.extend)
ports = args.port
if ports:
ports = ports.split(',')
ip_Queue = multiprocessing.Manager().Queue() # start port and title scan
locks = multiprocessing.Manager().Lock()
pools = multiprocessing.Pool(20)
for _ip in ip_lists:
pools.apply_async(func=scan, args=(str(_ip), ip_Queue, ports, locks,))
pools.close()
pools.join()
while not ip_Queue.empty():
s_results, s_hosts = ip_Queue.get()
ip_info = dict(ip_info, **s_results)
next_ips += s_hosts
q_results = multiprocessing.Manager().Queue() # start BBScan
lock = multiprocessing.Manager().Lock()
pool = multiprocessing.Pool(10)
scanned_ips = []
for _host in next_ips:
pool.apply_async(func=batch_scan, args=(_host, q_results, lock, 20, 5))
pool.close()
pool.join()
report(ip_info, q_results, start_time)