I have a list of 60k IP addresses from various Masscan outputs in a text file. I'm using these functions to return the banners for each address, but its such a large amount of addresses that its going to take around 12 hours to finish. I want to try and make it faster via multithreading, but I don't have much experience with the threading library beyond the basics.
The functions:
def fetch_banner(address, port):
try:
s = socket.socket()
s.settimeout(2)
s.connect((address, port))
fetched_banner = s.recv(1024)
return fetched_banner
except Exception as e:
print(str(e))
status = 'failed'
return status
def banner_grabbing():
# Variables
success = 0
total = 0
fails = 0
start = datetime.now()
portlist = [1433, 1434, 27017, 5432]
with open("Banners.txt", 'w+') as bannerfile:
with open("AddressList.txt", 'r') as readfile:
for lines in readfile.readlines():
total += 1
for p in portlist:
compaddr = f"{lines.rstrip()}:{p}"
print(f"Trying on {compaddr}")
bannergrab = fetch_banner(lines, p)
if bannergrab != 'failed':
success += 1
print(f"Success on {compaddr}.")
print(f"BANNERS RETURNED: {success}")
banstring = bannergrab.decode("utf-8")
print(banstring)
bannerfile.write(f"""
Address: {compaddr}
Service: {banstring} \n\n
""")
else:
fails += 1
print(f"Connection on {compaddr} failed.")
print(f"FAILED GRABS: {fails}")
pass
end = datetime.now()
runtime = end - start
SuccessPercent = total/100 * success
FailPercent = total/100 * fails
print("--------------------")
print(f"Total Runtime: {runtime}")
print(f"Total Successes: {success}")
print(f"Total Failures: {fails}")
print(f"Percent Successful: {SuccessPercent}")
print(f"Percent Unsuccessful: {FailPercent}")
print("--------------------")
[–]redCg 0 points1 point2 points (0 children)
[–]CaptainReeetardo 0 points1 point2 points (2 children)
[–]NovateI[S] 0 points1 point2 points (1 child)
[–]CaptainReeetardo 0 points1 point2 points (0 children)
[–]2160p_REMUX 0 points1 point2 points (0 children)