SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.
Python attacksurfacecticybersecurityfootprintinghackinginformation-gatheringinformation-securityinfosecintelligence-gatheringosintosint-frameworkosint-reconnaissanceosint-toolpentestingpythonreconsecurity-toolsthreat-intelligencethreatintel
sfp_portscan_tcp: Cleanup and fix loading ports from file/URL (#1301)
bcolescd52743e1408
diff --git a/modules/sfp_portscan_tcp.py b/modules/sfp_portscan_tcp.pyindex cb385a47..bd0d591a 100644--- a/modules/sfp_portscan_tcp.py+++ b/modules/sfp_portscan_tcp.py@@ -16,7 +16,6 @@ import threadingimport timefrom netaddr import IPNetwork-from spiderfoot import SpiderFootEvent, SpiderFootPlugin@@ -71,10 +70,15 @@ class sfp_portscan_tcp(SpiderFootPlugin):for opt in list(userOpts.keys()):self.opts[opt] = userOpts[opt]+ portlist = list()if self.opts['ports'][0].startswith("http://") or \self.opts['ports'][0].startswith("https://") or \self.opts['ports'][0].startswith("@"):- portlist = self.sf.optValueToData(self.opts['ports'][0])+ file_ports = self.sf.optValueToData(self.opts['ports'][0])+ if file_ports:+ portlist = file_ports.split("\n")+ else:+ self.sf.error(f"Could not load ports from {self.opts['ports'][0]}")else:portlist = self.opts['ports']@@ -93,8 +97,6 @@ class sfp_portscan_tcp(SpiderFootPlugin):return ['IP_ADDRESS', 'NETBLOCK_OWNER']# What events this module produces- # This is to support the end user in selecting modules based on events- # produced.def producedEvents(self):return ["TCP_PORT_OPEN", "TCP_PORT_OPEN_BANNER"]@@ -128,9 +130,9 @@ class sfp_portscan_tcp(SpiderFootPlugin):# Spawn threads for scanningwhile i < len(portList):- self.sf.info("Spawning thread to check port: " + str(portList[i]) + " on " + ip)- t.append(threading.Thread(name='sfp_portscan_tcp_' + str(portList[i]),- target=self.tryPort, args=(ip, portList[i])))+ port = portList[i]+ self.sf.info(f"Spawning thread to check port: {port} on {ip}")+ t.append(threading.Thread(name=f"sfp_portscan_tcp_{port}", target=self.tryPort, args=(ip, port)))t[i].start()i += 1@@ -150,75 +152,84 @@ class sfp_portscan_tcp(SpiderFootPlugin):# Generate TCP_PORT_OPEN_BANNER eventdef sendEvent(self, resArray, srcEvent):for cp in resArray:- if resArray[cp]:- self.sf.info("TCP Port " + cp + " found to be OPEN.")- evt = SpiderFootEvent("TCP_PORT_OPEN", cp, self.__name__, srcEvent)- self.notifyListeners(evt)- if resArray[cp] != "" and resArray[cp] is not True:- banner = str(resArray[cp], 'utf-8', errors='replace')- bevt = SpiderFootEvent("TCP_PORT_OPEN_BANNER", banner,- self.__name__, evt)- self.notifyListeners(bevt)+ if not resArray[cp]:+ continue++ self.sf.info(f"TCP port {cp} found to be OPEN.")+ evt = SpiderFootEvent("TCP_PORT_OPEN", cp, self.__name__, srcEvent)+ self.notifyListeners(evt)++ if resArray[cp] is not True:+ banner = str(resArray[cp], 'utf-8', errors='replace')+ bevt = SpiderFootEvent("TCP_PORT_OPEN_BANNER", banner, self.__name__, evt)+ self.notifyListeners(bevt)# Handle events sent to this moduledef handleEvent(self, event):eventName = event.eventTypesrcModuleName = event.moduleeventData = event.data- scanIps = list()if self.errorState:- return None+ returnself.sf.debug(f"Received event, {eventName}, from {srcModuleName}")if not self.portlist:self.sf.error('No ports specified in port list')self.errorState = True- return None+ return- try:- if eventName == "NETBLOCK_OWNER" and self.opts['netblockscan']:+ scanIps = list()+ if eventName == "NETBLOCK_OWNER":+ if not self.opts['netblockscan']:+ self.sf.debug(f"Scanning of owned netblocks is disabled. Skipping netblock {eventData}.")+ return++ try:net = IPNetwork(eventData)- if net.prefixlen < self.opts['netblockscanmax']:- self.sf.debug("Skipping port scanning of " + eventData + ", too big.")- return None-- for ip in net:- ipaddr = str(ip)- if ipaddr.split(".")[3] in ['255', '0']:- continue- if '255' in ipaddr.split("."):- continue- scanIps.append(ipaddr)- else:- scanIps.append(eventData)- except Exception as e:- self.sf.error("Strange netblock identified, unable to parse: " + eventData + " (" + str(e) + ")")- return None+ except Exception as e:+ self.sf.error(f"Strange netblock identified, unable to parse: {eventData} ({e})")+ return++ if net.prefixlen < self.opts['netblockscanmax']:+ self.sf.debug(f"Skipping port scanning of owned net block {eventData}, too big.")+ return++ for ip in net:+ ipaddr = str(ip)+ if '255' in ipaddr.split("."):+ continue+ if ipaddr.split(".")[3] == '0':+ continue+ scanIps.append(ipaddr)+ else:+ scanIps.append(eventData)- for ipAddr in scanIps:- # Don't look up stuff twice+ for ipAddr in set(scanIps):if ipAddr in self.results:- self.sf.debug("Skipping " + ipAddr + " as already scanned.")- return None- else:- self.results[ipAddr] = True+ self.sf.debug(f"Skipping {ipAddr} as already scanned.")+ return++ self.results[ipAddr] = True++ self.sf.info(f"Scanning {len(set(self.portlist))} ports on {ipAddr}")i = 0portArr = []for port in self.portlist:if self.checkForStop():- return None+ returnif i < self.opts['maxthreads']:portArr.append(port)- i += 1else:self.sendEvent(self.tryPortWrapper(ipAddr, portArr), event)- i = 1+ i = 0portArr = [port]+ i += 1+# Scan whatever is remainingself.sendEvent(self.tryPortWrapper(ipAddr, portArr), event)