-
Notifications
You must be signed in to change notification settings - Fork 3
/
xenv.go
199 lines (184 loc) · 4.53 KB
/
xenv.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
package main
import (
"bufio"
"bytes"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
URL "net/url"
"os"
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/mmpx12/optionparser"
)
var (
success int32
mu = &sync.Mutex{}
thread = make(chan struct{}, 50)
wg sync.WaitGroup
output = "found_env.txt"
proxy string
insecure bool
version = "1.0.2"
userAgent = "Mozilla/5.0 (X11; Linux x86_64)"
path = []string{"/.env"}
)
func CheckEnv(client *http.Client, url, path string) {
defer wg.Done()
req, err := http.NewRequest("GET", "https://"+url+path, nil)
if err != nil {
<-thread
return
}
req.Header.Add("User-Agent", userAgent)
//prevent RAM exhaustion from large body
req.Header.Set("Range", "bytes=0-4000")
resp, err := client.Do(req)
if err != nil {
<-thread
return
}
if resp.Body != nil {
defer resp.Body.Close()
}
//prevent RAM exhaustion from large body if range header isn't honored
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 4000))
if err != nil {
<-thread
return
}
match, err := regexp.MatchString(`(?mi)<body|<script|<html>|<?php`, string(body))
if err != nil {
<-thread
return
}
if len(body) >= 3700 || match {
<-thread
return
}
r := regexp.MustCompile(`(?m)^([A-Za-z0-9#-_]){1,35}[\s]{0,10}=.{2,100}$`)
if r.MatchString(string(body)) {
all := r.FindAllString(string(body), -1)
if len(all) > 5 {
mu.Lock()
atomic.AddInt32(&success, 1)
WriteToFile("============================\n" + resp.Request.URL.String())
fmt.Println("\033[1K\r\033[32mENV FOUND:\033[36m", resp.Request.URL.String()+"\033[0m")
for _, j := range all {
key, val, _ := strings.Cut(j, "=")
fmt.Printf("\033[33m%s\033[37m=\033[35m%s\n", key, val)
WriteToFile(key + "=" + val)
}
mu.Unlock()
<-thread
return
}
}
<-thread
}
func WriteToFile(target string) {
f, _ := os.OpenFile(output, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
defer f.Close()
fmt.Fprintln(f, target)
}
func LineNBR(f string) int {
r, _ := os.Open(f)
defer r.Close()
buf := make([]byte, 32*1024)
count := 0
lineSep := []byte{'\n'}
for {
c, err := r.Read(buf)
count += bytes.Count(buf[:c], lineSep)
switch {
case err == io.EOF:
return count
case err != nil:
return 0
}
}
}
func main() {
var threads, input, env string
var printversion bool
op := optionparser.NewOptionParser()
op.Banner = "Scan for exposed env file\n\nUsage:\n"
op.On("-t", "--thread NBR", "Number of threads (default 50)", &threads)
op.On("-o", "--output FILE", "Output file (default found_env.txt)", &output)
op.On("-i", "--input FILE", "Input file", &input)
op.On("-e", "--env-path ENV", "Env Path comma sparated (default '/.env')", &env)
op.On("-k", "--insecure", "Ignore certificate errors", &insecure)
op.On("-u", "--user-agent USR", "Set user agent", &userAgent)
op.On("-p", "--proxy PROXY", "Use proxy (proto://ip:port)", &proxy)
op.On("-V", "--version", "Print version and exit", &printversion)
op.Exemple("xenv -i alexa-top-1M.lst")
op.Exemple("xenv -k -e /.env,/api/.env,/admin/.env -i alexa-top-1M.lst")
op.Parse()
fmt.Printf("\033[31m")
op.Logo("[X-env]", "doom", false)
fmt.Printf("\033[0m")
if printversion {
fmt.Println("version:", version)
os.Exit(1)
}
if threads != "" {
tr, _ := strconv.Atoi(threads)
thread = make(chan struct{}, tr)
}
if input == "" {
fmt.Println("\033[31m[!] You must specify an input file\033[0m\n")
op.Help()
os.Exit(1)
}
if env != "" {
path = strings.Split(env, ",")
}
client := &http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
},
}
if proxy != "" {
proxyURL, _ := URL.Parse(proxy)
client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
},
}
}
log.SetOutput(io.Discard)
os.Setenv("GODEBUG", "http2client=0")
readFile, err := os.Open(input)
defer readFile.Close()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
i := 0
total := LineNBR(input) * len(path)
for fileScanner.Scan() {
target := fileScanner.Text()
for _, p := range path {
i++
mu.Lock()
fmt.Printf("\033[1K\r\033[31m[\033[33m%d\033[36m/\033[33m%d \033[36m(\033[32m%d\033[36m)\033[31m] \033[0m%s%s\033[0m", i, total, int(success), target, p)
mu.Unlock()
thread <- struct{}{}
wg.Add(1)
go CheckEnv(client, target, p)
}
}
wg.Wait()
fmt.Printf("\033[1K\rFound %d env files.\n", success)
}