Ejemplo n.º 1
0
    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