通过WEB启动本地Wireshark远程抓包
业务需求:
在分布式环境中,每台执行机需要分别开启远程抓包功能,前台界面通过Wireshark自动抓取某台执行机的报文,以便分析业务之间的报文交互。
前台界面嵌入Wireshark,点击抓包是自动启动抓包。
需求分析:
通过查阅资料,Web启动本地应用程序主要有两种方式:
一、通过ActiveX控件启动本地应用程序
二、通过自定义URL协议启动本地应用程序
但是ActiveX方式只有IE浏览器支持,chrome、Opera、firefox都不支持该控件,所以采用第二种方式来实现。
方案设计:
通过自定义URL协议启动本地应用程序,这个方法主要思路是利用自定义URL Protocol来调用应用程序。
浏览器在解析到自定义URL Protocol之后,会寻找注册表,然后通过注册表启动相应的程序,然后调用该程序传入参数,这样就可以在WEB页面调到你的程序了。
1、编写注册表信息:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\WiresharkRpcap]
"URL Protocol"="D:\\Program Files (x86)\\Wireshark\\Start.exe"
@="WiresharkRpcapProtocol"
[HKEY_CLASSES_ROOT\WiresharkRpcap\DefaultIcon]
@="D:\\Program Files (x86)\\Wireshark\\Start.exe,1"
[HKEY_CLASSES_ROOT\WiresharkRpcap\shell]
@=""
[HKEY_CLASSES_ROOT\WiresharkRpcap\shell\open]
@=""
[HKEY_CLASSES_ROOT\WiresharkRpcap\shell\open\command]
@="\"D:\\Program Files (x86)\\Wireshark\\Start.exe\" \"%1\""
将上面的内容保存为WiresharkRpcap.reg文件,在上面的注册表代码中[@="\"D:\\Program Files (x86)\\Wireshark\\Start.exe\\" \"%1\""], %1表示传入参数。
2、WEB页面调用:
在网页直接定义一个超链接[协议://参数]:
<!DOCTYPE HTML PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>自动启动Wireshark远程抓包</title>
<metahttp-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<div>Platform-browser-dynamic</div>
<div>
<a href="WiresharkRpcap://D:\\Program Files (x86)\\Wireshark\\Wireshark.exe -B 1 -k -i rpcap://[192.168.10.188]:2002/eth0">
<h2>启动Wireshark远程抓包</h2>
</a>
</div>
</body>
</html>
保存上面内容为WiresharkRpcap.html文件,此处的"WiresharkRpcap://D:\\Program Files (x86)\\Wireshark\\Wireshark.exe -B 1 -k -i rpcap://[192.168.10.188]:2002/eth0"就是对应的%1所表示的参数,解析之后就可以得到参数执行命令。
3、编写启动程序Start.exe
#include "stdafx.h"
#include <windows.h>
#include "stdio.h"
#include "stdlib.h"
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int i= 0;
char* cmd = NULL;
for(i = 0; i < argc; ++i)
{
printf("argv[%d]: %s\n", i, argv[i]);
}
if(argc < 2)
{
return 1;
}
cmd = strstr(argv[1], "://");
if(cmd == NULL || strlen(cmd) <= strlen("://"))
{
return 1;
}
cmd += strlen("://");
printf("cmd: %s\n", cmd);
system(cmd);
return 0;
}
4、在浏览器中打开WiresharkRpcap.html,点击超链接即可启动Wireshark远程抓包。
[END]