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