forked from knownsec/ksubdomain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.go
269 lines (243 loc) · 6.65 KB
/
script.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package core
import (
"crypto/tls"
"errors"
lua "github.com/yuin/gopher-lua"
"io"
"io/ioutil"
luajson "layeh.com/gopher-json"
"net/http"
"net/http/cookiejar"
"regexp"
"strings"
"time"
)
const (
// UserAgent is the default user agent used by Amass during HTTP requests.
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"
// Accept is the default HTTP Accept header value used by Amass.
Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
// AcceptLang is the default HTTP Accept-Language header value used by Amass.
AcceptLang = "en-US,en;q=0.8"
defaultTLSConnectTimeout = 3 * time.Second
defaultHandshakeDeadline = 5 * time.Second
SUBRE = "(([a-zA-Z0-9]{1}|[_a-zA-Z0-9]{1}[_a-zA-Z0-9-]{0,61}[a-zA-Z0-9]{1})[.]{1})+"
)
func getStringField(L *lua.LState, t lua.LValue, key string) (string, bool) {
lv := L.GetField(t, key)
if s, ok := lv.(lua.LString); ok {
return string(s), true
}
return "", false
}
// SubdomainRegexString returns a regular expression string that matchs
// subdomain names ending with the domain provided by the parameter.
func SubdomainRegexString(domain string) string {
// Change all the periods into literal periods for the regex
return SUBRE + strings.Replace(domain, ".", "[.]", -1)
}
func SubdomainRegex(domain string) *regexp.Regexp {
return regexp.MustCompile(SubdomainRegexString(domain))
}
// AnySubdomainRegex returns a Regexp object initialized to match any DNS subdomain name.
func AnySubdomainRegex() *regexp.Regexp {
return regexp.MustCompile(AnySubdomainRegexString())
}
// AnySubdomainRegexString returns a regular expression string to match any DNS subdomain name.
func AnySubdomainRegexString() string {
return SUBRE + "[a-zA-Z]{2,61}"
}
// Wrapper so that scripts can scrape the contents of a GET request for subdomain names in scope.
func scrape(L *lua.LState) int {
opt := L.CheckTable(1)
//
var body io.Reader
if method, ok := getStringField(L, opt, "method"); ok && (method == "POST" || method == "post") {
if data, ok := getStringField(L, opt, "data"); ok {
body = strings.NewReader(data)
}
}
//
url, found := getStringField(L, opt, "url")
if !found {
L.Push(lua.LNil)
L.Push(lua.LString("No URL found in the parameters"))
return 2
}
//
headers := make(map[string]string)
lv := L.GetField(opt, "headers")
if tbl, ok := lv.(*lua.LTable); ok {
tbl.ForEach(func(k, v lua.LValue) {
headers[k.String()] = v.String()
})
}
//
id, _ := getStringField(L, opt, "id")
pass, _ := getStringField(L, opt, "pass")
//
page, err := RequestWebPage(url, body, headers, id, pass)
if err != nil {
L.Push(lua.LNil)
return 1
}
//
domain := L.ToString(2)
var res []string
if domain == "" {
for _, name := range AnySubdomainRegex().FindAllString(page, -1) {
res = append(res, name)
}
} else {
// SUBRE is a regular expression that will match on all subdomains once the domain is appended.
for _, name := range SubdomainRegex(domain).FindAllString(page, -1) {
res = append(res, name)
}
}
t := L.NewTable()
for _, v := range res {
t.Append(lua.LString(v))
}
// 将返货结果堆栈
L.Push(t)
return 1
}
func RequestWebPage(urlstring string, body io.Reader, hvals map[string]string, uid, secret string) (string, error) {
method := "GET"
if body != nil {
method = "POST"
}
req, err := http.NewRequest(method, urlstring, body)
if err != nil {
return "", err
}
if uid != "" && secret != "" {
req.SetBasicAuth(uid, secret)
}
req.Header.Set("User-Agent", UserAgent)
req.Header.Set("Accept", Accept)
req.Header.Set("Accept-Language", AcceptLang)
for k, v := range hvals {
req.Header.Set(k, v)
}
jar, _ := cookiejar.New(nil)
var DefaultClient *http.Client
DefaultClient = &http.Client{
Timeout: time.Second * 180, // Google's timeout
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
MaxIdleConns: 200,
MaxConnsPerHost: 50,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 20 * time.Second,
ExpectContinueTimeout: 20 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Jar: jar,
}
resp, err := DefaultClient.Do(req)
if err != nil {
return "", err
}
in, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
err = errors.New(resp.Status)
}
return string(in), err
}
func request(L *lua.LState) int {
opt := L.CheckTable(1)
//
var body io.Reader
if method, ok := getStringField(L, opt, "method"); ok && (method == "POST" || method == "post") {
if data, ok := getStringField(L, opt, "data"); ok {
body = strings.NewReader(data)
}
}
//
url, found := getStringField(L, opt, "url")
if !found {
L.Push(lua.LNil)
L.Push(lua.LString("No URL found in the parameters"))
return 2
}
//
headers := make(map[string]string)
lv := L.GetField(opt, "headers")
if tbl, ok := lv.(*lua.LTable); ok {
tbl.ForEach(func(k, v lua.LValue) {
headers[k.String()] = v.String()
})
}
//
id, _ := getStringField(L, opt, "id")
pass, _ := getStringField(L, opt, "pass")
//
page, err := RequestWebPage(url, body, headers, id, pass)
if err != nil {
L.Push(lua.LString(page))
L.Push(lua.LString(err.Error()))
return 2
}
//
L.Push(lua.LString(page))
L.Push(lua.LNil)
return 2
}
type Script struct {
luaState *lua.LState
}
func (s *Script) newLuaState(script string) {
L := lua.NewState() // 创建一个lua解释器实例
//defer L.Close()
L.PreloadModule("json", luajson.Loader)
L.SetGlobal("request", L.NewFunction(request))
L.SetGlobal("scrape", L.NewFunction(scrape))
if err := L.DoString(script); err != nil {
panic(err)
}
s.luaState = L
}
func (s *Script) Close() {
s.luaState.Close()
}
func (s *Script) Scan(domain string) []string {
L := s.luaState
err := L.CallByParam(lua.P{
Fn: L.GetGlobal("vertical"),
NRet: 1,
Protect: true,
}, lua.LString(domain))
if err != nil {
panic(err)
}
ret := L.Get(-1)
L.Pop(1)
var q []string = []string{}
switch ret.Type() {
case lua.LTNil:
case lua.LTString:
q = append(q, ret.String())
case lua.LTTable:
res, ok := ret.(*lua.LTable)
if ok {
res.ForEach(func(_ lua.LValue, value lua.LValue) {
q = append(q, value.String())
})
}
}
return q
}
// Acquires the script name of the script by accessing the global variable.
func (s *Script) ScriptName() (string, error) {
L := s.luaState
lv := L.GetGlobal("name")
if lv.Type() == lua.LTNil {
return "", errors.New("Script does not contain the 'name' global")
}
if str, ok := lv.(lua.LString); ok {
return string(str), nil
}
return "", errors.New("The script global 'name' is not a string")
}