@@ -11,6 +11,7 @@ import (
11
11
12
12
_ "github.com/microsoft/go-mssqldb"
13
13
"github.com/praetorian-inc/fingerprintx/pkg/plugins/services/mssql"
14
+ "github.com/projectdiscovery/nuclei/v3/pkg/js/utils"
14
15
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
15
16
)
16
17
@@ -132,3 +133,61 @@ func isMssql(host string, port int) (bool, error) {
132
133
}
133
134
return false , nil
134
135
}
136
+
137
+ // ExecuteQuery connects to MS SQL database using given credentials and executes a query.
138
+ // It returns the results of the query or an error if something goes wrong.
139
+ // @example
140
+ // ```javascript
141
+ // const mssql = require('nuclei/mssql');
142
+ // const client = new mssql.MSSQLClient;
143
+ // const result = client.ExecuteQuery('acme.com', 1433, 'username', 'password', 'master', 'SELECT @@version');
144
+ // log(to_json(result));
145
+ // ```
146
+ func (c * MSSQLClient ) ExecuteQuery (host string , port int , username , password , dbName , query string ) (* utils.SQLResult , error ) {
147
+ if host == "" || port <= 0 {
148
+ return nil , fmt .Errorf ("invalid host or port" )
149
+ }
150
+ if ! protocolstate .IsHostAllowed (host ) {
151
+ // host is not valid according to network policy
152
+ return nil , protocolstate .ErrHostDenied .Msgf (host )
153
+ }
154
+
155
+ target := net .JoinHostPort (host , fmt .Sprintf ("%d" , port ))
156
+
157
+ ok , err := c .IsMssql (host , port )
158
+ if err != nil {
159
+ return nil , err
160
+ }
161
+ if ! ok {
162
+ return nil , fmt .Errorf ("not a mssql service" )
163
+ }
164
+
165
+ connString := fmt .Sprintf ("sqlserver://%s:%s@%s?database=%s&connection+timeout=30" ,
166
+ url .PathEscape (username ),
167
+ url .PathEscape (password ),
168
+ target ,
169
+ dbName )
170
+
171
+ db , err := sql .Open ("sqlserver" , connString )
172
+ if err != nil {
173
+ return nil , err
174
+ }
175
+ defer db .Close ()
176
+
177
+ db .SetMaxOpenConns (1 )
178
+ db .SetMaxIdleConns (0 )
179
+
180
+ rows , err := db .Query (query )
181
+ if err != nil {
182
+ return nil , err
183
+ }
184
+
185
+ data , err := utils .UnmarshalSQLRows (rows )
186
+ if err != nil {
187
+ if data != nil && len (data .Rows ) > 0 {
188
+ return data , nil
189
+ }
190
+ return nil , err
191
+ }
192
+ return data , nil
193
+ }
0 commit comments