Repository navigation
Expand file tree
/
Copy pathcheckMSSQLStatus.py
More file actions
154 lines (129 loc) · 5.36 KB
/
Copy pathcheckMSSQLStatus.py
File metadata and controls
154 lines (129 loc) · 5.36 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
#!/usr/bin/env python3
# coding: utf-8
"""
mssql_cbt_check.py
Check whether Channel Binding Token (CBT) is enforced on a MSSQL server.
Usage:
mssql_cbt_check.py [domain/]username[:password]@target [-port PORT] [-debug]
Writen by @Defte_
"""
import sys
import logging
import argparse
from getpass import getpass
from impacket import version
from impacket.examples import logger
from impacket.examples.utils import parse_target
from impacket.tds import MSSQL, TDS_ENCRYPT_REQ, TDS_ENCRYPT_OFF
class MSQLCBTCheck:
def __init__(self, options, username, password, domain, target):
self.username = username
self.password = password
self.domain = domain
self.target = target
self.port = int(options.port)
self.options = options
def _new_conn(self):
conn = MSSQL(self.target, self.port, "")
conn.connect()
return conn
def _login(self, conn, cbt):
opts = self.options
if opts.k:
return conn.kerberosLogin(
None,
self.username,
self.password,
self.domain,
opts.hashes,
opts.aesKey,
opts.dc_ip,
None,
None,
useCache=True,
cbt_fake_value=cbt,
)
else:
return conn.login(
None,
self.username,
self.password,
self.domain,
opts.hashes,
useWindowsAuth=True,
cbt_fake_value=cbt,
)
def run(self):
print(f"[*] Checking Channel Binding status on: {self.target}:{self.port}")
try:
conn = self._new_conn()
prelogin_resp = conn.preLogin()
enc = prelogin_resp["Encryption"]
if not enc == TDS_ENCRYPT_REQ and not enc == TDS_ENCRYPT_OFF:
print("[!] Encryption not activated nor required. Channel Binding off.")
conn.disconnect()
return
except Exception as e:
logging.debug(f"preLogin failed: {e}")
print("[-] Prelogin failed, cannot check MSSQL status.")
return
print("\n[*] First try: TDS computes the real Channel Binding Token (cbt=None)")
try:
conn = self._new_conn()
first_ok = self._login(conn, cbt=None)
conn.disconnect()
except Exception as e:
logging.debug(f"First try exception: {e}")
first_ok = False
print(f" Result: {'Success' if first_ok else 'Failure'}")
print("\n[*] Second try: invalid Channel Binding Token (cbt='')")
try:
conn = self._new_conn()
second_ok = self._login(conn, cbt=b'')
conn.disconnect()
except Exception as e:
logging.debug(f"Second try exception: {e}")
second_ok = False
print(f" Result: {'Success' if second_ok else 'Failure'}")
if first_ok and second_ok:
print("\n[+] The two authentications succeded. Channel Binding not required (CBT not enforced).")
elif first_ok and not second_ok:
print("\n[!] First authentication succeded, second failed. Channel Binding required (CBT enforced).")
elif not first_ok and not second_ok:
print("\n[!] The two authentications failed, invalid credentials.")
if __name__ == '__main__':
print(version.BANNER)
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument('target', action='store', help='[[domain/]username[:password]@]<targetName or address>')
parser.add_argument('-port', default=1433, help='Port MSSQL (default: 1433)')
parser.add_argument('-debug', action='store_true', help='Turn DEBUG output ON')
parser.add_argument('-ts', action='store_true', help='Adds timestamp to every logging output', dest='timestamp')
group = parser.add_argument_group('authentication')
group.add_argument('-hashes', metavar='LMHASH:NTHASH', help='NTLM hashes')
group.add_argument('-no-pass', action='store_true', help="Don't ask for password (useful with -k)")
group.add_argument('-k', action='store_true', help='Use Kerberos authentication (ccache via KRB5CCNAME)')
group.add_argument('-aesKey', metavar='hex key', help='AES key for Kerberos (128 or 256 bits)')
group = parser.add_argument_group('connection')
group.add_argument('-dc-ip', metavar='ip address', help='IP of the domain controller')
group.add_argument('-target-ip', metavar='ip address', help='IP of the target (overrides target name resolution)')
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
options = parser.parse_args()
logger.init(options.timestamp, options.debug)
domain, username, password, target = parse_target(options.target)
if domain is None:
domain = ''
if options.target_ip:
target = options.target_ip
if options.aesKey:
options.k = True
if password == '' and username != '' and not options.hashes and not options.no_pass and not options.aesKey:
password = getpass("Password: ")
try:
MSQLCBTCheck(options, username, password, domain, target).run()
except Exception as e:
if logging.getLogger().level == logging.DEBUG:
import traceback
traceback.print_exc()
logging.error(str(e))