Skip to content

Commit

Permalink
fix keyboard interrupt handling
Browse files Browse the repository at this point in the history
  • Loading branch information
alexej996 committed Mar 3, 2023
1 parent cf134a1 commit a150d47
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions tmap
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import threading
try:
import socks
except ImportError as ie:
print("Socks module not found.\nMake sure to install all requirements with\npip3 install -r requirments.txt")
print("Socks module not found.\nMake sure to install all requirements with\npip3 install -r requirments.txt\nAlternatively you can install python3-socks package on debian based systems with:\nsudo apt install python3-socks")
exit()

VERSION='1.3.1'
VERSION='1.3.2'

def getPortInfo(port, portfile):
"""
Expand Down Expand Up @@ -110,8 +110,6 @@ def connScan(host, port, wait, notor, openports):
sckt.close()
openports.append(port)
return True
except KeyboardInterrupt:
exit()
except:
return False

Expand All @@ -132,12 +130,20 @@ def portScan(host, ports, wait, notor, jobs):
if p > 65535:
return openports

while threading.activeCount() >= jobs + 1:
while threading.activeCount() >= jobs + 1:
pass

thread=threading.Thread(target=connScan,args=(host, p, wait, notor, openports))
threads.append(thread)
thread.start()
try:
thread=threading.Thread(target=connScan,args=(host, p, wait, notor, openports))
thread.daemon=True
threads.append(thread)
thread.start()
## need to sleep so Keybaord Interrupt is caught
time.sleep(0.1)
except (KeyboardInterrupt, SystemExit):
print("Caught keyboard interrupt. Exiting...")
## return tuple instead of just openports, so hostScan knows about interrupt
return (openports,False)

## Wait until all threads are done
for thread in threads:
Expand All @@ -164,21 +170,35 @@ def hostScan(host, ports, wait, notor, jobs):
if ips.num_addresses > 1:
for ip in ips.hosts():
if ip.is_private:
ret[str(ip)] = portScan(str(ip), ports, wait, True, jobs)
resultportscan = portScan(str(ip), ports, wait, True, jobs)
else:
ret[str(ip)] = portScan(str(ip), ports, wait, notor, jobs)
resultportscan = portScan(str(ip), ports, wait, notor, jobs)
if isinstance(resultportscan, list):
ret[str(ip)] = resultportscan
else:
ret[str(ip)] = resultportscan[0]
return ret
else:
if ips.is_private:
ret[str(host)] = portScan(str(host), ports, wait, True, jobs)
resultportscan = portScan(str(host), ports, wait, True, jobs)
else:
resultportscan = portScan(str(host), ports, wait, notor, jobs)
if isinstance(resultportscan, list):
ret[str(host)] = resultportscan
else:
ret[str(host)] = portScan(str(host), ports, wait, notor, jobs)
ret[str(host)] = resultportscan[0]
return ret

## Otherwise scan host as usual
except:
if host == 'localhost':
ret[str(host)] = portScan(str(host), ports, wait, True, jobs)
resultportscan = portScan(str(host), ports, wait, True, jobs)
else:
ret[str(host)] = portScan(str(host), ports, wait, notor, jobs)

resultportscan = portScan(str(host), ports, wait, notor, jobs)
if isinstance(resultportscan, list):
ret[str(host)] = resultportscan
else:
ret[str(host)] = resultportscan[0]
return ret

def parseArgs(parser):
Expand Down

0 comments on commit a150d47

Please sign in to comment.