forked from elastic/detection-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistent_scripts.py
62 lines (45 loc) · 1.77 KB
/
persistent_scripts.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
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
# Name: Persistent Scripts
# RTA: persistent_scripts.py
# ATT&CK: T1064 (Scripting), T1086 (PowerShell)
import os
import time
from . import common
VBS = common.get_path("bin", "persistent_script.vbs")
NAME = "rta-vbs-persistence"
@common.requires_os(common.WINDOWS)
@common.dependencies(common.PS_EXEC, VBS)
def main():
common.log("Persistent Scripts")
if common.check_system():
common.log("Must be run as a non-SYSTEM user", log_type="!")
return 1
# Remove any existing profiles
user_profile = os.environ['USERPROFILE']
log_file = os.path.join(user_profile, NAME + ".log")
# Remove log file if exists
common.remove_file(log_file)
common.log("Running VBS")
common.execute(["cscript.exe", VBS])
# Let the script establish persistence, then read the log file back
time.sleep(5)
common.print_file(log_file)
common.remove_file(log_file)
# Now trigger a 'logon' event which causes persistence to run
common.log("Simulating user logon and loading of profile")
# common.execute(["taskkill.exe", "/f", "/im", "explorer.exe"])
# time.sleep(2)
common.execute(["C:\\Windows\\System32\\userinit.exe"], wait=True)
common.execute(["schtasks.exe", "/run", "/tn", NAME])
# Wait for the "logon" to finish
time.sleep(30)
common.print_file(log_file)
# Now delete the user profile
common.log("Cleanup", log_type="-")
common.remove_file(log_file)
common.execute(["schtasks.exe", "/delete", "/tn", NAME, "/f"])
if __name__ == "__main__":
exit(main())