-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathport-crawler.py
executable file
·77 lines (66 loc) · 2.24 KB
/
port-crawler.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
#!/usr/bin/env python3
import os
import sys
import argparse
import time
import subprocess
import shutil
import requests
from elasticsearch import Elasticsearch
parser = argparse.ArgumentParser(description="Port crawling script")
parser.add_argument('-r', '--rate', help='masscan rate')
parser.add_argument('-c', '--config', help='masscan config file')
parser.add_argument('--ip', help='IP(s) to scan', nargs='+')
parser.add_argument('-p', '--ports', help='Port(s) to scan', nargs='+')
parser.add_argument('-i', '--index_prefix', help='Prefix of index', default='portscan')
parser.add_argument('--test', help='do not upload for testing', action="store_true")
args = parser.parse_args()
def es_uploader(date, complete_file, es, index_prefix):
i=0
docs ={}
index_name = index_prefix + date
with open(complete_file) as f:
for line in f:
line = line.rstrip('\n')
if line == '[' or line == ',' or line == ']':
pass
else:
es.index(index=index_name, doc_type='scan', id=i, body=line)
i=i+1
f.closed
def scanner(ip, ports, masscan_rate, masscan_config):
if masscan_config:
subprocess.run(['masscan', '-c', masscan_config, '-oJ', complete_file])
else:
if not ip:
print('"--ip" argument required')
sys.exit(1)
if not ports:
print('"--ports" argument required')
sys.exit(1)
ip = ','.join(ip)
ports = ','.join(ports)
subprocess.run(['masscan', str(ip),'-p', str(ports), '--rate', str(masscan_rate), '--banners', '-oJ', complete_file])
try:
os.remove(date)
except FileNotFoundError:
pass
def main():
global date
date = time.strftime("%Y-%m-%d_%H:%M")
ext = '.json'
global complete_file
complete_file = date + ext
elasticsearch_host = Elasticsearch()
masscan_rate = args.rate
masscan_config = args.config
scanner(args.ip, args.ports, masscan_rate, masscan_config)
if not args.test:
es_uploader(date, complete_file, elasticsearch_host, args.index_prefix)
if not args.test:
try:
os.remove(complete_file)
except FileNotFoundError:
pass
if __name__ == '__main__':
main()