现代浏览器出于安全考虑,不允许通过JavaScript直接执行本机命令。然而,在某些特定情况下,我们可能需要浏览器能够调用本机命令,例如在浏览器全屏状态下移动键盘鼠标自动锁屏,或者在浏览器中直接打开本机软件处理文档如PPT、PDF等。
本文将介绍一种利用注册表和自定义协议的方法来实现这一需求。
方法概述
我们可以通过以下步骤实现浏览器调用本机命令:
在注册表中添加私有协议,指定处理协议的程序。
在浏览器的JavaScript中访问此私有协议,传递参数。
编写接收参数并执行命令的程序。
步骤详细说明
- 在注册表中添加私有协议
首先,我们需要在Windows注册表中添加一个自定义协议,并指定处理该协议的程序。
创建一个.reg文件,内容如下:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\local-command]
@="URL:local-command Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\local-command\shell]
[HKEY_CLASSES_ROOT\local-command\shell\open]
[HKEY_CLASSES_ROOT\local-command\shell\open\command]
@="\"C:\\CommExe.exe\" \"%1\""
双击这个文件,将其导入注册表。
- 编写处理协议的程序
接下来,我们编写一个处理自定义协议的程序CommExe.exe,用于接收并执行传递的命令。以下是一个示例代码,用C语言实现:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
void build_decoding_table();
static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'};
static char *decoding_table = NULL;
static int mod_table[] = {0, 2, 1};
char *base64_encode(const unsigned char *data,
size_t input_length,
size_t *output_length) {
*output_length = 4 * ((input_length + 2) / 3);
char *encoded_data = malloc(*output_length);
if (encoded_data == NULL) return NULL;
for (int i = 0, j = 0; i < input_length;) {
uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
}
for (int i = 0; i < mod_table[input_length % 3]; i++)
encoded_data[*output_length - 1 - i] = '=';
return encoded_data;
}
unsigned char *base64_decode(const char *data,
size_t input_length,
size_t *output_length) {
if (decoding_table == NULL) build_decoding_table();
if (input_length % 4 != 0) return NULL;
*output_length = input_length / 4 * 3;
if (data[input_length - 1] == '=') (*output_length)--;
if (data[input_length - 2] == '=') (*output_length)--;
unsigned char *decoded_data = malloc(*output_length);
if (decoded_data == NULL) return NULL;
for (int i = 0, j = 0; i < input_length;) {
uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t triple = (sextet_a << 3 * 6)
+ (sextet_b << 2 * 6)
+ (sextet_c << 1 * 6)
+ (sextet_d << 0 * 6);
if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
}
return decoded_data;
}
void build_decoding_table() {
decoding_table = malloc(256);
for (int i = 0; i < 64; i++)
decoding_table[(unsigned char) encoding_table[i]] = i;
}
void base64_cleanup() {
free(decoding_table);
}
void trimEncodedCommand(char *encoded_command) {
const char *prefix = "local-command://";
size_t prefix_len = strlen(prefix);
if (strncmp(encoded_command, prefix, prefix_len) == 0) {
memmove(encoded_command, encoded_command + prefix_len, strlen(encoded_command) - prefix_len + 1);
}
size_t len = strlen(encoded_command);
if (len > 0 && encoded_command[len - 1] == '/') {
encoded_command[len - 1] = '\0';
}
}
int main(int argc, char *argv[]) {
char *encoded_command = argv[1];
trimEncodedCommand(encoded_command);
size_t output_length;
unsigned char *decoded_command = base64_decode(encoded_command, strlen(encoded_command), &output_length);
if (decoded_command == NULL) return 1;
system((char *)decoded_command);
free(decoded_command);
return 0;
}
这个程序会接收通过自定义协议传递的Base64编码命令,解码后执行。
- 浏览器中调用私有协议
在浏览器的JavaScript代码中,通过调用自定义协议来传递命令:
// 将命令进行Base64编码
function base64Encode(str) {
return btoa(unescape(encodeURIComponent(str)));
}
// 调用私有协议执行命令
function executeCommand(command) {
const encodedCommand = base64Encode(command);
window.location.href = `local-command://${encodedCommand}`;
}
// 示例:调用锁屏命令
executeCommand('rundll32.exe user32.dll,LockWorkStation');
通过这种方法,可以在浏览器中执行一些特定的本机命令。请注意,这种方式需要用户在本地安装并授权相应的程序,有一定的安全风险,仅适用于可信的应用场景。