|
@@ -0,0 +1,136 @@
|
|
|
|
|
+import json
|
|
|
|
|
+import os
|
|
|
|
|
+import signal
|
|
|
|
|
+import socket
|
|
|
|
|
+import subprocess
|
|
|
|
|
+import sys
|
|
|
|
|
+import websocket
|
|
|
|
|
+
|
|
|
|
|
+DEBUG = False
|
|
|
|
|
+DEBUG = True
|
|
|
|
|
+def log(s):
|
|
|
|
|
+ if DEBUG == False:
|
|
|
|
|
+ return
|
|
|
|
|
+ print(s)
|
|
|
|
|
+
|
|
|
|
|
+def sendmsg(msg):
|
|
|
|
|
+ s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
+ ret = None
|
|
|
|
|
+ try:
|
|
|
|
|
+ s.connect(('localhost',9999))
|
|
|
|
|
+ tosend = msg + "\r\n"
|
|
|
|
|
+ s.sendall(tosend.encode("ascii"))
|
|
|
|
|
+ ret = s.recv(4096)
|
|
|
|
|
+ s.close()
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ log(e)
|
|
|
|
|
+ return None
|
|
|
|
|
+ log("[nc output] %s " % ret)
|
|
|
|
|
+ return ret
|
|
|
|
|
+
|
|
|
|
|
+class PManager:
|
|
|
|
|
+ ws = websocket.WebSocket()
|
|
|
|
|
+ ip = "ws://192.168.1.208:9001"
|
|
|
|
|
+ p_list = {}
|
|
|
|
|
+ ptable = {}
|
|
|
|
|
+ log_list = {}
|
|
|
|
|
+ def __init__(self, ptable):
|
|
|
|
|
+ self.ptable = ptable
|
|
|
|
|
+ self.conn(self.ip)
|
|
|
|
|
+ signal.signal(signal.SIGINT, self.sighandler)
|
|
|
|
|
+ signal.signal(signal.SIGCHLD, signal.SIG_IGN) #To kill zombies
|
|
|
|
|
+
|
|
|
|
|
+ while True:
|
|
|
|
|
+ try:
|
|
|
|
|
+ data = self.ws.recv()
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ log(e)
|
|
|
|
|
+ self.conn(self.ip)
|
|
|
|
|
+ continue
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ j = json.loads(data)
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ log(e)
|
|
|
|
|
+ log("Invalid json?")
|
|
|
|
|
+ continue
|
|
|
|
|
+
|
|
|
|
|
+ if ("type" not in j or "command" not in j or
|
|
|
|
|
+ "key" not in j or j["type"] != "snowmix"):
|
|
|
|
|
+ log("Dropping: %s" % j)
|
|
|
|
|
+ continue
|
|
|
|
|
+
|
|
|
|
|
+ log(j)
|
|
|
|
|
+ if j["command"] == "launch":
|
|
|
|
|
+ self.launch(j["key"])
|
|
|
|
|
+
|
|
|
|
|
+ if j["command"] == "kill":
|
|
|
|
|
+ self.stop(j["key"])
|
|
|
|
|
+
|
|
|
|
|
+ if j["command"] == "netcat":
|
|
|
|
|
+ sendmsg(j["key"])
|
|
|
|
|
+
|
|
|
|
|
+ def conn(self,target):
|
|
|
|
|
+ if self.ws is None:
|
|
|
|
|
+ self.ws = websocket.WebSocket()
|
|
|
|
|
+ try:
|
|
|
|
|
+ self.ws.connect(target)
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ time.sleep(2)
|
|
|
|
|
+ conn(target) #keep trying you basterd
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ def close(self):
|
|
|
|
|
+ if self.ws is None:
|
|
|
|
|
+ return
|
|
|
|
|
+ self.ws.close()
|
|
|
|
|
+
|
|
|
|
|
+ def sighandler(self,signal,frame):
|
|
|
|
|
+ log("\rExiting!")
|
|
|
|
|
+ self.close()
|
|
|
|
|
+ log("Bye!")
|
|
|
|
|
+ sys.exit(0)
|
|
|
|
|
+
|
|
|
|
|
+ def stop(self,key):
|
|
|
|
|
+ if key not in self.p_list:
|
|
|
|
|
+ log("key %s doesn't exist! not killing!" % (key))
|
|
|
|
|
+ return
|
|
|
|
|
+ try:
|
|
|
|
|
+ os.killpg(os.getpgid(self.p_list[key].pid), signal.SIGTERM)
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ log("This process (%d) can't be killed! Why?" % self.p_list[key].pid)
|
|
|
|
|
+ log(e)
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ self.log_list[key].close()
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ log("ex file: %s" % e)
|
|
|
|
|
+
|
|
|
|
|
+ if key in self.log_list:
|
|
|
|
|
+ del self.log_list[key]
|
|
|
|
|
+
|
|
|
|
|
+ del self.p_list[key]
|
|
|
|
|
+
|
|
|
|
|
+ def launch(self,key,options = None):
|
|
|
|
|
+ if key in self.p_list:
|
|
|
|
|
+ log("key %s exist! not running!" % (key))
|
|
|
|
|
+ return
|
|
|
|
|
+
|
|
|
|
|
+ if key not in self.ptable:
|
|
|
|
|
+ log("This process (%s) is not on my table!" % key)
|
|
|
|
|
+ return
|
|
|
|
|
+
|
|
|
|
|
+ log("Launching! key: %s, opts: %s" % (key,options))
|
|
|
|
|
+ tolaunch = self.ptable[key]
|
|
|
|
|
+ if options is not None:
|
|
|
|
|
+ tolaunch = tolaunch + options
|
|
|
|
|
+
|
|
|
|
|
+ log(tolaunch)
|
|
|
|
|
+ self.log_list[key] = open("/tmp/%s" % key, "w")
|
|
|
|
|
+ proc = subprocess.Popen(tolaunch, preexec_fn = os.setsid, stdout = self.log_list[key])
|
|
|
|
|
+ #Le doy su propio grupo al subproceso
|
|
|
|
|
+
|
|
|
|
|
+ self.p_list[key] = proc
|
|
|
|
|
+ return proc.pid
|
|
|
|
|
+
|
|
|
|
|
+
|