|
#! /usr/bin/env python2.7 |
|
# -*- coding: utf-8 -*- |
|
|
|
import datetime |
|
import socket |
|
|
|
# Yet Another Humble Banner Grabber |
|
# 03.07.2020 |
|
# https://hkt.me |
|
# |
|
# ██╗ ██╗██╗ ██╗████████╗ |
|
# ██║ ██║██║ ██╔╝╚══██╔══╝ |
|
# ███████║█████╔╝ ██║ |
|
# ██╔══██║██╔═██╗ ██║ |
|
# ██║ ██║██║ ██╗ ██║ |
|
# ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ |
|
|
|
# Command to send. Enter is added as suffix. No need to write here. |
|
cmdtosend = "dir" |
|
|
|
# Input file name. File should be on the same folder with this script. Input format is [IP]:[PORT] on each line. |
|
filename = "ipport.txt" |
|
|
|
# Output file name. File should be on the same folder with this script. |
|
outfile = "bannergrab_" + datetime.datetime.now().strftime('%Y%m%d%H%M%S') + ".csv" |
|
|
|
print("{}> Start.\r\n".format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S %Z %z'))) |
|
|
|
with open(filename, 'r') as ipportfile: |
|
ipportliste = ipportfile.readlines() |
|
|
|
f = open(outfile, "w") |
|
f.write("IP;Port;Status;FR Length;First Response; CmdRes Len;Response to Command\r\n") |
|
|
|
ctr = 1 |
|
for ipport in ipportliste: |
|
try: |
|
t = ipport.strip().split(':') |
|
sip = t[0].strip() |
|
sport = int(t[1].strip()) |
|
f.write(sip + ";" + str(sport) + ";") |
|
except: |
|
continue |
|
|
|
print("{time}#{ctr} > Banner grabing is starting for the IP {ip} on port {port}.".format(ip=sip, port=sport, ctr=ctr, time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S %Z %z'))) |
|
ctr+=1 |
|
s = socket.socket() |
|
s.settimeout(2) |
|
try: |
|
s.connect((sip,sport)) |
|
print(' Port is open') |
|
f.write("OPEN;") |
|
|
|
try: |
|
data = s.recv(1024).strip().encode('unicode_escape') |
|
#print(data) |
|
print(' Data received. {}. MECO successful.'.format(len(data))) |
|
f.write(str(len(data)) + ";" + data + ";") |
|
except: |
|
print(' No banner.') |
|
f.write("N/A;N/A;") |
|
|
|
try: |
|
s.sendall(cmdtosend + "\r\n") |
|
print(' Sent the payload.') |
|
data2 = s.recv(1024).strip().encode('unicode_escape') |
|
#print(data2) |
|
print(' Data received. {}. SECO successful.'.format(len(data2))) |
|
f.write(str(len(data2)) + ";" + data2 + "\r\n") |
|
except: |
|
print(' Error during second stage.') |
|
f.write("N/A;N/A\r\n") |
|
except: |
|
print(' Port is closed') |
|
f.write("CLOSED;-;-;-;-\r\n") |
|
s.close() |
|
print(' Landing burn successful.') |
|
|
|
f.close() |
|
print("{}> Done.".format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S %Z %z'))) |
|
|