forked from EmpireProject/Empire
-
-
Notifications
You must be signed in to change notification settings - Fork 580
/
data_util.py
88 lines (67 loc) · 2.46 KB
/
data_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import logging
import socket
import subprocess
from empire.server.core.db import models
from empire.server.core.db.base import SessionLocal
log = logging.getLogger(__name__)
def get_config(fields):
"""
Helper to pull common database config information outside of the
normal menu execution.
Fields should be comma separated.
i.e. 'version,install_path'
"""
with SessionLocal.begin() as db:
results = []
config = db.query(models.Config).first()
for field in fields.split(","):
results.append(config[field.strip()])
return results
def get_listener_options(listener_name):
"""
Returns the options for a specified listenername from the database outside
of the normal menu execution.
"""
try:
with SessionLocal() as db:
listener_options = (
db.query(models.Listener.options)
.filter(models.Listener.name == listener_name)
.first()
)
return listener_options
except Exception:
return None
def is_powershell_installed():
return get_powershell_name() != ""
def get_powershell_name():
try:
subprocess.check_output("which powershell", shell=True)
except subprocess.CalledProcessError:
try:
subprocess.check_output("which pwsh", shell=True)
except subprocess.CalledProcessError:
return ""
return "pwsh"
return "powershell"
def convert_obfuscation_command(obfuscate_command):
return "".join(obfuscate_command.split()).replace(",", ",home,").replace("\\", ",")
def ps_convert_to_oneliner(psscript):
"""
Converts a PowerShell script to a one-liner.
"""
psscript = psscript.replace('"kernel32"', '`"kernel32`"')
psscript = psscript.replace('"Kernel32.dll"', '`"Kernel32.dll`"')
psscript = psscript.replace('"RtlMoveMemory"', '`"RtlMoveMemory`"')
psscript = psscript.replace('"amsi.dll"', '`"amsi.dll`"')
psscript = psscript.replace('"Amsi"', '`"Amsi`"')
psscript = psscript.replace('"Scan"', '`"Scan`"')
psscript = psscript.replace('"Buffer"', '`"Buffer`"')
psscript = psscript.replace('@"', '"')
psscript = psscript.replace('"@', '"')
psscript = psscript.replace("\n", "")
psscript = psscript.replace(" ", "")
return psscript
def is_port_in_use(port: int) -> bool:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(("localhost", port)) == 0