-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaes_cbc.js
99 lines (86 loc) · 2.85 KB
/
aes_cbc.js
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
// npm install express body-parser
// node aes_cbc.js
const express = require("express");
const crypto = require("crypto");
const bodyParser = require("body-parser");
const app = express();
const PORT = 5000;
// 全局密钥和IV
const KEY = Buffer.from("32byteslongsecretkeyforaes256!aa"); // 32字节密钥
const IV = Buffer.from("16byteslongiv456"); // 16字节IV
const JSON_KEY = "data";
// 中间件处理 JSON 请求
app.use(bodyParser.json({ limit: "10mb" }));
// 加密函数
function encrypt(content) {
const cipher = crypto.createCipheriv("aes-256-cbc", KEY, IV);
const encrypted = Buffer.concat([cipher.update(content), cipher.final()]);
return encrypted;
}
// 解密函数
function decrypt(content) {
const decipher = crypto.createDecipheriv("aes-256-cbc", KEY, IV);
const decrypted = Buffer.concat([decipher.update(content), decipher.final()]);
return decrypted;
}
// 获取加密数据
function getData(content) {
const bodyJson = JSON.parse(content.toString());
return Buffer.from(bodyJson[JSON_KEY], "base64");
}
// 将数据转换为 JSON 字符串格式
function toData(content) {
const bodyJson = {};
bodyJson[JSON_KEY] = content.toString("base64");
return Buffer.from(JSON.stringify(bodyJson));
}
// 请求钩子:hookRequestToBurp
app.post("/hookRequestToBurp", (req, res) => {
try {
const encryptedData = getData(Buffer.from(req.body.contentBase64, "base64"));
const data = decrypt(encryptedData);
req.body.contentBase64 = data.toString("base64");
res.json(req.body);
} catch (err) {
res.status(500).send({ error: "Decryption failed" });
}
});
// 请求钩子:hookRequestToServer
app.post("/hookRequestToServer", (req, res) => {
try {
const data = Buffer.from(req.body.contentBase64, "base64");
const encryptedData = encrypt(data);
const body = toData(encryptedData);
req.body.contentBase64 = body.toString("base64");
res.json(req.body);
} catch (err) {
res.status(500).send({ error: "Encryption failed" });
}
});
// 响应钩子:hookResponseToBurp
app.post("/hookResponseToBurp", (req, res) => {
try {
const encryptedData = getData(Buffer.from(req.body.contentBase64, "base64"));
const data = decrypt(encryptedData);
req.body.contentBase64 = data.toString("base64");
res.json(req.body);
} catch (err) {
res.status(500).send({ error: "Decryption failed" });
}
});
// 响应钩子:hookResponseToClient
app.post("/hookResponseToClient", (req, res) => {
try {
const data = Buffer.from(req.body.contentBase64, "base64");
const encryptedData = encrypt(data);
const body = toData(encryptedData);
req.body.contentBase64 = body.toString("base64");
res.json(req.body);
} catch (err) {
res.status(500).send({ error: "Encryption failed" });
}
});
// 启动服务
app.listen(PORT, () => {
console.log(`Server running at http://0.0.0.0:${PORT}`);
});