-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
159 lines (133 loc) · 4.24 KB
/
utils.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
package main
import (
"fmt"
"net"
"strings"
"time"
// "net"
"net/http"
"net/url"
"github.com/PuerkitoBio/goquery"
log "github.com/sirupsen/logrus"
// "time"
)
func Magic(fgtIP string, port string) (string, error) {
// The URL to send the GET request to
// url := "http://172.18.10.10:1000/login?"
url := "http://" + fgtIP + ":" + port + "/logout?"
// Send the GET request
resp, err := http.Get(url)
if err != nil {
// log.Fatalf("Failed to make GET request: %v", err)
return "", fmt.Errorf("Failed to make GET request: %w", err)
}
defer resp.Body.Close()
// Check if the request was successful
if resp.StatusCode != http.StatusOK {
// log.Fatalf("Received non-200 response: %d %s", resp.StatusCode, resp.Status)
return "", fmt.Errorf("Received non-200 response: %d %s", resp.StatusCode, resp.Status)
}
// Parse the HTML response
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
// log.Fatalf("Failed to parse HTML: %v", err)
return "", fmt.Errorf("Failed to parse HTML: %w", err)
}
// Find the hidden input element with name="magic" and extract its value
magicValue, exists := doc.Find(`input[name="magic"]`).Attr("value")
if !exists {
// log.Fatalf("Magic value not found")
return "", fmt.Errorf("Magic value not found")
}
return magicValue, nil
}
func Login(fgtIP, uname, passw, sessionID, port string) error {
postData := url.Values{}
// magic, err := Magic(fgtIP)
// if err != nil {
// log.Error("Error:", err)
// }
// sessionID = magic
log.Debug(sessionID)
postData.Set("magic", sessionID)
postData.Set("username", uname)
postData.Set("password", passw)
client := &http.Client{
Timeout: 5 * time.Second,
}
req, err := http.NewRequest("POST", "http://"+fgtIP+":"+port+"/logout?", strings.NewReader(postData.Encode()))
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error sending request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return nil
} else {
return fmt.Errorf("failed to authenticate: %s", resp.Status)
}
}
func Logout(fgtIP string, port string, userAgent string) error {
// Define the URL
url := "http://" + fgtIP + ":" + port + "/logout?"
// Create a new HTTP request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
// Set the headers
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/png,image/svg+xml,*/*;q=0.8")
req.Header.Set("Accept-Language", "en-US,en;q=0.5")
req.Header.Set("Accept-Encoding", "gzip, deflate")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Upgrade-Insecure-Requests", "1")
req.Header.Set("Priority", "u=0, i")
// Create an HTTP client with a timeout
client := &http.Client{
Timeout: 5 * time.Second,
}
// Send the request
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// Check the response status
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("logout failed: %s", resp.Status)
}
//fmt.Println("Logout successful")
return nil
}
/*
Detect the addresses which can be used for dispatching in non-tunnelling mode.
Alternate to ipconfig/ifconfig
*/
func detect_interfaces() (map[string]string, error) {
// //fmt.Println("--- Listing the available adresses for dispatching")
ifaces, err := net.Interfaces()
if len(ifaces) == 0 {
//fmt.Println("No interfaces found")
return nil, err
}
dict := map[string]string{}
for _, iface := range ifaces {
if (iface.Flags&net.FlagUp == net.FlagUp) && (iface.Flags&net.FlagLoopback != net.FlagLoopback) {
addrs, _ := iface.Addrs()
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
//fmt.Printf("[+] %s, IPv4:%s\n", iface.Name, ipnet.IP.String())
dict[iface.Name] = ipnet.IP.String()
}
}
}
}
}
return dict, nil
}