-
Notifications
You must be signed in to change notification settings - Fork 0
/
mass_effect.py
executable file
·187 lines (147 loc) · 6.85 KB
/
mass_effect.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
#!/usr/bin/env python
# coding: utf-8
#
# By Leon Johnson - twitter.com/sho_luv
#
# This program takes a file of IP addresses and scans
# them using masscan one port at a time to create files
# named after the service that was scanned.
# I'm porting overe my mass-effect.sh to python
# https://github.com/sho-luv/mass-effect
import argparse
import subprocess
import os
import sys
# ANSI color codes
RED = "\033[1;31m"
GREEN = "\033[0;32m"
YELLOW = "\033[0;33m"
BLUE = "\033[1;34m"
MAGENTA = "\033[1;35m"
CYAN = "\033[0;36m"
RESET = "\033[0m"
import os
def run_masscan(service, ports, target, exclude_file, rate):
print(f"{CYAN}Running masscan on {service} (ports: {ports}){RESET}")
# Define the iptables rules
iptables_rule = ["iptables", "-A", "INPUT", "-p", "tcp", "--dport", "60000", "-j", "DROP"]
output_dir = "masseffect_output"
os.makedirs(output_dir, exist_ok=True)
try:
if service == 'http':
print(" ".join(iptables_rule))
subprocess.run(iptables_rule)
cmd = ["masscan", "--open", "-p", ports]
if os.path.isfile(target):
cmd.extend(["-iL", target])
else:
cmd.append(target)
if service == 'http':
cmd.extend(["--source-port", "60000"])
if exclude_file:
cmd.extend(["--excludefile", exclude_file])
if service == 'http':
cmd.extend(["--banners"])
cmd.extend(["-oB", os.path.join(output_dir, service)])
if rate is not None:
cmd.extend(["--rate", str(rate)])
print(" ".join(cmd))
subprocess.run(cmd)
if service == 'http':
readscan_cmd = ["masscan", "--readscan", os.path.join(output_dir, service), "-oX", os.path.join(output_dir, f"{service}.xml")]
output = subprocess.run(readscan_cmd, capture_output=True, text=True)
iptables_rule[1] = "-D"
print(" ".join(iptables_rule))
subprocess.run(iptables_rule)
# check if only one port being scanned if so print out service.txt file
if ',' not in ports:
readscan_cmd = ["masscan", "--readscan", os.path.join(output_dir, service), os.path.join(output_dir, f"{service}.txt")]
output = subprocess.run(readscan_cmd, capture_output=True, text=True)
if output.stdout.strip():
ip_addresses = [line.split(' ')[5] for line in output.stdout.split('\n') if line.strip()]
# Now write ip_addresses to a file
with open(os.path.join(output_dir, f"{service}_ips.txt"), 'w') as f:
for ip in ip_addresses:
f.write(f"{ip}\n")
# get all files in the directory
for file_name in os.listdir(output_dir):
output_file = os.path.join(output_dir, file_name)
if os.path.exists(output_file) and os.path.getsize(output_file) == 0:
os.remove(output_file)
except FileNotFoundError:
print(f"{RED}masscan not found. Please install masscan and try again.{RESET}")
sys.exit(1)
if service == 'http' and output.stdout.strip():
with open(os.path.join(output_dir, f"{service}.txt"), 'w') as f:
for line in output.stdout.split('\n'):
if line.strip():
f.write(line.split(' ')[5] + '\n')
banner = """
███ ███ █████ ███████ ███████ ███████ ███████ ███████ ███████ ██████ ████████
████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ████ ██ ███████ ███████ ███████ █████ █████ █████ █████ █████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ███████ ███████ ███████ ██ ██ ███████ ██████ ██
$BWhite Port Scanner For Things I Like To Hack$Off | $BYellow@sho_luv$Off
"""
from termcolor import colored
def print_banner():
print(f"{YELLOW}##############################################")
print(f"# #")
print(f"# Masscan Automation Script #")
print(f"# #")
print(f"##############################################{RESET}")
def main():
print_banner()
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--target', required=True,
help='IP address or file of IP addresses to be scanned')
parser.add_argument('-r', '--rate', default='100',
help='Rate to scan (default is 100 packets per second)')
parser.add_argument('-e', '--exclude', default='', help='File of IPs to be excluded from scan')
args = parser.parse_args()
target = args.target
services = {
'smb': '445',
'http': '80,443,8080,8081',
'snmp': 'U:161',
'ike': 'U:500',
'ipmi': 'U:623',
'ftp': '21',
'ssh': '22',
'nfs': '111',
'rlogin': '513',
'ghost_cat': '8009',
'java-rmi': '1099',
'mssql': '1433',
'oracle': '1521',
'jdwp': '2010,8000,9999',
'rdp': '3389',
'erlang': '4369',
'siet': '4786',
'vnc': '5900',
'couchdb': '5984',
'winrm': '5985,5986',
'x11': '6000-6005',
'redis': '6379',
'weblogic': '7001',
'manage_engine':'8383,8400',
'intel-amt':'16992,16993,5900,623,664',
'iscsi':'860,3205,3260',
'hi-lo':'17988',
'rabbitmq':'25672',
'mongodb':'27017',
'ldap':'389',
'ldaps':'636',
'lantronix':'9999,30718',
'sap':'8000,50000,50013',
'iSCSI':'3260',
'track-it':'9010'
# Add more services here
}
exclude = f"--excludefile {args.exclude}" if args.exclude else ""
for service, ports in services.items():
run_masscan(service, ports, target, exclude, args.rate)
if __name__ == "__main__":
main()