Skip to content

v1.0.5 #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions Utils/Multi_TCP_Echo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Multiple TCP Echo Server
# Based on the third example from:
# https://rosettacode.org/wiki/Echo_server#Python

#!usr/bin/env python
import socket
import threading

NUM_PORTS = 5 # Echo on this many ports starting at PORT_BASE
PORT_BASE = 1200 # Change this if required
HOST = '192.168.0.50' # Change this to match your local IP
SOCKET_TIMEOUT = 30

# This function handles reading data sent by a client, echoing it back
# and closing the connection in case of timeout (30s) or "quit" command
# This function is meant to be started in a separate thread
# (one thread per client)
def handle_echo(client_connection, client_address, port):
client_connection.settimeout(SOCKET_TIMEOUT)
try:
while True:
data = client_connection.recv(1024)
# Close connection if "quit" received from client
if data == b'quit\r\n' or data == b'quit\n':
print('{} : {} disconnected'.format(client_address,port))
client_connection.shutdown(1)
client_connection.close()
break
# Echo back to client
elif data:
print('FROM {} : {} : {}'.format(client_address,port,data))
client_connection.send(data)
# Timeout and close connection after 30s of inactivity
except socket.timeout:
print('{} : {} timed out'.format(client_address,port))
client_connection.shutdown(1)
client_connection.close()

# This function opens a socket and listens on specified port. As soon as a
# connection is received, it is transfered to another socket so that the main
# socket is not blocked and can accept new clients.
def listen(host, port):
# Create the main socket (IPv4, TCP)
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
connection.bind((host, port))
# Listen for clients (max 10 clients in waiting)
connection.listen(10)
# Every time a client connects, allow a dedicated socket and a dedicated
# thread to handle communication with that client without blocking others.
# Once the new thread has taken over, wait for the next client.
while True:
current_connection, client_address = connection.accept()
print('{} : {} connected'.format(client_address, port))
handler_thread = threading.Thread( \
target = handle_echo, \
args = (current_connection,client_address,port) \
)
# daemon makes sure all threads are killed if the main server process
# gets killed
handler_thread.daemon = True
handler_thread.start()

if __name__ == "__main__":
print('starting')

threads = list()

for i in range(NUM_PORTS):
threads.append( threading.Thread( \
target = listen, args = (HOST, PORT_BASE + i)) )
threads[i].daemon = True
threads[i].start()

40 changes: 40 additions & 0 deletions Utils/Multi_UDP_Echo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Multiple UDP Echo Server
# Based on code by David Manouchehri
# Threading added by PaulZC

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

# Original author: David Manouchehri <manouchehri@protonmail.com>
# This script will always echo back data on the UDP port of your choice.
# Useful if you want nmap to report a UDP port as "open" instead of "open|filtered" on a standard scan.
# Works with both Python 2 & 3.

import socket
import threading

num_ports = 5 # Echo on this many ports starting at server_port_base
server_port_base = 1200 # Change this if required
server_address = '192.168.0.50' # Change this to match your local IP

def echo(address, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
serv = (address, port)
sock.bind(serv)
while True:
payload, client_address = sock.recvfrom(4) # Try to receive 4 bytes
print("Port " + str(port) + ": Echoing 4 bytes back to " + str(client_address))
sent = sock.sendto(payload, client_address)

if __name__ == "__main__":
print("Listening on " + server_address)

threads = list()

for i in range(num_ports):
threads.append( threading.Thread( \
target = echo, args = (server_address, server_port_base + i)) )
threads[i].daemon = True
threads[i].start()


Loading