/
requests.go
180 lines (158 loc) · 4.43 KB
/
requests.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
package attacks
/*
3 maneras de usar
[-] auth
[ ] prueba de directorios
[ ] form login
*/
import (
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"golang.org/x/net/publicsuffix"
)
//RequestData Basic data for requests
type RequestData struct {
User string
Pass string
UserAgent string
Proxy string
Client *http.Client
}
//NewRequestData Creates a requests data for http attacks
func NewRequestData() *RequestData {
r := new(RequestData)
r.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"
r.Client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
log.Fatal(err)
}
r.Client.Jar = jar
return r
}
//SetProxy set proxy the data
func (r *RequestData) SetProxy(urlPort string) {
proxy, err := url.Parse(urlPort)
if err != nil {
log.Fatal("Error parsing url")
}
r.Client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
Proxy: http.ProxyURL(proxy),
}
}
//GetOrPost request for get or post method
func (r *RequestData) GetOrPost(urlsite string, post string, redirect bool) (string, int, *http.Response) {
if post == "" {
return r.Send("GET", urlsite, "", redirect)
}
return r.Send("POST", urlsite, post, redirect)
}
//Send Send the data
func (r *RequestData) Send(method, urlsite, post string, redirect bool) (string, int, *http.Response) {
var req *http.Request
var err error
data := url.Values{}
if method == "GET" {
req, err = http.NewRequest(method, urlsite, nil)
} else {
// sep := strings.Split(post, "&")
if post == "" {
panic("there is no post data")
}
for _, v := range strings.Split(post, "&") {
w := strings.Split(v, "=")
data.Set(w[0], w[1])
}
req, err = http.NewRequest("POST", urlsite, strings.NewReader(data.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
req.Header.Set("User-Agent", r.UserAgent)
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
}
if err != nil {
fmt.Println("[!] Didn't get a response from the server")
return "", 0, nil
}
if r.User != "" {
req.SetBasicAuth(r.User, r.Pass)
}
resp, err := r.Client.Do(req)
if err != nil || resp == nil {
return "", 0, nil
}
defer resp.Body.Close()
code := resp.StatusCode
if resp.Body == nil {
return "", code, resp
}
html, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", code, resp
}
// fmt.Println(code, urlsite, redirect)
if redirect && (code == 302 || code == 301) {
ret := resp.Header.Get("Location")
if strings.HasPrefix(ret, "http://") || strings.HasPrefix(ret, "https://") {
return r.Send("GET", ret, "", false)
}
//else
if strings.HasPrefix(ret, "/") {
return r.Send("GET", extractDomain(urlsite)+ret, "", false)
}
return r.Send("GET", extractDomain(urlsite)+"/"+ret, "", false)
}
return string(html), code, resp
}
func extractDomain(url string) string {
dn := strings.Split(url, "/")
return dn[0] + "//" + dn[2]
}
//FileTry Send a request to verify a file if it exist
func FileTry(urlsite, word, fileWord, ext, post, proxy string, redirect bool) (string, int, string) {
r := NewRequestData()
if proxy != "" {
r.SetProxy(proxy)
}
urlsite = strings.Replace(urlsite, word, fileWord+ext, 1)
html, code, _ := r.GetOrPost(urlsite, post, redirect)
return html, code, urlsite
}
//SendAuth Send Auth request
func SendAuth(urlsite, post, username, password, proxy string, redirect bool) (string, int) { //, *http.Response) {
r := NewRequestData()
if proxy != "" {
r.SetProxy(proxy)
}
r.User = username
r.Pass = password
html, code, _ := r.GetOrPost(urlsite, post, redirect)
return html, code
}
//FormLogin Send a request of a form to log in
func FormLogin(urlsite, post, phrase, proxy string, redirect bool) (string, int, bool) {
r := NewRequestData()
if proxy != "" {
r.SetProxy(proxy)
}
// fmt.Println(urlsite, post, phrase, proxy)
html, code, _ := r.GetOrPost(urlsite, post, redirect)
// fmt.Println(html)
return html, code, !strings.Contains(html, phrase)
}