-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices_tasks.py
More file actions
214 lines (174 loc) · 6.88 KB
/
services_tasks.py
File metadata and controls
214 lines (174 loc) · 6.88 KB
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
# =============================================================================
# DumpSec-Py - Windows Security Auditing Tool
# =============================================================================
#
# Author: Keith Pachulski
# Company: Red Cell Security, LLC
# Email: keith@redcellsecurity.org
# Website: www.redcellsecurity.org
#
# Copyright (c) 2025 Keith Pachulski. All rights reserved.
#
# License: This software is licensed under the MIT License.
# You are free to use, modify, and distribute this software
# in accordance with the terms of the license.
#
# Purpose: This script is part of the DumpSec-Py tool, which is designed to
# perform detailed security audits on Windows systems. It covers
# user rights, services, registry permissions, file/share permissions,
# group policy enumeration, risk assessments, and more.
#
# DISCLAIMER: This software is provided "as-is," without warranty of any kind,
# express or implied, including but not limited to the warranties
# of merchantability, fitness for a particular purpose, and non-infringement.
# In no event shall the authors or copyright holders be liable for any claim,
# damages, or other liability, whether in an action of contract, tort, or otherwise,
# arising from, out of, or in connection with the software or the use or other dealings
# in the software.
#
# =============================================================================
import win32service
import win32serviceutil
import win32con
import win32api
import win32security
import win32com.client
from risk_engine import RiskEngine
risk = RiskEngine()
def get_services():
services = []
risks = []
try:
scm_handle = win32service.OpenSCManager(
None,
None,
win32service.SC_MANAGER_ENUMERATE_SERVICE
)
statuses = win32service.EnumServicesStatus(
scm_handle,
win32service.SERVICE_WIN32,
win32service.SERVICE_STATE_ALL
)
for (name, display_name, status) in statuses:
try:
svc_handle = win32service.OpenService(scm_handle, name, win32service.SC_MANAGER_ALL_ACCESS)
config = win32service.QueryServiceConfig(svc_handle)
start_type = decode_start_type(config[1])
logon_user = config[7]
binary_path = config[3]
current_state = "Running" if status[1] == win32service.SERVICE_RUNNING else "Stopped"
services.append({
"Service Name": name,
"Display Name": display_name,
"Status": current_state,
"Start Type": start_type,
"Logon User": logon_user,
"Binary Path": binary_path
})
risks.extend(risk.evaluate_service(name, display_name, binary_path, logon_user))
except Exception as e:
services.append({
"Service Name": name,
"Display Name": display_name,
"Status": "<error>",
"Start Type": "<error>",
"Logon User": f"<error: {e}>"
})
except Exception as e:
services.append({"Error": str(e)})
return services, risks
def decode_start_type(code):
return {
win32service.SERVICE_AUTO_START: "Automatic",
win32service.SERVICE_DEMAND_START: "Manual",
win32service.SERVICE_DISABLED: "Disabled"
}.get(code, f"Unknown ({code})")
def get_scheduled_tasks():
tasks = []
risks = []
try:
scheduler = win32com.client.Dispatch("Schedule.Service")
scheduler.Connect()
folders = [scheduler.GetFolder("\\")]
while folders:
folder = folders.pop()
for task in folder.GetTasks(0):
name = task.Name
try:
definition = task.Definition
user_id = definition.Principal.UserId
executable = None
for action in definition.Actions:
if action.Type == 0: # TASK_ACTION_EXEC
executable = action.Path
break
tasks.append({
"Task Name": name,
"Run As": user_id,
"Executable": executable or "<none>"
})
risks.extend(risk.evaluate_task(name, user_id, executable))
except Exception as e:
tasks.append({
"Task Name": name,
"Run As": f"<error: {e}>",
"Executable": "<error extracting action>"
})
folders.extend(folder.GetFolders(0))
except Exception as e:
tasks.append({"Error": str(e)})
return tasks, risks
def detect_hidden_tasks():
hidden_tasks = []
risks = []
try:
scheduler = win32com.client.Dispatch("Schedule.Service")
scheduler.Connect()
folders = [scheduler.GetFolder("\\")]
while folders:
folder = folders.pop()
for task in folder.GetTasks(0):
try:
if task.Hidden:
name = task.Name
path = task.Path
run_as = task.Definition.Principal.UserId
hidden_tasks.append(f"{name} (Path: {path}, RunAs: {run_as})")
risks.append({
"severity": "medium",
"category": "Hidden Scheduled Task",
"description": f"Hidden task '{name}' is configured to run as '{run_as}'"
})
except Exception:
# Silently skip error unless task.Hidden was confirmed
continue
folders.extend(folder.GetFolders(0))
except Exception as e:
# Only report global failure of querying scheduled tasks
risks.append({
"severity": "high",
"category": "Hidden Scheduled Task",
"description": f"Failed to query scheduled tasks: {e}"
})
result = {}
if hidden_tasks:
result["Hidden Scheduled Tasks"] = hidden_tasks
result["_risks"] = risks
return result
def run():
services, service_risks = get_services()
tasks, task_risks = get_scheduled_tasks()
hidden = detect_hidden_tasks()
results = {
"Services": services,
"Scheduled Tasks": tasks,
"_risks": service_risks + task_risks
}
results.update(hidden)
results["_risks"].extend(hidden.get("_risks", []))
return results
def main():
get_services()
get_scheduled_tasks()
if __name__ == "__main__":
main()