diff --git a/MainPresenter.py b/MainPresenter.py index 43fef43..23d934e 100644 --- a/MainPresenter.py +++ b/MainPresenter.py @@ -34,13 +34,13 @@ class MainPresenter: thread.start() def on_thread_exit(self, is_last): - if is_last == True: + if is_last: self.isScanEnabled = False self.ui.startButton.setText("Start") return count = 0 for thr in self.threads: - if thr.is_running == True: + if thr.is_running: count = count + 1 self.setCurrentThreadsLabel(count) @@ -52,7 +52,7 @@ class MainPresenter: count = 0 is_last_thread = False for i in self.threads: - if i.is_running != True: + if not i.is_running: count += 1 if count == len(self.threads): is_last_thread = True @@ -90,7 +90,7 @@ class ScanThread(QThread): count = 0 is_last_thread = False for i in self.presenter.threads: - if i.isRunning() != True: + if not i.isRunning(): count += 1 if count == len(self.presenter.threads): is_last_thread = True diff --git a/Parser.py b/Parser.py index 3da1ff2..c73ee8c 100644 --- a/Parser.py +++ b/Parser.py @@ -2,53 +2,75 @@ import ipaddress def getPortsFromString(ports): - # Converts ports from form 20-40,100-900,40000-70000 - # It will automatically prune off non-existent ports (<1 >65535) - if ports is None: - return [21, 22, 23, 25, 80, 443, 110, 111, 135, 139, 445, 8080, 8443, 53, 143, 989, 990, 3306, 1080, 5554, 6667, 2222, 4444, 666, 6666, 1337, 2020, 31337] # Change to default ports from constant - else: - if "-" not in ports: - tports = ports.split(",") - print(tports) - else: - ports = ports.split(",") - tports = [] - for port in ports: - if "-" not in port: - tports.append(int(port)) + """ + Parses ports from string, returns them as integers in the list. + Handles non-existent ports and non-port values. + """ + if ports: + # Using set to avoid repetitions + parsed = set() + ports = ports.split(",") + for port in ports: + try: + # Input is in range form ("100-200"): + if '-' in port: + start, end = map(int, port.split('-')) + parsed.update( + [p for p in range(start, end + 1) if 65355 >= p > 0]) + # Input is a single port ("80"): else: - # I made this one line because I wanted to - tports.extend( - list(range(int(port.split("-")[0]), int(port.split("-")[1]) + 1))) - ports = [int(n) for n in tports if int(n) > 0 and int(n) < 65536] - return ports + parsed.add(int(port)) + except ValueError as e: + # If we get any not integer just ignore it + pass + return sorted(list(parsed)) + else: + # Change to default ports from constant + return [21, 22, 23, 25, 80, 443, 110, 111, 135, 139, 445, 8080, 8443, 53, 143, 989, 990, 3306, 1080, 5554, 6667, 2222, 4444, 666, 6666, 1337, 2020, 31337] def getCIDRFromRanges(ips): + """ + Parses ip input string, returns the generator over them. + + Supports next inputs: + 1) 1.2.3.4 + 2) 192.168.0.0/24 + 3) 1.2.3.4 - 5.6.7.8 + Any non-ip value will be ignored. + """ + + # A set to contain non repeating ip objects from ipaddress ip_objects = set() inputs = [ip.strip() for ip in ips.split(',')] for input_ in inputs: try: + # Input is in range form ("1.2.3.4 - 5.6.7.8"): if '-' in input_: input_ips = input_.split('-') ranges = {ipaddr for ipaddr in ipaddress.summarize_address_range( ipaddress.IPv4Address(input_ips[0]), ipaddress.IPv4Address(input_ips[1])) - } + } ip_objects.update(ranges) + # Input is in CIDR form ("192.168.0.0/24"): elif '/' in input_: network = ipaddress.ip_network(input_) ip_objects.add(network) + # Input is a single ip ("1.1.1.1"): else: ip = ipaddress.ip_address(input_) ip_objects.add(ip) except ValueError as e: - print(e) + # If we get any non-ip value just ignore it + pass for ip_obj in ip_objects: - if not isinstance(ip_obj, ipaddress.IPv4Address): - for ip in ip_obj.hosts(): - yield ip - else: + # The object is just one ip, simply yield it: + if isinstance(ip_obj, ipaddress.IPv4Address): yield ip_obj + # The object is a network, yield every host in it: + else: + for host in ip_obj.hosts(): + yield host diff --git a/main.py b/main.py index b16c9af..455abd3 100644 --- a/main.py +++ b/main.py @@ -19,15 +19,17 @@ class MyWin(QtWidgets.QMainWindow): self.isScanActive = False def startButtonClicked(self): - if self.presenter.isScanEnabled == False: - self.presenter.isScanEnabled = True - self.ui.startButton.setText("Stop") - self.presenter.startScan(self.ui.ipLine.text(), self.ui.portsLine.text(), self.ui.threadsLine.text(), - self.ui.timeoutLine.text()) - else: + if self.presenter.isScanEnabled: self.presenter.isScanEnabled = False self.ui.startButton.setText("Start") self.presenter.stopScan() + else: + self.presenter.isScanEnabled = True + self.ui.startButton.setText("Stop") + self.presenter.startScan(self.ui.ipLine.text(), + self.ui.portsLine.text(), + self.ui.threadsLine.text(), + self.ui.timeoutLine.text()) if __name__ == "__main__":