-
Notifications
You must be signed in to change notification settings - Fork 8
/
tallgrass.py
337 lines (274 loc) · 14.1 KB
/
tallgrass.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
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env python3
#
# TallGrass
# By chdav
# Credit to Impacket (by SecureAuth) + their examples
import argparse
import sys
import time
from colorama import init, Fore, Style
from impacket.dcerpc.v5 import transport, rrp, scmr, samr
from impacket.system_errors import ERROR_NO_MORE_ITEMS
from impacket.smbconnection import SMBConnection
from impacket.dcerpc.v5.rpcrt import DCERPCException
from impacket.nt_errors import STATUS_MORE_ENTRIES
class HostEnumeration:
def __init__(self, username, password, domain, hashes=None):
self.__username = username
self.__password = password
self.__domain = domain
self.__lmhash = ''
self.__nthash = ''
self.__machinesList = list()
if hashes is not None:
self.__lmhash, self.__nthash = hashes.split(':')
def run(self):
domainController = self.__domain
print("[" + Fore.GREEN + "Info" + Fore.RESET + "] Retrieving host list from " + Fore.CYAN + domainController)
rpctransport = transport.SMBTransport(domainController, 445, r'\samr', self.__username, self.__password,
self.__domain, self.__lmhash, self.__nthash)
dce = rpctransport.get_dce_rpc()
try:
dce.connect()
dce.bind(samr.MSRPC_UUID_SAMR)
except:
print("[" + Fore.RED + "Error" + Fore.RESET + "] DC unreachable")
exit()
try:
resp = samr.hSamrConnect(dce)
serverHandle = resp['ServerHandle']
resp = samr.hSamrEnumerateDomainsInSamServer(dce, serverHandle)
domains = resp['Buffer']['Buffer']
resp = samr.hSamrLookupDomainInSamServer(dce, serverHandle,domains[0]['Name'] )
resp = samr.hSamrOpenDomain(dce, serverHandle = serverHandle, domainId = resp['DomainId'])
domainHandle = resp['DomainHandle']
status = STATUS_MORE_ENTRIES
enumerationContext = 0
while status == STATUS_MORE_ENTRIES:
try:
resp = samr.hSamrEnumerateUsersInDomain(dce, domainHandle, samr.USER_WORKSTATION_TRUST_ACCOUNT,
enumerationContext=enumerationContext)
except DCERPCException as e:
if str(e).find('STATUS_MORE_ENTRIES') < 0:
raise
resp = e.get_packet()
for user in resp['Buffer']['Buffer']:
self.__machinesList.append(user['Name'][:-1])
enumerationContext = resp['EnumerationContext']
status = resp['ErrorCode']
except Exception:
print("[" + Fore.RED + "Error" + Fore.RESET + "] Unable to retrieve host list")
dce.disconnect()
return self.__machinesList
class RemoteConnections:
def __init__(self, smbConnection):
self.__smbConnection = smbConnection
self.__smbConnection.setTimeout(5 * 60)
self.__serviceName = 'RemoteRegistry'
self.__stringBindingWinReg = r'ncacn_np:445[\pipe\winreg]'
self.__rrp = None
self.__disabled = False
self.__shouldStop = False
self.__started = False
self.__stringBindingSvcCtl = r'ncacn_np:445[\pipe\svcctl]'
self.__scmr = None
def getRRP(self):
return self.__rrp
def __connectSvcCtl(self):
rpc = transport.DCERPCTransportFactory(self.__stringBindingSvcCtl)
rpc.set_smb_connection(self.__smbConnection)
self.__scmr = rpc.get_dce_rpc()
self.__scmr.connect()
self.__scmr.bind(scmr.MSRPC_UUID_SCMR)
def connectWinReg(self):
rpc = transport.DCERPCTransportFactory(self.__stringBindingWinReg)
rpc.set_smb_connection(self.__smbConnection)
self.__rrp = rpc.get_dce_rpc()
self.__rrp.connect()
self.__rrp.bind(rrp.MSRPC_UUID_RRP)
def __checkServiceStatus(self):
# Open SC Manager
ans = scmr.hROpenSCManagerW(self.__scmr)
self.__scManagerHandle = ans['lpScHandle']
# Open the service
ans = scmr.hROpenServiceW(self.__scmr, self.__scManagerHandle, self.__serviceName)
self.__serviceHandle = ans['lpServiceHandle']
# Check service status
ans = scmr.hRQueryServiceStatus(self.__scmr, self.__serviceHandle)
if ans['lpServiceStatus']['dwCurrentState'] == scmr.SERVICE_STOPPED:
print("[" + Fore.YELLOW + "Caution" + Fore.RESET + "] Service \'" + self.__serviceName + "\' is in stopped state")
self.__shouldStop = True
self.__started = False
elif ans['lpServiceStatus']['dwCurrentState'] == scmr.SERVICE_RUNNING:
self.__shouldStop = False
self.__started = True
else:
print("[" + Fore.RED + "Error" + Fore.RESET + "] Unknown service state 0x" + ans['CurrentState'] + " - Aborting")
# Check service configuration to see if service is stopped
if self.__started is False:
ans = scmr.hRQueryServiceConfigW(self.__scmr, self.__serviceHandle)
if ans['lpServiceConfig']['dwStartType'] == 0x4:
print("[" + Fore.YELLOW + "Caution" + Fore.RESET + "] Service \'" + self.__serviceName + "\' is disabled, enabling it")
self.__disabled = True
scmr.hRChangeServiceConfigW(self.__scmr, self.__serviceHandle, dwStartType=0x3)
print("[" + Fore.GREEN + "Info" + Fore.RESET + "] Starting service \'" + self.__serviceName + "\'")
scmr.hRStartServiceW(self.__scmr, self.__serviceHandle)
time.sleep(1)
def enableRegistry(self):
self.__connectSvcCtl()
self.__checkServiceStatus()
self.connectWinReg()
def __restore(self):
# Restores service to original state
if self.__shouldStop is True:
print("[" + Fore.GREEN + "Info" + Fore.RESET + "] Stopping service \'" + self.__serviceName + "\'")
scmr.hRControlService(self.__scmr, self.__serviceHandle, scmr.SERVICE_CONTROL_STOP)
if self.__disabled is True:
print("[" + Fore.GREEN + "Info" + Fore.RESET + "] Restoring disabled state for service \'" + self.__serviceName + "\'")
scmr.hRChangeServiceConfigW(self.__scmr, self.__serviceHandle, dwStartType=0x4)
def finish(self):
self.__restore()
if self.__rrp is not None:
self.__rrp.disconnect()
if self.__scmr is not None:
self.__scmr.disconnect()
class RegistryHandler:
def __init__(self, username, password, domain, rootKeys, subKeys):
self.__username = username
self.__password = password
self.__domain = domain
self.__rootKeys= rootKeys
self.__subKeys = subKeys
self.__port = '445'
self.__lmhash = ''
self.__nthash = ''
self.__smbConnection = None
self.__remoteConn = None
if args.n is not None:
self.__lmhash, self.__nthash = args.n.split(':')
def connect(self, remoteName, remoteHost):
self.__smbConnection = SMBConnection(remoteName, remoteHost, sess_port=int(self.__port))
self.__smbConnection.login(self.__username, self.__password, self.__domain, self.__lmhash, self.__nthash)
def run(self, remoteName, remoteHost):
try:
self.connect(remoteName, remoteHost)
self.__remoteConn = RemoteConnections(self.__smbConnection)
try:
self.__remoteConn.enableRegistry()
except:
print("[" + Fore.RED + "Error" + Fore.RESET + "] Cannot check RemoteRegistry status")
self.__remoteConn.connectWinReg()
print("[" + Fore.GREEN + "Info" + Fore.RESET + "] Enumerating antivirus products")
for av_product in self.__rootKeys:
try:
dce = self.__remoteConn.getRRP()
try:
self.query(dce, self.__rootKeys[av_product])
print(Style.BRIGHT + "[" + Fore.MAGENTA + "Antivirus" + Fore.RESET + "] " + av_product + " detected")
except:
continue
for subKey in self.__subKeys:
print("[" + Fore.GREEN + "Info" + Fore.RESET + "] "+ av_product + ": Querying \'" + subKey[1:] + "\' for exclusions")
self.query(dce, self.__rootKeys[av_product] + subKey)
except (Exception, KeyboardInterrupt) as e:
print("[" + Fore.RED + "Error" + Fore.RESET + "] " + str(e))
except:
print("[" + Fore.RED + "Error" + Fore.RESET + "] Unable to connect. Host may be offline.")
finally:
if self.__remoteConn:
self.__remoteConn.finish()
def query(self, dce, keyName):
try:
rootKey = keyName.split('\\')[0]
subKey = '\\'.join(keyName.split('\\')[1:])
except:
print("[" + Fore.RED + "Error" + Fore.RESET + "] Unable to parse keyName \'" + keyName + "\'")
if rootKey.upper() == 'HKLM':
ans = rrp.hOpenLocalMachine(dce)
elif rootKey.upper() == 'HKU':
ans = rrp.hOpenCurrentUser(dce)
elif rootKey.upper() == 'HKCR':
ans = rrp.hOpenClassesRoot(dce)
else:
print("[" + Fore.RED + "Error" + Fore.RESET + "] Invalid root key: " + rootKey)
hRootKey = ans['phKey']
ans2 = rrp.hBaseRegOpenKey(dce, hRootKey, subKey,
samDesired=rrp.MAXIMUM_ALLOWED | rrp.KEY_ENUMERATE_SUB_KEYS | rrp.KEY_QUERY_VALUE)
self.__print_key_values(dce, ans2['phkResult'])
def __print_key_values(self, rpc, keyHandler):
i = 0
while True:
try:
ans4 = rrp.hBaseRegEnumValue(rpc, keyHandler, i)
lp_value_name = ans4['lpValueNameOut'][:-1]
if len(lp_value_name) == 0:
lp_value_name = '(Default)'
lp_type = ans4['lpType']
lp_data = b''.join(ans4['lpData'])
print(Style.BRIGHT + "[" + Fore.GREEN + "Exclusion" + Fore.RESET + "] " + lp_value_name)
i += 1
except rrp.DCERPCSessionError as e:
if e.get_error_code() == ERROR_NO_MORE_ITEMS:
break
def banner():
print(Style.BRIGHT + Fore.GREEN + "\n ░")
print(Style.BRIGHT + Fore.GREEN + " ░░")
print(Style.BRIGHT + Fore.GREEN + " ░░")
print(Style.BRIGHT + Fore.GREEN + " ░ ░░ ░")
print(Style.BRIGHT + Fore.GREEN + " ░ ░░░░ ░░ ░")
print(Style.BRIGHT + Fore.CYAN + " _____ _ _ ___ " + Style.BRIGHT + Fore.GREEN + " ░░░░ ░░")
print(Style.BRIGHT + Fore.GREEN + " ▒" + Style.BRIGHT + Fore.CYAN + " |_ _|_ _| | |/ __|_ _ __ _ ______")
print(Style.BRIGHT + Fore.GREEN + " ▒" + Style.BRIGHT + Fore.CYAN + " | |/ _` | | | (_ | '_/ _` (_-<_-<")
print(Style.BRIGHT + Fore.GREEN + " ░" + Style.BRIGHT + Fore.CYAN + "|_|\__,_|_|_|\___|_| \__,_/__/__/" + Style.BRIGHT + Fore.GREEN + " ▒")
print(Style.BRIGHT + Fore.GREEN + " ▒▒ ░░ ▒▒ ▒▒ ░░ ▒▒ ▒▒ ░░")
print(Style.BRIGHT + Fore.GREEN + " ░░░░ ▒▒ ▒▒░░░░▒▒░░░░ ▓░░▓▓░░ ▒▒")
print(Style.BRIGHT + Fore.GREEN + " ▒▒▓▓▒▒▒▒▓▒▓▓▒▒▒▒▒▒▓▒▒▒▒▓▓▒▒▓▓▒▒▒▒▒░░\n")
print("\t TallGrass v0.0.1\n\n")
def args():
parser = argparse.ArgumentParser(add_help=True, description="Windows domain AV exclusion enumeration.")
group = parser.add_argument_group('required arguments')
group.add_argument("-u", metavar="USERNAME", action="store", required=True, help='Domain username')
group.add_argument("-d", metavar="DC FQDN", action="store", required=True, help='Target Domain Controller FQDN, I.E. dc-1.example.local')
group = parser.add_argument_group('authentication')
group.add_argument('-n', metavar="LMHASH:NTHASH", action="store", help='NTLM hashes, format is LMHASH:NTHASH')
group.add_argument('-p', metavar="PASSWORD", action="store", help="Cleartext password")
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
return parser.parse_args()
def start(username, password, hashes, domain_controller, av_keys, sub_keys):
domain_controller = domain_controller.upper()
dc_split = domain_controller.split(".")
domain_computers = list()
domain_computers.append(dc_split[0])
# Enumerate domain hosts
host_enum = HostEnumeration(username, password, domain_controller, hashes)
domain_computers.extend(host_enum.run())
print("[" + Fore.GREEN + "Info" + Fore.RESET + "] Found " + Fore.CYAN + str(len(domain_computers)) + Fore.RESET +" computers")
# For each host, check AV present and enumerate exclusions
for host in domain_computers:
remote_name = host + "." + ".".join(dc_split[1:])
print("\t" + Style.BRIGHT + Fore.CYAN + remote_name)
regHandler = RegistryHandler(username, password, dc_split[1], av_keys, sub_keys)
try:
regHandler.run(remote_name, remote_name)
except Exception as e:
print(str(e))
if __name__ == '__main__':
init(autoreset=True)
banner()
args = args()
username = args.u
password = args.p
hashes = args.n
domain_controller = args.d
### Add paths for other AVs here
defender_root_key = "HKLM\\Software\\Microsoft\\Windows Defender\\Exclusions"
essentials_root_key = "HKLM\\SOFTWARE\\Microsoft\\Microsoft Antimalware\\Exclusions"
av_keys = {"Windows Defender":defender_root_key, "Microsoft Essentials":essentials_root_key}
sub_keys = ["\\Paths", "\\Processes", "\\Extensions"]
###
if password is None and hashes is None:
from getpass import getpass
password = getpass("Password:")
start(username, password, hashes, domain_controller, av_keys, sub_keys)