Skip to content

Instantly share code, notes, and snippets.

@rafa-br34
Created March 10, 2024 08:46
Show Gist options
  • Save rafa-br34/a0e174455a829bb63157646c425eb457 to your computer and use it in GitHub Desktop.
Save rafa-br34/a0e174455a829bb63157646c425eb457 to your computer and use it in GitHub Desktop.
A simple python script to check if hosts are responding or not
import contextlib
import threading
import socket
import struct
from colorama import Fore, Back, Style
c_TargetsFile = "Targets.txt"
c_Timeout = 10
c_Threads = 200
c_Ports = [23]
#"""
# Telnet
def c_VerifyHost(Socket):
Socket.send(b"ls /\r\r\n")
Socket.recv(8192)
Socket.send(b"ls /\r\r\n")
return (b"dev" in Socket.recv(8192))
#"""
"""
# Remote FrameBuffer (VNC)
def c_VerifyHost(Socket):
# Server sends the version number and we verify if it's RFB
if b"RFB" not in Socket.recv(12):
return False
# We send our "version number" and wait for the possible auth types
Socket.send(b"RFB 003.008\n")
SecurityTypes = Socket.recv(512)
# Connection error or invalid data?
if SecurityTypes[0] == 0x00 or SecurityTypes[0] > len(SecurityTypes) - 1:
return False
# Iterate security types to check if server has no auth
for i in range(SecurityTypes[0]):
if SecurityTypes[i + 1] == 1:
return True
return False
"""
class _State:
Threads = []
Hosts = []
Dead = []
Live = []
g_State = _State()
def CheckHost(Host, Port):
with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as Socket:
Socket.settimeout(c_Timeout)
if Socket.connect_ex((Host, Port)) == 0:
try:
Result = c_VerifyHost(Socket)
if Result:
print(Fore.LIGHTGREEN_EX + Back.RED + f"{Host}:{Port} {Result}" + Style.RESET_ALL)
return True
else:
print(f"{Host}:{Port} Failed")
except TimeoutError:
print(f"{Host}:{Port} Timed out")
except ConnectionResetError:
print(f"{Host}:{Port} Connection was reset")
else:
print(f"{Host}:{Port} Failed to connect or timed out")
return False
def WorkerLogic():
while len(g_State.Hosts):
Host = g_State.Hosts.pop()
for Port in c_Ports:
if CheckHost(Host, Port):
g_State.Live.append((Host, Port))
else:
g_State.Dead.append((Host, Port))
def main():
with contextlib.closing(open(c_TargetsFile, "r+")) as File:
g_State.Hosts.extend(list(set(File.read().split('\n'))))
print(f"{len(g_State.Hosts)} Hosts loaded")
for _ in range(c_Threads):
NewThread = threading.Thread(target=WorkerLogic)
NewThread.start()
g_State.Threads.append(NewThread)
for Thread in g_State.Threads:
Thread.join()
print("Dead hosts:")
for Host in g_State.Dead:
print(Fore.LIGHTBLACK_EX + "{}:{}".format(*Host) + Style.RESET_ALL)
print("Live hosts:")
for Host in g_State.Live:
print(Fore.LIGHTGREEN_EX + "{}:{}".format(*Host) + Style.RESET_ALL)
print(f"{len(g_State.Live)} Live hosts, {len(g_State.Dead)} dead hosts")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment