def forwardTraffic(self, srcip, srcport, dstip, dstport, msg): for uid in self.neighbors: if (self.neighbors[uid].location == "Mesh"): if (self.neighbors[uid].ip == dstip) and (self.neighbors[uid].port == dstport): # connect and send remote_sock = Comms.create_direct_socket( self.neighbors[uid].ip, self.neighbors[uid].port) if (remote_sock): Comms.sendMsg( remote_sock, srcip + ":" + str(srcport) + ":" + dstip + ":" + str(dstport) + ":" + msg) msg = srcip + ":" + str( srcport) + ":" + dstip + ":" + str( dstport) + ":" + msg remote_sock.close() else: print("FAILED TO SEND") return for uid in self.neighbors: if (self.neighbors[uid].location == "Mesh"): if not ((self.neighbors[uid].ip == srcip) and (self.neighbors[uid].port == srcport)): # connect and send remote_sock = Comms.create_direct_socket( self.neighbors[uid].ip, self.neighbors[uid].port) if (remote_sock): Comms.sendMsg( remote_sock, srcip + ":" + str(srcport) + ":" + dstip + ":" + str(dstport) + ":" + msg) remote_sock.close() else: print("FAILED TO SEND") return
def rmtsh(self, tmp_sock, slist, server_sock): prompt = "rmtsh (EXIT to quit) " cwd = "" # used to keep track of current working dir # attempt to get the pwd/cwd so we can use in in our commands Comms.sendMsg(tmp_sock, "EXEC pwd") Comms.sendMsg(tmp_sock, "GETBUFFER") while (1): displayPrompt = False ready_to_read, ready_to_write, in_error = select.select( slist, [], [], 0) for sock in ready_to_read: displayPrompt = False if (sock == sys.stdin): # server sending message msg = sys.stdin.readline() msg = msg.lstrip( '\r\n ' ) # clean up line removing any starting spaces and CRLF if 'EXIT' in msg: # did we enter EXIT? return else: #must have entered some other command msg = msg.rstrip('\r\n') if len(msg ) > 0: # is this a blank line? just a return? if (cwd): # do we have a stored cwd? msg = "cd " + cwd + " ; " + msg # if so, change command to prepend a "cd <cwd> ; " Comms.sendMsg( tmp_sock, "EXEC " + msg + " ; pwd" ) # append a "; pwd" to the command so we can find out the ending working directory Comms.sendMsg(tmp_sock, "GETBUFFER") else: displayPrompt = True elif (sock != server_sock) and (sock != sys.stdin): msg = Comms.readMsg(sock, 4096) msg = msg.rstrip('\r\n') indx = msg.rfind('\n') # what is the ending line break? if indx == -1: indx = 0 cwd = msg[indx:].lstrip('\r\n').rstrip('\r\n') msg = msg[:indx] sys.stdout.write("\r\n") sys.stdout.write(msg) sys.stdout.write("\r\n") displayPrompt = True else: displayPrompt = False if (displayPrompt): sys.stdout.write(prompt + cwd + "> ") sys.stdout.flush()
def server_process_cmds(self, slist, ignore_list, msg, server_sock): displayPrompt = False if (msg.startswith("NODE:")): p = re.compile("NODE:(\d+)\s+(.*)") m = p.match(msg) if (m): if (self.neighbors[int(m.group(1))].location == "Direct"): displayPrompt = self.server_process_cmds( [self.neighbors[int(m.group(1))].socket], ignore_list, m.group(2), server_sock) else: sys.stdout.write( "Can only use NODE on Direct connections" + '\r\n') displayPrompt = True else: displayPrompt = True elif (msg.startswith("HELP") or msg.startswith("help") or msg.startswith("?") or msg.startswith("/?")): sys.stdout.write(getHelp()) displayPrompt = True elif (msg.startswith("LIST")): sys.stdout.write("List of current client/slave nodes:\r\n") sys.stdout.write( "------------------------------------------------------\r\n") sys.stdout.write('{:5} {:21} {:13}'.format("<#>", "<IP>:<PORT>", "<Direct/Mesh>")) sys.stdout.write("\r\n") for uid in self.neighbors: sys.stdout.write('{:5} {:21} {:13}'.format( str(uid), self.neighbors[uid].ip + ":" + str(self.neighbors[uid].port), self.neighbors[uid].location)) sys.stdout.write("\r\n") displayPrompt = True elif (msg.startswith("PUSH")): (tmp, filename) = msg.split(':', 1) for key in self.neighbors: if (self.neighbors[key].location == "Direct"): if self.neighbors[key].socket in slist: Comms.sendFile(self.neighbors[key].socket, filename) displayPrompt = True elif (msg.startswith("PULL")): p = re.compile("PULL:(.*)") m = p.match(msg) if (m): Comms.broadcast(slist, ignore_list, msg) displayPrompt = True elif (msg.startswith("SCAN")): Comms.broadcast(slist, ignore_list, msg) displayPrompt = True elif (msg.startswith("WGET")): Comms.broadcast(slist, ignore_list, msg) displayPrompt = True elif (msg.startswith("EXEC")): Comms.broadcast(slist, ignore_list, msg) displayPrompt = True elif (msg.startswith("SHELL")): p = re.compile("SHELL:(\d+)") m = p.match(msg) if (m): if (self.neighbors[int(m.group(1))].location == "Direct"): self.rmtsh(self.neighbors[int(m.group(1))].socket, slist, server_sock) else: sys.stdout.write( "Can only use SHELL on Direct connections" + '\r\n') displayPrompt = True elif (msg.startswith("CLEARBUFFER")): Comms.broadcast(slist, ignore_list, msg) displayPrompt = True elif (msg.startswith("GETBUFFER")): Comms.broadcast(slist, ignore_list, msg) displayPrompt = False elif (msg.startswith("EXIT")): Comms.broadcast(slist, ignore_list, msg) displayPrompt = True elif (msg.startswith("QUIT")): self.cleanup() elif (msg.startswith("PROCLIST")): Comms.broadcast(slist, ignore_list, msg) displayPrompt = True elif (msg.startswith("NEIGHBORS")): Comms.broadcast(slist, ignore_list, msg) displayPrompt = True elif (msg.startswith("MESH:")): p = re.compile("MESH:(\d+)\s+(.*)") m = p.match(msg) if (m): if (self.neighbors[int(m.group(1))].location == "Mesh"): n = self.neighbors[int(m.group(1))] self.forwardTraffic(self.ip, self.meshPort, n.ip, n.port, m.group(2)) displayPrompt = True elif (msg.startswith("DIST")): #match DIST <command> <file> #\s+(.+) for command #(.*)for filename #p compiles the regex p = re.compile("DIST:(.*?) (.*)") #m matches DIST <something> <something>. m = p.match(msg) #m.group(1) is the command, m.group(2) is the filename #correct command matches? then lets go! if (m): # make a local list to use clist = dict() # only copy the Direct neighbors into the new list count = 0 for key in self.neighbors: if self.neighbors[key].location == "Direct": print("testing if command exists : ") Comms.sendMsg(self.neighbors[key].socket, "EXIST:" + m.group(2)) if Comms.readMsg(self.neighbors[key].socket) == "true": clist[count] = self.neighbors[key] print('clist ' + clist[count].ip) count += 1 #check for clients, sending commands is pointless if no clients if len(clist) < 1: #is this the best way? probably not print('no clients!') #give the user back their prompt displayPrompt = True #ok, we have clients...now what? else: #first, split the input file into n parts, where n is count of nodes #splitjobs.Split takes clist to count nodes, and the filename to split #splitjobs.Split will then write files to ./tmp called 0.splitout 1.splitout ,etc s = Split(clist, m.group(1)) files = s.getFiles() print(files) #command logic check--todo #if m.group(2) is nmap, then xx, if its hashcat, then... etc #for now assume any command we want to distribute accepts a text file #for each client in clist for i in range(0, len(clist)): filename = files.pop() for key in self.neighbors: if self.neighbors[key].uid == clist[i].uid: #send this file to a node. file 0.splitout would go to node 0 #PUSH code goes here to transfer 0.splitout to node 1 (0th node), etc #issue PUSH as a server command print("running NODE:" + str(key) + " PUSH:tmp/" + filename) displayPrompt = self.server_process_cmds( [clist[i].socket], ignore_list, "NODE:%s PUSH:%s" % (key, filename), server_sock) time.sleep(2) print("running NODE:%s %s " % (i, m.group(2)) + filename) displayPrompt = self.server_process_cmds( [clist[i].socket], ignore_list, "NODE:%s EXEC %s" % (key, m.group(2)) + '' + filename, server_sock) time.sleep(2) break else: # do nothing for now displayPrompt = True return displayPrompt
def client_process_cmds(self, msg, sock): if (msg.startswith("EXIT")): sys.stdout.write("Client Terminated!!!\r\n") self.cleanup() elif (msg.startswith("SCAN")): p = re.compile("SCAN:(.+?):(.+?):(.+)") m = p.match(msg) if (m): print("Scanning: " + m.group(1) + " " + m.group(2) + "-" + m.group(3)) self.outputBuf += '\r\n'.join( str(x) for x in Scanner.scan( m.group(1), range(int(m.group(2)), int(m.group(3))))) print("finished") print(self.outputBuf) elif (msg.startswith("WGET")): p = re.compile("WGET\s+(.+)") m = p.match(msg) if (m): print("Getting: " + m.group(1)) Utils.wget(m.group(1)) elif (msg.startswith("EXEC")): p = re.compile("EXEC\s+(.+)") m = p.match(msg) if (m): sys.stdout.write("Executing [%s]\n" % m.group(1)) self.outputBuf += "\n\n" + Utils.execWait( m.group(1)).decode('unicode_escape') #pid = Utils.exec(m.group(1)) #self.proclist[pid] = m.group(1) else: None elif (msg.startswith("PROCLIST")): for pid in self.proclist: self.outputBuf += str(pid) + " " + self.proclist[pid] + '\n' elif (msg.startswith("EXIST")): (tmp, cmd) = msg.split(':', 1) result = "false" (short_cmd, args) = cmd.split(' ') if (Utils.which(short_cmd)): result = "true" Comms.sendMsg(sock, result) elif (msg.startswith("CLEARBUFFER")): self.outputBuf = "" elif (msg.startswith("GETBUFFER")): created_sock = False if not sock: # assume we are returning something to the mesh server sock = Comms.create_direct_socket(self.meshServerIP, int(self.meshServerPort)) created_sock = True self.outputBuf = self.ip + ":" + str( self.meshPort) + ":" + self.meshServerIP + ":" + str( self.meshServerPort) + ":" + self.outputBuf Comms.sendMsg(sock, self.outputBuf) if created_sock: sock.close() elif (msg.startswith("PUSH")): (tmp, filename, file_len, file_data) = msg.split(':', 3) filename = os.path.basename(filename) print("received file: " + filename) filename_orig = filename filename = "tmp/" + filename if (Utils.fileExists(filename)): filename += "_" + Utils.getRandStr(5) self.pushedfiles[filename_orig] = filename Utils.writeFile(file_data, filename, "ab") elif (msg.startswith("PULL")): (tmp, filename) = msg.split(':', 1) file_data = "" if (Utils.fileExists(filename)): file_t = open(filename, "rb") file_data = file_t.read() self.outputBuf = file_data.decode() elif (msg.startswith("NEIGHBORS")): self.outputBuf = '\n'.join(self.listNeighbors()) else: sys.stdout.write(msg)