#!/usr/bin/python # Use this script to send CNC packets to a CNC Listener to test out functionality import socket import sys import getopt from threading import Thread import bson import time SEND_IP="127.0.0.1" SEND_PORT=8124 IFACE='wlan0' PERIOD=1 def debugOut(str): DEBUG = True if DEBUG: print str ''' takes a dictionary and sends the BSON representation to SEND_IP:SEND_PORT ''' def sendCNC(dict, sock): c = bson.dumps(dict) b = bson.loads(c) debugOut('\nsending to %s:%s' % (SEND_IP, SEND_PORT)) debugOut('packet: %s' % b) sock.sendto(c,(SEND_IP, SEND_PORT)) # Send periodic heartbeat packets class CNCSenderThread(Thread): def __init__(self, iface=IFACE, ip = SEND_IP, port = SEND_PORT, period = PERIOD): Thread.__init__(self, name='Sender') self.iface = iface self.ip = ip self.port = port self.period = period self.done = False def stop(self): self.done = 1; def run(self): debugOut("Monitoring nodes from %s" % self.iface) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) while not self.done: sendCNC( { "response_ip": "127.0.0.1", "response_port": 8123, "cmd":"traceroute", "args":{"dest": "http://www.google.com"} }, sock) time.sleep(self.period) def main(): sender = CNCSenderThread() sender.start() #stay in a while loop until killed try: while 1: pass except KeyboardInterrupt: pass sender.stop() if __name__ == '__main__': main()