-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
219 lines (181 loc) · 6.83 KB
/
main.js
File metadata and controls
219 lines (181 loc) · 6.83 KB
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
const axios = require('axios');
const crypto = require('crypto-js'); // 使用 CryptoJS
const fs = require('fs'); // 文件操作
const pwd = '123'
const BASE_URL = 'http://192.168.3.1/api/system';
const SESSION_FILE = './session.txt'; // 保存 session 的文件
const debug = true; // 调试开关
const instance = axios.create({
withCredentials: true,
headers: { 'Content-Type': 'application/json' }
});
let sessionCookie = ''; // 初始会话 Cookie
async function scramLogin(username, password) {
try {
const csrf = await initializeCsrf();
if (debug) console.log('CSRF Initialized:', csrf);
const firstNonce = generateNonce();
if (debug) console.log('First Nonce:', firstNonce);
const nonceResponse = await sendNonceRequest(username, firstNonce, csrf);
if (debug) console.log('Nonce Response:', nonceResponse);
const { servernonce, salt, iterations } = nonceResponse;
await sendProofRequest(username, password, firstNonce, servernonce, salt, iterations, csrf);
} catch (error) {
handleError(error);
}
}
async function initializeCsrf() {
const response = await instance.post(`${BASE_URL}/user_login_nonce`, {
csrf: { csrf_param: '1', csrf_token: '2' }
});
const setCookieHeader = response.headers['set-cookie'];
if (setCookieHeader) {
sessionCookie = setCookieHeader.find(cookie => cookie.startsWith('SessionID_R3'));
if (debug) console.log('Session Cookie:', sessionCookie);
}
if (response.data.errcode === 1) {
return {
csrf_param: response.data.csrf_param,
csrf_token: response.data.csrf_token
};
} else {
throw new Error('Failed to initialize CSRF');
}
}
async function sendNonceRequest(username, firstNonce, csrf) {
const requestData = {
data: { username, firstnonce: firstNonce },
csrf
};
if (debug) console.log('Request Body:', JSON.stringify(requestData, null, 2));
const response = await instance.post(`${BASE_URL}/user_login_nonce`, requestData, {
headers: { 'Cookie': sessionCookie }
});
if (response.data.csrf_param && response.data.csrf_token) {
csrf.csrf_param = response.data.csrf_param;
csrf.csrf_token = response.data.csrf_token;
if (debug) console.log('CSRF Updated:', csrf);
}
if (response.data.err !== 0) {
console.log('Nonce Request Failed:', response.data);
throw new Error('Nonce request failed');
}
return response.data;
}
async function sendProofRequest(username, password, firstNonce, serverNonce, salt, iterations, csrf) {
const saltBuffer = crypto.enc.Hex.parse(salt);
const authMessage = `${firstNonce},${serverNonce},${serverNonce}`;
const clientProof = SCRAM.clientProof(password, saltBuffer, iterations, authMessage);
if (debug) console.log('Client Proof:', clientProof);
const proofRequestData = {
name: 'user_login_proof',
data: {
clientproof: clientProof,
finalnonce: serverNonce
},
csrf
};
if (debug) console.log('Proof Request Body:', JSON.stringify(proofRequestData, null, 2));
try {
const response = await instance.post(`${BASE_URL}/user_login_proof`, proofRequestData, {
headers: { 'Cookie': sessionCookie }
});
const setCookieHeader = response.headers['set-cookie'];
if (setCookieHeader) {
const newSessionCookie = setCookieHeader
.find(cookie => cookie.startsWith('SessionID_R3'))
.split(';')
.slice(0, -1)
.join(';') + ';';
sessionCookie = newSessionCookie;
if (debug) console.log('New Session Cookie:', sessionCookie);
saveSessionCookie(); // 保存到文件
}
if (response.data.err === 0) {
console.log('Login successful!');
console.log('Response:', response.data);
} else {
console.log('Login failed:', response.data);
}
} catch (error) {
handleError(error);
}
}
// 保存 Session Cookie 到文件
function saveSessionCookie() {
fs.writeFileSync(SESSION_FILE, sessionCookie, 'utf8');
if (debug) console.log('Session Cookie saved to file.');
}
// 从文件读取 Session Cookie
function loadSessionCookie() {
if (fs.existsSync(SESSION_FILE)) {
sessionCookie = fs.readFileSync(SESSION_FILE, 'utf8');
if (debug) console.log('Session Cookie loaded from file:', sessionCookie);
}
}
// 请求 WAN Diagnose 并解析 External IP
async function getExternalIP() {
try {
const response = await instance.get('http://192.168.3.1/api/ntwk/wandiagnose', {
headers: { 'Cookie': sessionCookie }
});
const data = response.data;
if (debug) console.log('WAN Diagnose Response:', data);
const externalIP = data.ExternalIPAddress;
if (externalIP) {
console.log('External IP Address:', externalIP);
} else {
console.log('External IP not found. Re-authenticating...');
await scramLogin('admin', pwd);
await getExternalIP(); // 重新请求
}
} catch (error) {
console.error('Error fetching WAN Diagnose:', error.message);
console.log('Re-authenticating...');
await scramLogin('admin', pwd);
await getExternalIP(); // 重新请求
}
}
// 处理错误
function handleError(error) {
console.error('Error:', error.response ? error.response.data : error.message);
}
// 生成随机 Nonce
function generateNonce() {
return crypto.lib.WordArray.random(32).toString(crypto.enc.Hex);
}
// SCRAM 实现
const SCRAM = {
keySize: 8,
hasher: crypto.algo.SHA256,
hmac: crypto.HmacSHA256,
saltedPassword: function (password, salt, iterations) {
return crypto.PBKDF2(password, salt, {
keySize: this.keySize,
iterations: iterations,
hasher: this.hasher,
});
},
clientKey: function (saltedPwd) {
return this.hmac(saltedPwd, 'Client Key');
},
storedKey: function (clientKey) {
return this.hasher.create().update(clientKey).finalize();
},
signature: function (key, message) {
return this.hmac(key, message);
},
clientProof: function (password, salt, iterations, authMessage) {
const saltedPwd = this.saltedPassword(password, salt, iterations);
const clientKey = this.clientKey(saltedPwd);
const storedKey = this.storedKey(clientKey);
const clientSignature = this.signature(storedKey, authMessage);
for (let i = 0; i < clientKey.sigBytes / 4; i++) {
clientKey.words[i] ^= clientSignature.words[i];
}
return clientKey.toString(crypto.enc.Hex);
},
};
// 初始化并请求 External IP
loadSessionCookie();
getExternalIP();