This repository has been archived by the owner on Nov 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RetriveStartupProgramsClass.py
172 lines (147 loc) · 5.97 KB
/
RetriveStartupProgramsClass.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
from winreg import *
import os
from CleanMemoryClass import CleanMemory
class StartupProgramsRetrival(CleanMemory):
def __init__(self):
super().__init__()
self.common_extensions = ['.exe ', '.vbs ', '.bat ', '.dll ', '.sys ']
self.startup_programs = []
self.startup_services = []
self.startup_drivers = []
self.scheduled_tasks = []
self.final = []
def startup_programs_retrieval(self):
self.retrieve_startup_programs()
self.retrieve_startup_services()
self.retrieve_scheduled_tasks()
def process_data(data):
if '%' in data:
item = data.split('\\')
item[0] = os.path.expandvars(item[0])
self.final.append('\\'.join(item))
else:
self.final.append(data)
for i in self.startup_programs:
process_data(i)
for i in self.startup_services:
process_data(i)
for i in self.scheduled_tasks:
process_data(i)
return self.final
def retrieve_scheduled_tasks(self):
output = self.cmd_pipe('schtasks /query /fo LIST /v')
output = output.split('HostName:')
for i in output:
i = i.split('Task To Run: ')[-1]
path = i.split('Start In:')[0].strip()
path = path.strip('"')
if path != 'COM handler':
if path != 'Folder: \\':
for extn in self.common_extensions:
if extn in path:
extn_ = extn.rstrip(' ')
path = path.split(extn)[0] + extn_
if len(path.split('\\')) > 1:
if path not in self.scheduled_tasks:
self.scheduled_tasks.append(path)
def retrieve_startup_programs(self):
reg_paths = [r'SOFTWARE\Microsoft\Windows\CurrentVersion\Run',
r'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run']
def process_data(data):
data = str(data)
try:
if len(data) > 0:
data = data.strip('"')
data = data.split('"')[0]
self.startup_programs.append(data)
except EnvironmentError:
pass
def enum_vals(aKey):
for i in range(1024):
try:
n, path, t = EnumValue(aKey, i)
process_data(path)
except EnvironmentError:
break
aReg = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
for i in reg_paths:
try:
aKey = OpenKey(aReg, i)
except BaseException as e:
print(i, ':', e)
enum_vals(aKey)
CloseKey(aKey)
return self.startup_programs
def retrieve_startup_services(self):
reg_path = r'System\CurrentControlSet\Services'
def process_data_services(data):
data = str(data)
try:
if '.' in data:
if '\\' in data:
data = data.split(',')[0]
data = data.split(' -')[0]
data = data.split('//')[0]
data = data.rstrip('"')
data = data.lstrip('"')
data = data.lstrip('@')
data = data.split('" /')[0]
data = data.lstrip('\\')
for extn in self.common_extensions:
if extn in data:
extn_ = extn.rstrip(' ')
data = data.split(extn)[0] + extn_
data_list = data.split('\\')
if len(data_list) > 1:
if 'system32' not in data_list:
if 'System32' not in data_list:
if data.split("'")[0] != 'b':
if data not in self.startup_services:
self.startup_services.append(data)
if 'drivers' in data_list:
self.startup_drivers.append(data)
except:
pass
def enum_service(aKey, aReg):
for i in range(1024):
try:
service_key = EnumKey(aKey, i)
for j in range(1024):
try:
full_path = reg_path + r'\\' + service_key
aKey_2 = OpenKey(aReg, full_path)
n, data, t = EnumValue(aKey_2, j)
CloseKey(aKey_2)
process_data_services(data)
except EnvironmentError:
break
except EnvironmentError:
pass
aReg = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
try:
aKey = OpenKey(aReg, reg_path)
except BaseException as e:
print(i, ':', e)
return False
enum_service(aKey, aReg)
CloseKey(aKey)
return self.startup_services
if __name__ == '__main__':
s = StartupProgramsRetrial()
for i in s.startup_programs_retrieval():
print(i)
#s.retrieve_startup_programs()
s.retrieve_startup_services()
#s.retrieve_scheduled_tasks()
#print('--scheduledTasks--')
#for i in s.scheduled_tasks:
# print(i)
#print('--startupServices--')
#for i in s.startup_services:
# print(i)
#print('---startupPrograms--')
#for i in s.startup_programs:
# print(i)
#print('--startupDrivers--')
#for i in s.startup_drivers:
# print(i)