forked from vla-droid/IPSca-Reinforced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·326 lines (276 loc) · 11 KB
/
main.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/python3
import os
import sys
import re
from random import shuffle
from queue import Queue
from main_ui import *
from netaddr import IPNetwork
from modules.discover import *
from modules.masscan_runner import *
from modules.display import Result
from modules.logger import Logger
from modules.exporter import *
from config import *
from threading import Thread
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot, QObject
from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget, QMessageBox, QFileDialog, QVBoxLayout)
from modules.portscan import PortScan
class MyWin(QMainWindow):
def __init__(self, parent=None):
super(MyWin, self).__init__(parent)
QWidget.__init__(self, parent)
self.ui = Ui_IPSca()
self.ui.setupUi(self)
self.setWindowIcon(QIcon('ipsca.jpg'))
self.logger = Logger()
self.Scan = None
self.IoTOnly = config.IoTOnly
self.dead_counter = 0
self.successful_counter = 0
self.alive_counter = 0
self.report = {}
self.ui.scan_btn.clicked.connect(self.click_scan)
self.ui.import_btn.clicked.connect(self.file_open)
self.ui.stop_btn.clicked.connect(self.click_stop)
self.ui.shuffle_btn.clicked.connect(self.shuffle_hosts)
self.ui.iot_chbx.stateChanged.connect(self.iot_only_change)
self.ui.brute_chbx.stateChanged.connect(self.brute_enable_change)
self.ui.submit_export.clicked.connect(self.export)
self.scan = QThread()
def export(self):
format = self.ui.format_combo.currentText()
#try:
export = Exporter(format, progress.result)
export.run()
self.report = {}
# while not export.path:
self.ui.export_path.setText('File(s) was saved: ' + export.path)
#except Exception as e:
#print(str(e))
#pass
#export.prnt()
def iot_only_change(self):
if self.IoTOnly:
self.IoTOnly = False
else:
self.IoTOnly = True
def brute_enable_change(self):
if config.brute_enable:
config.brute_enable = False
else:
config.brute_enable = True
def file_open(self):
content = []
name, _ = QFileDialog.getOpenFileName(self, 'Open File', options=QFileDialog.DontUseNativeDialog)
if len(name) > 0:
file = open(name, 'r')
else:
return False
with file:
try:
text = file.read().split('\n')
except:
QMessageBox.about(self, 'Error', 'Wrong input file')
for line in text:
if ' ' in line:
content.append(line.replace(' ', ':'))
#
elif re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{2,5}$", line):
content.append(line)
elif re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,2}$", line):
content.append(line)
if len(content) == 0:
QMessageBox.about(self, 'Error', 'Empty input file')
self.ui.targetsList.setText('\n'.join(content))
def shuffle_hosts(self):
hosts = (self.ui.targetsList.toPlainText()).split('\n')
shuffle(hosts)
self.ui.targetsList.setText("\n".join(hosts))
def isRoot(self):
if os.name == 'nt':
config.OS = 'windows'
try:
os.listdir(os.sep.join([os.environ.get('SystemRoot', 'C:\\windows'), 'temp']))
except:
return False
else:
return True
else:
if 'SUDO_USER' in os.environ and os.geteuid() == 0:
return True
else:
return False
def click_scan(self):
self.clear_terminal()
self.sig = progress.Sig()
self.sig.change_value.connect(self.update_terminal)
self.sig.change_progress_str.connect(self.update_progress)
self.sig.change_brute_progress_str.connect(self.update_brute_progress)
self.sig.change_progressBar.connect(self.setProgressVal)
# self.sig.change_progressBar.connect()
self.sig.change_dead_counter.connect(self.update_dead_label)
self.sig.change_alive_counter.connect(self.update_alive_label)
self.sig.change_successful_counter.connect(self.update_successful_label)
self.sig.change_load_status.connect(self.update_load_status)
self.sig.change_actual_action.connect(self.update_actual_action)
self.scan = Scan(self.sig)
try:
self.scan.start()
except Exception as e:
print(f'{__name__} - {e}')
def click_stop(self):
progress.isScan = False
try:
if True:
progress.increment('stop')
for t in self.scan.threads:
if t.isRunning:
t.stop()
self.update_terminal(bold("[-] Canceling..."))
self.sig.send_change_progressBar(100)
self.scan.isScan = False
self.ui.action_label.setText('Finish')
except AttributeError as e:
print(f'{__name__} - {e}')
pass
except Exception as e:
print(f'{__name__} - {e}')
def update_terminal(self, val):
self.ui.terminal.append(val)
def clear_terminal(self):
progress.clear()
self.ui.terminal.setText('')
def update_progress(self, val):
# sig = self.sig
self.ui.progress.setText(val)
def update_brute_progress(self, val):
# sig = self.sig
self.ui.progress_2.setText(val)
def setProgressVal(self, val):
# print(self.Scan.change_progressBar)
self.ui.progressBar.setValue(int(val))
def update_dead_label(self, val):
self.ui.dead_label.setText(val)
def update_alive_label(self, val):
self.ui.alive_label.setText(val)
def update_successful_label(self, val):
self.ui.successful_label.setText(val)
def update_load_status(self, val):
self.ui.load_status.setText(val)
def update_actual_action(self, val):
self.ui.action_label.setText(val)
class Scan(QThread):
t = []
def __init__(self, sig, parent=None,):
super(Scan, self).__init__(parent)
self.addMassPar = ipsca.ui.addMassParTextbox.text()
self.HOSTS = (ipsca.ui.targetsList.toPlainText()).split('\n')
self.SCAN_THREADS = int(ipsca.ui.threads_portscan_edit.text())
self.sorted_hosts = list()
self.filter = str(ipsca.ui.filter_line.text())
self.ports = str(ipsca.ui.portsList.text())
self.sessions = []
self.threads = []
self.single_target = True
self.logins = default_logins_list
self.pwds = default_logins_list
self.sig = sig
self.scan_queue = Queue()
self.analyse_queue = Queue()
self.brute_queue = Queue()
self.result_queue = Queue()
progress.increment('total', value=len(self.HOSTS))
try:
if len(self.logins) * len(self.pwds) > 200:
QMessageBox.about(ipsca, 'Error', 'Too many credentials. Max.200')
progress.isScan = False
self.THREADS = int(ipsca.ui.threads_edit.text())
config.timeout = int(ipsca.ui.timeout_edit.text())
except TypeError:
QMessageBox.about(ipsca, 'Error', 'Input can only be a number')
self.isScan = False
except Exception as e:
print(f'{__name__} - {e}')
pass
def update_load(self):
while progress.isScan:
progress.update_load_status(self.sig)
sleep(1)
@pyqtSlot()
def run(self):
# ipsca.ui.terminal.
self.sig.send_signal(bold('[*] Starting...'))
for line in self.HOSTS:
if '/' in line:
self.single_target = False
try:
[self.sorted_hosts.append(ip) for ip in IPNetwork(line)]
except:
pass
else:
self.sorted_hosts.append(line)
if self.single_target:
progress.increment('discover_total', value=len(self.sorted_hosts))
# shuffle(self.sorted_hosts)
self.sig.send_signal(bold(f'[*] Loaded {len(self.sorted_hosts)} hosts and {len(self.ports.split(","))} ports.'))
if not self.single_target:
ipsca.ui.action_label.setText('Scanning...')
progress.actual_action = 'scanning'
progress.increment('scan_total', value=len(self.sorted_hosts))
if ipsca.isRoot():
self.sig.send_signal(bold('[!] You are sudo (admin) user. Starting masscan SYN-scan...'))
scan_worker = Masscan(self.sig, self.ports, self.HOSTS, self.SCAN_THREADS, self.addMassPar, self.analyse_queue)
scan_worker.setDaemon(True)
self.threads.append(scan_worker)
scan_worker.start()
else:
[self.scan_queue.put(ip) for ip in self.sorted_hosts]
mode = 'sock'
self.sig.send_signal(bold(f'[{red("ERROR")}] You are NOT sudo (admin) user. '
f'Please, restart IPSca as sudo (admin)'))
return
else:
progress.increment('action', value='discovering')
self.sig.change_actual_action.emit('Discovering')
for i in self.HOSTS:
self.analyse_queue.put(i)
# sleep(100)
display_worker = Result(self.result_queue, self.sig)
# display_worker.setDaemon(True)
display_worker.start()
tt = Thread(target=self.update_load)
tt.setDaemon(True)
tt.start()
if config.brute_enable:
for _ in range(self.THREADS):
# analyse_worker = Discover(self.analyse_queue, self.brute_queue, self.sig, self.result_queue)
analyse_worker = Discover(self)
# analyse_worker.setDaemon(True)
self.threads.append(analyse_worker)
analyse_worker.start()
for _ in range(self.THREADS):
brute_worker = Brute(self.brute_queue, self.sig, self.result_queue)
# brute_worker.setDaemon(True)
self.threads.append(brute_worker)
brute_worker.start()
self.analyse_queue.join()
self.brute_queue.join()
else:
for _ in range(self.THREADS):
analyse_worker = Discover(self)
# analyse_worker.setDaemon(True)
self.threads.append(analyse_worker)
analyse_worker.start()
self.analyse_queue.join()
self.result_queue.join()
if not progress.isScan:
ipsca.ui.progressBar.setValue(100)
self.sig.send_signal(bold('[*] All jobs are finished!'))
if __name__ == '__main__':
app = QApplication(sys.argv)
ipsca = MyWin()
# Thread(target=ipsca.run).start()
ipsca.show()
sys.exit(app.exec_())