3
3
Microsoft SQL Server (MSSQL) is a relational database management system. Commonly used in conjunction with web applications
4
4
and other software that need to persist data. MSSQL is a useful target for data extraction and code execution.
5
5
6
- MySQL is frequently found on port on the following ports:
6
+ MSSQL is frequently found on port on the following ports:
7
7
8
8
- 1433/TCP
9
9
- 1434/UDP
10
10
11
+ For a full list of MSSQL modules run the ` search ` command within msfconsole:
12
+
13
+ ``` msf
14
+ msf6 > search mssql
15
+ ```
16
+
17
+ Or to search for modules that work with a specific session type:
18
+
19
+ ``` msf
20
+ msf6 > search session_type:mssql
21
+ ```
22
+
11
23
### Lab Environment
12
24
13
25
Environment setup:
@@ -26,6 +38,176 @@ use auxiliary/admin/mssql/mssql_sql
26
38
run rhost=192.168.123.13 username=administrator password=p4$$w0rd sql='select auth_scheme from sys.dm_exec_connections where session_id=@@spid'
27
39
```
28
40
41
+ ### Logging in and obtaining a session
42
+ To log in or obtain an interactive session on an MSSQL instance running on the target, use mssql_login
43
+
44
+ ``` msf
45
+ use auxiliary/scanner/mssql_login
46
+ run CreateSession=true RPORT=1433 RHOSTS=192.168.2.242 USERNAME=user PASSWORD=password
47
+ ```
48
+
49
+ The CreateSession option, when set to true, will result in returning an interactive MSSQL session with the target machine
50
+ on a successful login:
51
+
52
+ ``` msf
53
+ [*] 192.168.2.242:1433 - 192.168.2.242:1433 - MSSQL - Starting authentication scanner.
54
+ [!] 192.168.2.242:1433 - No active DB -- Credential data will not be saved!
55
+ [+] 192.168.2.242:1433 - 192.168.2.242:1433 - Login Successful: WORKSTATION\user:password
56
+ [*] MSSQL session 1 opened (192.168.2.1:60963 -> 192.168.2.242:1433) at 2024-03-15 13:41:31 -0500
57
+ [*] 192.168.2.242:1433 - Scanned 1 of 1 hosts (100% complete)
58
+ [*] Auxiliary module execution completed
59
+ ```
60
+
61
+ Which you can interact with using ` sessions -i <session id> ` or ` sessions -i -1 ` to interact with the most recently opened session.
62
+
63
+ ``` msf
64
+ msf6 auxiliary(scanner/mssql/mssql_login) > sessions
65
+
66
+ Active sessions
67
+ ===============
68
+
69
+ Id Name Type Information Connection
70
+ -- ---- ---- ----------- ----------
71
+ 1 mssql MSSQL test @ 192.168.2.242:1433 192.168.2.1:60963 -> 192.168.23.242:1433 (192.168.2.242)
72
+
73
+ msf6 auxiliary(scanner/mssql/mssql_login) > sessions -i 1
74
+ [*] Starting interaction with 1...
75
+
76
+ mssql @ 192.168.2.242:1433 (master) > query 'select @@version;'
77
+ Response
78
+ ========
79
+
80
+ # NULL
81
+ - ----
82
+ 0 Microsoft SQL Server 2022 (RTM) - 16.0.1000.6 (X64)
83
+ Oct 8 2022 05:58:25
84
+ Copyright (C) 2022 Microsoft Corporation
85
+ Developer Edition (64-bit) on Windows Server 2022 Stand
86
+ ard 10.0 <X64> (Build 20348: ) (Hypervisor)
87
+ ```
88
+
89
+ When interacting with a session, the help command can be useful:
90
+
91
+ ``` msf
92
+ mssql @ 192.168.2.242:1433 (master) > help
93
+
94
+ Core Commands
95
+ =============
96
+
97
+ Command Description
98
+ ------- -----------
99
+ ? Help menu
100
+ background Backgrounds the current session
101
+ bg Alias for background
102
+ exit Terminate the PostgreSQL session
103
+ help Help menu
104
+ irb Open an interactive Ruby shell on the current session
105
+ pry Open the Pry debugger on the current session
106
+ sessions Quickly switch to another session
107
+
108
+
109
+ MSSQL Client Commands
110
+ =====================
111
+
112
+ Command Description
113
+ ------- -----------
114
+ query Run a single SQL query
115
+ query_interactive Enter an interactive prompt for running multiple SQL queries
116
+
117
+
118
+ Local File System Commands
119
+ ==========================
120
+
121
+ Command Description
122
+ ------- -----------
123
+ getlwd Print local working directory (alias for lpwd)
124
+ lcat Read the contents of a local file to the screen
125
+ lcd Change local working directory
126
+ ldir List local files (alias for lls)
127
+ lls List local files
128
+ lmkdir Create new directory on local machine
129
+ lpwd Print local working directory
130
+
131
+ This session also works with the following modules:
132
+
133
+ auxiliary/admin/mssql/mssql_enum
134
+ auxiliary/admin/mssql/mssql_escalate_dbowner
135
+ auxiliary/admin/mssql/mssql_escalate_execute_as
136
+ auxiliary/admin/mssql/mssql_exec
137
+ auxiliary/admin/mssql/mssql_findandsampledata
138
+ auxiliary/admin/mssql/mssql_idf
139
+ auxiliary/admin/mssql/mssql_sql
140
+ auxiliary/admin/mssql/mssql_sql_file
141
+ auxiliary/scanner/mssql/mssql_hashdump
142
+ auxiliary/scanner/mssql/mssql_schemadump
143
+ exploit/windows/mssql/mssql_payload
144
+ ```
145
+
146
+ To interact directly with the session as if in a SQL prompt, you can use the ` query ` command.
147
+
148
+ ``` msf
149
+ msf6 auxiliary(scanner/mssql/mssql_login) > sessions -i -1
150
+ [*] Starting interaction with 2...
151
+
152
+ mssql @ 192.168.2.242:1433 (master) > query -h
153
+ Usage: query
154
+
155
+ Run a single SQL query on the target.
156
+
157
+ OPTIONS:
158
+
159
+ -h, --help Help menu.
160
+ -i, --interact Enter an interactive prompt for running multiple SQL queries
161
+
162
+ Examples:
163
+
164
+ query select @@version;
165
+ query select user_name();
166
+ query select name from master.dbo.sysdatabases;
167
+
168
+ mssql @ 192.168.2.242:1433 (master) > query 'select @@version;'
169
+ Response
170
+ ========
171
+
172
+ # NULL
173
+ - ----
174
+ 0 Microsoft SQL Server 2022 (RTM) - 16.0.1000.6 (X64)
175
+ Oct 8 2022 05:58:25
176
+ Copyright (C) 2022 Microsoft Corporation
177
+ Developer Edition (64-bit) on Windows Server 2022 Standard 10.0 <X64> (B
178
+ uild 20348: ) (Hypervisor)
179
+ ```
180
+
181
+ Alternatively you can enter a SQL prompt via the ` query_interactive ` command which supports multiline commands:
182
+
183
+ ``` msf
184
+ mssql @ 192.168.2.242:1433 (master) > query_interactive -h
185
+ Usage: query_interactive
186
+
187
+ Go into an interactive SQL shell where SQL queries can be executed.
188
+ To exit, type 'exit', 'quit', 'end' or 'stop'.
189
+
190
+ mssql @ 192.168.2.242:1433 (master) > query_interactive
191
+ [*] Starting interactive SQL shell for mssql @ 192.168.2.242:1433 (master)
192
+ [*] SQL commands ending with ; will be executed on the remote server. Use the exit command to exit.
193
+
194
+ SQL >> select *
195
+ SQL *> from information_schema.tables
196
+ SQL *> where table_type = 'BASE TABLE';
197
+ [*] Executing query: select * from information_schema.tables where table_type = 'BASE TABLE';
198
+ Response
199
+ ========
200
+ # TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE
201
+ - ------------- ------------ ---------- ----------
202
+ 0 master dbo spt_fallback_db BASE TABLE
203
+ 1 master dbo spt_fallback_dev BASE TABLE
204
+ 2 master dbo spt_fallback_usg BASE TABLE
205
+ 4 master dbo Users BASE TABLE
206
+ 5 master dbo spt_monitor BASE TABLE
207
+ 6 master dbo MSreplication_options BASE TABLE
208
+ SQL >>
209
+ ```
210
+
29
211
### Link crawling
30
212
31
213
Identify if the SQL server has been configured with trusted links, which allows running queries on other MSSQL instances:
0 commit comments