Skip to content

Commit b9d0f25

Browse files
authored
Merge pull request #6200 from projectdiscovery/msssql-exec-query-support
feat: added support to mssql for execute query
2 parents cbf57ef + 088425d commit b9d0f25

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

pkg/js/generated/ts/mssql.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,33 @@ export class MSSQLClient {
6363
}
6464

6565

66+
/**
67+
* ExecuteQuery connects to MS SQL database using given credentials and executes a query.
68+
* It returns the results of the query or an error if something goes wrong.
69+
* @example
70+
* ```javascript
71+
* const mssql = require('nuclei/mssql');
72+
* const client = new mssql.MSSQLClient;
73+
* const result = client.ExecuteQuery('acme.com', 1433, 'username', 'password', 'master', 'SELECT @@version');
74+
* log(to_json(result));
75+
* ```
76+
*/
77+
public ExecuteQuery(host: string, port: number, username: string): SQLResult | null | null {
78+
return null;
79+
}
80+
81+
82+
}
83+
84+
85+
86+
/**
87+
* SQLResult Interface
88+
*/
89+
export interface SQLResult {
90+
91+
Count?: number,
92+
93+
Columns?: string[],
6694
}
6795

pkg/js/libs/mssql/mssql.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
_ "github.com/microsoft/go-mssqldb"
1313
"github.com/praetorian-inc/fingerprintx/pkg/plugins/services/mssql"
14+
"github.com/projectdiscovery/nuclei/v3/pkg/js/utils"
1415
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
1516
)
1617

@@ -132,3 +133,61 @@ func isMssql(host string, port int) (bool, error) {
132133
}
133134
return false, nil
134135
}
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

Comments
 (0)