Пример #1
0
def prep_results(operator, experiment_name):
    results = {}
    run_id = str(int(conf["run_id"]) + 1)
    ip = getmyip()
    # esttime = getESTTime()
    localtime = datetime.now().isoformat()
    country = geolocate(ip)[0]
    city = geolocate(ip)[1]

    meta = { # "est_time"		: esttime,
	     "local_time"	: localtime,
	     "client_tag"   	: conf["client_tag"],
	     "exp_name"		: experiment_name,
	     "country"		: country,
	     "city"		: city,
	     "operator" 	: operator,
	     "ext_ip"		: ip,
	     "run_id"		: run_id
	   }

    conf.set("run_id", run_id)
    conf.update()
    results["meta"] = meta

    return results, run_id
Пример #2
0
    def kobra_command_handler(self, clientsocket, address, username, aes_secret, com):
	try:
	    if com == "exit" or com == "quit":
		send_aes_crypt(clientsocket, address, "<END>", aes_secret)
		return False
	    send_aes_crypt(clientsocket, address, "<START_MESSAGE>", aes_secret)
	    
	    if com == "update_centinel":
		latest_version = open(".version", "r").read()
		if self.version <> latest_version:
		    log("i", "Centinel has been updated, creating new update package...")
		    send_aes_crypt(clientsocket, address, "Centinel has been updated, creating new update package...", aes_secret)
		    self.prepare_update()
		    self.version = latest_version
		else:
		    send_aes_crypt(clientsocket, address, "Centinel is up to date.", aes_secret)
		    
	    elif com == "listclients":
		send_aes_crypt(clientsocket, address, "Connected clients: ", aes_secret)
		for client, (lasttime, lastaddress) in self.client_last_seen.items():
		    if lasttime <> "":
		        if datetime.now() - lasttime < timedelta(seconds=60):
			    send_aes_crypt(clientsocket, address, "%s\t%s\t\t%s(%d seconds ago)\t\t%s" %(client, lastaddress, lasttime.strftime("%Y-%m-%d %H:%M:%S"), (datetime.now() - lasttime).seconds, (geolocate(lastaddress)[0]+", "+geolocate(lastaddress)[1]) if geolocate(lastaddress) else "" ), aes_secret)
		    
		send_aes_crypt(clientsocket, address,  "Disconnected clients: ", aes_secret)
		for client, (lasttime, lastaddress) in self.client_last_seen.items():
		    if not lasttime or datetime.now() - lasttime >= timedelta(seconds=60):
		        send_aes_crypt(clientsocket, address,  "%s\t%s\t\t%s(%s seconds ago)\t\t%s" %(client, lastaddress, lasttime.strftime("%Y-%m-%d %H:%M:%S") if lasttime else "never", str((datetime.now() - lasttime).seconds) if lasttime else "infinite",(geolocate(lastaddress)[0]+", "+geolocate(lastaddress)[1]) if geolocate(lastaddress) else "" ), aes_secret)

	    elif len(com.split()) == 2 and com.split()[0] == "listresults":
		tag = com.split()[1]
		if not os.path.exists(os.path.join(conf['results_dir'], tag)):
		    send_aes_crypt(clientsocket, address,  "Client results directory not found.", aes_secret)
		else:
		    for path in glob.glob(os.path.join(conf['results_dir'], tag + '/*.json')):
			results = json.loads(open(path,'r').read())
			#pp = pprint.PrettyPrinter(indent=2)
			#results["std_http"][0]["response"]["body"] = base64.b64decode(results["std_http"][0]["response"]["body"])
			#pp.pprint(results)
			send_aes_crypt(clientsocket, address, (results["meta"]["exp_name"] + ":\t" + results["meta"]["run_id"] + "\t" + results["meta"]["local_time"]).encode("utf8"), aes_secret)

	    elif len(com.split()) == 4 and com.split()[0] == "printresults":
		tag = com.split()[1]
		experiment_name = com.split()[2]
		run_id = com.split()[3]
		if not os.path.exists(os.path.join(conf['results_dir'], tag)):
		    send_aes_crypt(clientsocket, address,  "Client results directory not found.", aes_secret)
		else:
		    found = False
		    for path in glob.glob(os.path.join(conf['results_dir'], tag + '/*.json')):
			results = json.loads(open(path,'r').read())
			if results["meta"]["exp_name"] == experiment_name and results["meta"]["run_id"] == run_id:
			    pp = pprint.PrettyPrinter(indent=2)
			    results["std_http"][0]["response"]["body"] = base64.b64decode(results["std_http"][0]["response"]["body"])
			    formatted = pp.pformat(results).replace('\\n', '\n')
			    send_aes_crypt(clientsocket, address, (formatted).encode("utf8"), aes_secret)
			    found = True
		    if not found:
			send_aes_crypt(clientsocket, address, "Experiment and run ID not found!" , aes_secret)
	    elif len(com.split()) > 2:
		tag, command_list = com.split(" ", 1);
		if tag in self.client_list and command_list <> "chill" and command_list:
		    if self.client_commands[tag] == "chill":
			self.client_commands[tag] = command_list
		    else:
			self.client_commands[tag] = self.client_commands[tag] + "; " + command_list
		    log("s", "Scheduled command list \"%s\" to be run on %s. (last seen %s at %s)" %(self.client_commands[tag],tag, self.client_last_seen[tag][0], self.client_last_seen[tag][1]), tag=tag)
		    send_aes_crypt(clientsocket, address, "Scheduled command list \"%s\" to be run on %s. (last seen %s at %s)" %(self.client_commands[tag],tag, self.client_last_seen[tag][0], self.client_last_seen[tag][1]), aes_secret)
		elif tag == "onall" and command_list <> "chill" and command_list:
		    for client in self.client_list:
			if self.client_commands[client] == "chill":
			    self.client_commands[client] = command_list
			else:
			    self.client_commands[client] = self.client_commands[client] + "; " + command_list
		    log("s", "Scheduled command list \"%s\" to be run on all clients." %(command_list))
		    send_aes_crypt(clientsocket, address, "Scheduled command list \"%s\" to be run on all clients." %(command_list), aes_secret)
	    else:
		send_aes_crypt(clientsocket, address, "Command \"%s\" not recognized." %(com), aes_secret)

	    send_aes_crypt(clientsocket, address, "<END_MESSAGE>", aes_secret)
	    return True
	except Exception as e:
	    log ("e", "Error handling Kobra command \"%s\": " %(com) + str(e))
	    return False