#!/usr/bin/env python

# Multi-threaded Python Proprietary Protocol Responder
# HackWhackandSmack 2014
# www.hackwhackandsmack.com


from subprocess import call
import socket, threading, os, signal, struct, time

class ClientThread(threading.Thread):

	def __init__(self,ip,port,socket):
		threading.Thread.__init__(self)
		self.ip = ip
		self.port = port
		self.socket = socket
		print "[+] New thread started for "+ip+":"+str(port)


	def run(self):    
		print "Connection from : "+ip+":"+str(port)
		
		# Send an initial HEX String when the client connects here........
		# self.socket.send("\x00\x00\x00\x00\x00\x00\x00\xB6\x00\x00\x00\x45")
			
		while len(data):
			data = self.socket.recv(2048)
			for line in data.split('\n'):
			
			# If you want to send more data after the inital connection do it here........................
			#self.socket.send("\x00\x00\x00\x00\x00\x00\x00\xB6\x00\x00\x00\x45")

				# Uncomment this is you want to search for a specific hex string then resond with some hex uncomment these lines and use accordingly
				# if "\x00\x00\x00\x00\x00\x00\x00\xB6\x00\x00\x00\x45" in line:
					# print line
					# self.socket.send("\x00\x00\x00\x00\x00\x00\x00\xB6\x00\x00\x00\x45")

				
def ctrlc_handler(signum, frm):
        print "\n\nExiting"
        exit()

signal.signal(signal.SIGINT, ctrlc_handler)

host = "0.0.0.0"

# Change this to the management port
port = 9999

tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

tcpsock.bind((host,port))
threads = []


while True:
	tcpsock.listen(4)
	print "\nListening for incoming connections..."
	(clientsock, (ip, port)) = tcpsock.accept()
	newthread = ClientThread(ip, port, clientsock)
	newthread.start()
	threads.append(newthread)

for t in threads:
	t.join()
