@@ -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,53 @@ 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
+ connString := fmt .Sprintf ("sqlserver://%s:%s@%s?database=%s&connection+timeout=30" ,
158
+ url .PathEscape (username ),
159
+ url .PathEscape (password ),
160
+ target ,
161
+ dbName )
162
+
163
+ db , err := sql .Open ("sqlserver" , connString )
164
+ if err != nil {
165
+ return nil , err
166
+ }
167
+ defer db .Close ()
168
+
169
+ db .SetMaxOpenConns (1 )
170
+ db .SetMaxIdleConns (0 )
171
+
172
+ rows , err := db .Query (query )
173
+ if err != nil {
174
+ return nil , err
175
+ }
176
+
177
+ data , err := utils .UnmarshalSQLRows (rows )
178
+ if err != nil {
179
+ if data != nil && len (data .Rows ) > 0 {
180
+ return data , nil
181
+ }
182
+ return nil , err
183
+ }
184
+ return data , nil
185
+ }
0 commit comments