-
Notifications
You must be signed in to change notification settings - Fork 1
/
simpleps.py
285 lines (257 loc) · 9.24 KB
/
simpleps.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# -*- coding: utf-8 -*-
'''
---===> SimplePS (Simple PortScanner) <===---
____________________________________________________________________________________________________
AUTHOR: DrPython3 @ GitHub.com
RELEASE 2
DATE: 2021 / 02 / 28
INFO: A basic tool for multi threaded port scanning. It scans an IP or IP range for most
common services and saves active / available ones to textfiles. Hostnames can be scanned,
too. Therefor, SimplePS will convert the given hostname to an IP as long as it can be
resolved.
No extra software like Nmap is neccessary. Users just need Python 3.8+ and some time.
____________________________________________________________________________________________________
Honor the work, if you like this tool or in case you use it on a regular basis! Donate, plase!
Every donation supports future projects and the maintenance (updates, improvements etc.) of those
already available.
BTC: 1GfmbWQHfhA7mXwGuuM9iF467eVUnwHDbx
LTC: LdeBaZVUGeyYoFjmo8FMDgT66tyv4yhYJ9
____________________________________________________________________________________________________
'''
# ---[needed modules]---:
import sys
try:
import os
import colorama
import threading
import socket
import ipaddress
from queue import Queue
from time import sleep
except:
sys.exit('Error importing needed Python modules!\n' + '#'*38 + '\n'
+ 'Just the requirements.txt for installing those dependencies and\n'
+ 'start SimplePS again.')
# init colorama for further use
colorama.init(autoreset=True)
# ---[needed variables +++ dictionaries +++ etc]---:
TargetsIps = []
TargetsLeft = int(0)
TargetsScanned = int(0)
ServicesFound = int(0)
targets_type = int(0)
scan_threads = int(1)
scan_timeout = float(5.00)
scan_locker = threading.Lock()
scan_queue = Queue()
results_all = str('found.txt')
# following ports will be scanned for the named services ...
services_ports = {
21:'ftp',
22:'ssh',
53:'dns',
3389:'rdp',
80:'http',
443:'https',
25:'smtp',
143:'imap',
23:'telnet',
445:'smb',
161:'snmp',
162:'snmp',
389:'ldap',
636:'ldaps',
137:'netbios',
139:'netbios',
427:'slp',
548:'afp',
110:'pop3'
}
# ---[logo]---:
logo_main = '''
,---. ,--. ,--. ,------. ,---.
' .-' `--',--,--,--. ,---. | | ,---. | .--. '' .-'
`. `-. ,--.| || .-. || || .-. :| '--' |`. `-.
.-' || || | | || '-' '| |\ --.| | --' .-' |
`-----' `--'`--`--`--'| |-' `--' `----'`--' `-----'
`--'
<<=====================================================>>
SimpleP(ort)S(canner) by DrPython3 @ GitHub.com
#[OPTIONS]: (1) Scan single IP
(2) Scan hostname
(3) Scan IP-range
------------------
(9) EXIT
'''
# ---[functions]---:
def blank():
'''
Blank screen whenever needed.
:return: None
'''
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
return None
def writer(filename, content):
'''
Saves any given content to a certain file.
:param str filename: output-file
:param str content: content to save
:return: True, False
'''
try:
with open(str(filename), 'a+') as output_file:
output_file.seek(0)
empty = output_file.read(100)
if len(empty) > 0:
output_file.write('\n')
else:
pass
output_file.write(str(content))
return True
except:
return False
def portscanner(targetip):
'''
Establishes a connection to a given target using Sockets.
:param str targetip: ip to scan
:return: True, False
'''
global TargetsScanned
global ServicesFound
global TargetsLeft
scan_ip = str('')
# prepare socket ...
scanner = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scanner.settimeout(scan_timeout)
# get next ip ...
scan_ip = str(socket.gethostbyname(targetip))
try:
with scan_locker:
print(colorama.Fore.YELLOW + f'Starting scan for ip: {scan_ip}')
# scan given target ip for all ports in dictionay ...
for x in services_ports:
scan_port = int(x)
scan_service = str(services_ports[x])
try:
with scan_locker:
print(colorama.Fore.WHITE + f'Scanning {scan_ip}:{str(scan_port)} for {scan_service}')
scan = scanner.connect((scan_ip,scan_port))
results_service = str(f'found_{scan_service}.txt')
result_output = str(f'{scan_service}, {scan_ip}:{str(scan_port)}')
with scan_locker:
ServicesFound += 1
print(colorama.Fore.GREEN + f'Found service: {result_output}')
save_found = writer(results_all, result_output)
if targets_type == 3:
save_service = writer(results_service, result_output)
else:
pass
scanner.close()
except:
continue
TargetsScanned += 1
TargetsLeft -= 1
except:
with scan_locker:
print(colorama.Fore.RED + f'Scanning ip: {str(attack_ip)} failed')
return None
def scanner_threads():
'''
Pulls IPs from queue and processes the portscan.
:return: None
'''
while True:
next_target = str(scan_queue.get())
portscanner(next_target)
scan_queue.task_done()
return None
def main():
'''
Main function for user interaction and starting portscans.
:return: None
'''
global targets_type
global TargetsIps
global TargetsLeft
global scan_threads
global scan_timeout
blank()
print(colorama.Fore.CYAN + logo_main + '\n\n')
targets_type = int(input(colorama.Fore.WHITE + 'Choose an option, please: '))
blank()
if targets_type == 9:
sys.exit(colorama.Fore.YELLOW + 'Your choice: EXIT\n' + '#'*17 + '\nBye bye && see you again, mate!\n\n')
elif targets_type in (1, 2, 3):
print(colorama.Fore.CYAN + '\n\n--[S*i*m*p*l*e*P*S__S*t*a*r*t*U*p]--\n' + '#'*36 + '\n\n\n')
else:
return None
# get target(s) ...
if targets_type == 1:
print(colorama.Fore.YELLOW + 'Enter target IP, e.g. 127.0.0.1:\n')
elif targets_type == 2:
print(colorama.Fore.YELLOW + 'Enter target hostname, e.g. mydomain.com:\n')
elif targets_type == 3:
print(colorama.Fore.YELLOW + 'Enter target IP-range in CDIR format, e.g. 127.0.0.0/24 (has to end on 0!):\n')
new_target = str(input())
if targets_type == 1 or targets_type == 2:
TargetsIps.append(new_target)
else:
print(colorama.Fore.YELLOW + '\n\n\n ... adding targets, please wait!\n')
try:
TargetsIps = [str(newip) for newip in ipaddress.IPv4Network(new_target)]
print(colorama.Fore.GREEN + str(len(TargetsIps)) + ' have been added!')
print(colorama.Fore.YELLOW + '\n\n\nEnter amount of threads to use, e.g. 10:\n')
try:
scan_threads = int(input())
except:
scan_threads = int(1)
print(colorama.Fore.YELLOW + f'\n\nScanner threads set to: {str(scan_threads)}')
except:
blank()
print(colorama.Fore.RED + '\n\nSorry! No targets added ...\nPress [ENTER] to return to main menu!')
wait_for_user = input()
return None
print(colorama.Fore.YELLOW + '\n\n\nEnter defaulf timeout for portscan in seconds, e.g. 5.0:\n')
try:
scan_timeout = float(input())
except:
scan_timeout = float(5.0)
print(colorama.Fore.YELLOW + f'\n\nDefault timeout set to: {str(scan_timeout)} seconds')
print(colorama.Fore.YELLOW + '\n\n\nPress [ENTER] to start!')
wait_for_user = input()
TargetsLeft = int(len(TargetsIps))
# start threads ...
for _ in range(scan_threads):
psthread = threading.Thread(target=scanner_threads)
psthread.daemon = True
psthread.start()
# fill queue ...
for targetip in TargetsIps:
scan_queue.put(targetip)
# provide stats in window title ...
while TargetsLeft > 0:
try:
sleep(0.5)
wintitle = f'LEFT TO SCAN: {str(TargetsLeft)} | SCANNED: {str(TargetsScanned)} | FOUND: {str(ServicesFound)}'
sys.stdout.write('\33]0;' + str(wintitle) + '\a')
sys.stdout.flush()
except:
pass
# wait for fall threads to finish ...
scan_queue.join()
blank()
# print results ...
print(colorama.Fore.YELLOW + f'Scanned: {str(TargetsScanned)}')
if ServicesFound > 0:
print(colorama.Fore.GREEN + f'Active services found: {str(ServicesFound)}')
else:
print(colorama.Fore.RED + 'No active service found.')
print(colorama.Fore.YELLOW + '\n\n\nPress [ENTER] to return to main menu!\n')
wait_for_user = input()
return None
# ---[the magic starts here]---:
while True:
main()