Skip to content

Commit 6bf3439

Browse files
committed
refactor(scanners): replace host_list with input_file in scanner classes for generator based file handling and task generation for hosts
1 parent d16437d commit 6bf3439

8 files changed

Lines changed: 77 additions & 64 deletions

File tree

bugscanx/modules/scanners_pro/concurrency/multithread.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ def __init__(self, threads=50):
2121

2222
def _add_task(self, task):
2323
self._queue.put(task)
24-
if not hasattr(self, '_total_preset'):
25-
self._total += 1
2624

2725
def set_total(self, total):
2826
self._total = total
29-
self._total_preset = True
3027

3128
def start(self):
3229
print()

bugscanx/modules/scanners_pro/host_scanner_pro.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@
44
from bugscanx.utils.common import get_input, get_confirm, is_cidr
55

66

7-
def read_hosts(filename=None, cidr=None):
8-
if filename:
9-
with open(filename) as file:
10-
return [line.strip() for line in file]
11-
elif cidr:
12-
return []
13-
return []
14-
15-
167
def get_cidr_ranges_from_input(cidr_input):
178
return [c.strip() for c in cidr_input.split(',')]
189

@@ -65,7 +56,7 @@ def get_input_direct(no302=False):
6556

6657
if cidr:
6758
cidr_ranges = get_cidr_ranges_from_input(cidr)
68-
from .scanners.direct import CIDRDirectScanner
59+
from .scanners.direct import CIDRDirectScanner
6960
scanner = CIDRDirectScanner(
7061
method_list=method_list,
7162
cidr_ranges=cidr_ranges,
@@ -76,7 +67,7 @@ def get_input_direct(no302=False):
7667
from .scanners.direct import HostDirectScanner
7768
scanner = HostDirectScanner(
7869
method_list=method_list,
79-
host_list=read_hosts(filename, cidr),
70+
input_file=filename,
8071
port_list=port_list,
8172
no302=no302
8273
)
@@ -112,7 +103,7 @@ def get_input_proxy():
112103
else:
113104
from .scanners.proxy_check import HostProxyScanner
114105
scanner = HostProxyScanner(
115-
host_list=read_hosts(filename, cidr),
106+
input_file=filename,
116107
port_list=port_list,
117108
target=target_url,
118109
payload=payload,
@@ -161,7 +152,7 @@ def get_input_proxy2():
161152
from .scanners.proxy_request import HostProxy2Scanner
162153
scanner = HostProxy2Scanner(
163154
method_list=method_list,
164-
host_list=read_hosts(filename, cidr),
155+
input_file=filename,
165156
port_list=port_list,
166157
).set_proxy(proxy, proxy_username, proxy_password)
167158

@@ -184,14 +175,16 @@ def get_input_ssl():
184175
else:
185176
from .scanners.ssl import HostSSLScanner
186177
scanner = HostSSLScanner(
187-
host_list=read_hosts(filename, cidr),
178+
input_file=filename,
188179
)
189180

190181
return scanner, output, threads
191182

192183

193184
def get_input_ping():
194185
filename, cidr = get_host_input()
186+
if filename is None and cidr is None:
187+
return None, None, None
195188

196189
port_list = get_input("Enter port(s)", "number", default="443").split(',')
197190
output, threads = get_common_inputs(filename or cidr)
@@ -206,7 +199,7 @@ def get_input_ping():
206199
else:
207200
from .scanners.ping import HostPingScanner
208201
scanner = HostPingScanner(
209-
host_list=read_hosts(filename, cidr),
202+
input_file=filename,
210203
port_list=port_list,
211204
)
212205

bugscanx/modules/scanners_pro/scanners/base.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,6 @@ def get_url(self, host, port):
1616
protocol = 'https' if port == '443' else 'http'
1717
return f'{protocol}://{self.convert_host_port(host, port)}'
1818

19-
def filter_list(self, data):
20-
if self.is_cidr_input:
21-
return data
22-
23-
filtered_data = []
24-
for item in data:
25-
item = str(item).strip()
26-
if item.startswith(('#', '*')) or not item:
27-
continue
28-
filtered_data.append(item)
29-
return list(set(filtered_data))
30-
3119
def generate_cidr_hosts(self, cidr_ranges):
3220
for cidr in cidr_ranges:
3321
try:
@@ -53,3 +41,27 @@ def set_cidr_total(self, cidr_ranges):
5341
port_multiplier = len(getattr(self, 'port_list', [1]))
5442
method_multiplier = len(getattr(self, 'method_list', [1]))
5543
self.set_total(total_hosts * port_multiplier * method_multiplier)
44+
45+
def read_lines_count(self, filepath):
46+
line_count = 0
47+
with open(filepath, 'rb') as f:
48+
while chunk := f.read(8192):
49+
line_count += chunk.count(b'\n')
50+
return line_count
51+
52+
def set_host_total(self, host_filepath):
53+
if host_filepath:
54+
total_hosts = self.read_lines_count(host_filepath)
55+
port_multiplier = len(getattr(self, 'port_list', [1]))
56+
method_multiplier = len(getattr(self, 'method_list', [1]))
57+
self.set_total(total_hosts * port_multiplier * method_multiplier)
58+
59+
def generate_hosts_from_file(self, filepath):
60+
try:
61+
with open(filepath, 'r', encoding='utf-8') as file:
62+
for line in file:
63+
host = line.strip()
64+
if host and not host.startswith(('#', '*')):
65+
yield host
66+
except (FileNotFoundError, IOError, UnicodeDecodeError):
67+
return

bugscanx/modules/scanners_pro/scanners/direct.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class HostDirectScanner(DirectScannerBase):
9292
def __init__(
9393
self,
9494
method_list=None,
95-
host_list=None,
95+
input_file=None,
9696
port_list=None,
9797
no302=False,
9898
threads=50,
@@ -106,7 +106,10 @@ def __init__(
106106
is_cidr_input=False,
107107
**kwargs
108108
)
109-
self.host_list = host_list or []
109+
self.input_file = input_file
110+
111+
if self.input_file:
112+
self.set_host_total(self.input_file)
110113

111114
def log_info(self, **kwargs):
112115
server = kwargs.get('server', '')
@@ -119,15 +122,13 @@ def log_info(self, **kwargs):
119122
self.logger.colorize(f"{{server:<15}}", "MAGENTA"),
120123
self.logger.colorize(f"{{port:<4}}", "ORANGE"),
121124
self.logger.colorize(f"{{ip:<16}}", "BLUE"),
122-
self.logger.colorize(f"{{host}}", "LGRAY")
123-
]
124-
125+
self.logger.colorize(f"{{host}}", "LGRAY") ]
125126
self.logger.log(' '.join(messages).format(**kwargs))
126127

127128
def generate_tasks(self):
128-
for method in self.filter_list(self.method_list):
129-
for host in self.filter_list(self.host_list):
130-
for port in self.filter_list(self.port_list):
129+
for method in self.method_list:
130+
for host in self.generate_hosts_from_file(self.input_file):
131+
for port in self.port_list:
131132
yield {
132133
'method': method.upper(),
133134
'host': host,
@@ -180,12 +181,11 @@ def log_info(self, **kwargs):
180181
self.logger.colorize(f"{{host}}", "LGRAY")
181182
]
182183

183-
self.logger.log(' '.join(messages).format(**kwargs))
184-
184+
self.logger.log(' '.join(messages).format(**kwargs))
185185
def generate_tasks(self):
186-
for method in self.filter_list(self.method_list):
186+
for method in self.method_list:
187187
for host in self.generate_cidr_hosts(self.cidr_ranges):
188-
for port in self.filter_list(self.port_list):
188+
for port in self.port_list:
189189
yield {
190190
'method': method.upper(),
191191
'host': host,

bugscanx/modules/scanners_pro/scanners/ping.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,16 @@ def complete(self):
4949
class HostPingScanner(PingScannerBase):
5050
def __init__(
5151
self,
52-
host_list=None,
52+
input_file=None,
5353
port_list=None,
5454
threads=50,
5555
**kwargs
5656
):
5757
super().__init__(port_list=port_list, threads=threads, is_cidr_input=False, **kwargs)
58-
self.host_list = host_list or []
58+
self.input_file = input_file
59+
60+
if self.input_file:
61+
self.set_host_total(self.input_file)
5962

6063
def init(self):
6164
self.log_info(port='Port', ip='IP', host='Host')
@@ -75,17 +78,16 @@ def _handle_success(self, data):
7578
data['ip'] = ip
7679
self.success(data)
7780
self.log_info(**data)
78-
81+
7982
def generate_tasks(self):
80-
for host in self.filter_list(self.host_list):
81-
for port in self.filter_list(self.port_list):
83+
for host in self.generate_hosts_from_file(self.input_file):
84+
for port in self.port_list:
8285
yield {
8386
'host': host,
8487
'port': port,
8588
}
8689

8790

88-
8991
class CIDRPingScanner(PingScannerBase):
9092
def __init__(
9193
self,
@@ -114,7 +116,7 @@ def _handle_success(self, data):
114116

115117
def generate_tasks(self):
116118
for host in self.generate_cidr_hosts(self.cidr_ranges):
117-
for port in self.filter_list(self.port_list):
119+
for port in self.port_list:
118120
yield {
119121
'host': host,
120122
'port': port,

bugscanx/modules/scanners_pro/scanners/proxy_check.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def log_info(self, proxy_host_port, status_code, response_lines=None):
8282
class HostProxyScanner(ProxyScannerBase):
8383
def __init__(
8484
self,
85-
host_list=None,
85+
input_file=None,
8686
port_list=None,
8787
target='',
8888
payload='',
@@ -97,11 +97,14 @@ def __init__(
9797
is_cidr_input=False,
9898
**kwargs
9999
)
100-
self.host_list = host_list or []
100+
self.input_file = input_file
101+
102+
if self.input_file:
103+
self.set_host_total(self.input_file)
101104

102105
def generate_tasks(self):
103-
for proxy_host in self.filter_list(self.host_list):
104-
for port in self.filter_list(self.port_list):
106+
for proxy_host in self.generate_hosts_from_file(self.input_file):
107+
for port in self.port_list:
105108
yield {
106109
'proxy_host': proxy_host,
107110
'port': port,
@@ -138,7 +141,7 @@ def __init__(
138141

139142
def generate_tasks(self):
140143
for proxy_host in self.generate_cidr_hosts(self.cidr_ranges):
141-
for port in self.filter_list(self.port_list):
144+
for port in self.port_list:
142145
yield {
143146
'proxy_host': proxy_host,
144147
'port': port,

bugscanx/modules/scanners_pro/scanners/proxy_request.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class HostProxy2Scanner(Proxy2ScannerBase):
6767
def __init__(
6868
self,
6969
method_list=None,
70-
host_list=None,
70+
input_file=None,
7171
port_list=None,
7272
no302=False,
7373
threads=50,
@@ -81,7 +81,10 @@ def __init__(
8181
is_cidr_input=False,
8282
**kwargs
8383
)
84-
self.host_list = host_list or []
84+
self.input_file = input_file
85+
86+
if self.input_file:
87+
self.set_host_total(self.input_file)
8588

8689
def log_info(self, **kwargs):
8790
server = kwargs.get('server', '')
@@ -100,9 +103,9 @@ def log_info(self, **kwargs):
100103
self.logger.log(' '.join(messages).format(**kwargs))
101104

102105
def generate_tasks(self):
103-
for method in self.filter_list(self.method_list):
104-
for host in self.filter_list(self.host_list):
105-
for port in self.filter_list(self.port_list):
106+
for method in self.method_list:
107+
for host in self.generate_hosts_from_file(self.input_file):
108+
for port in self.port_list:
106109
yield {
107110
'method': method.upper(),
108111
'host': host,
@@ -158,9 +161,9 @@ def log_info(self, **kwargs):
158161
self.logger.log(' '.join(messages).format(**kwargs))
159162

160163
def generate_tasks(self):
161-
for method in self.filter_list(self.method_list):
164+
for method in self.method_list:
162165
for host in self.generate_cidr_hosts(self.cidr_ranges):
163-
for port in self.filter_list(self.port_list):
166+
for port in self.port_list:
164167
yield {
165168
'method': method.upper(),
166169
'host': host,

bugscanx/modules/scanners_pro/scanners/ssl.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ def complete(self):
5252
class HostSSLScanner(SSLScannerBase):
5353
def __init__(
5454
self,
55-
host_list=None,
55+
input_file=None,
5656
threads=50,
5757
**kwargs
5858
):
5959
super().__init__(threads=threads, is_cidr_input=False, **kwargs)
60-
self.host_list = host_list or []
60+
self.input_file = input_file
61+
62+
if self.input_file:
63+
self.set_host_total(self.input_file)
6164

6265
def log_info(self, **kwargs):
6366
messages = [
@@ -68,7 +71,7 @@ def log_info(self, **kwargs):
6871
self.logger.log(' '.join(messages).format(**kwargs))
6972

7073
def generate_tasks(self):
71-
for host in self.filter_list(self.host_list):
74+
for host in self.generate_hosts_from_file(self.input_file):
7275
yield {
7376
'host': host,
7477
}

0 commit comments

Comments
 (0)