发一个自己写的抓包软件,支持插件化脚本分析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jinglexy/article/details/83023150

市场上的抓包工具已经足够多,轻量级的,重量级的都有,典型的wireshark,smartsniff等,

各有优缺点,PowerSniff是为程序员准备的一款抓包工具,目标是使协议解析插件编写更简单。文件格式完全兼容wiareshark和tcpdump。

原理:捕获到数据就调用预设置的脚本,将数据的指针和长度传递给脚本分析,在脚本中也支持调用注册函数。

无需编译,支持即写即用,目前支持c语言和lua两种编程语言作为脚本,安装文件自带一些例子,欢迎测试并提供意见。

链接: http://weiqiba.com/powersniff/PowerSniff-Setup-v0.9.exe

一、典型场景

抓包软件运行时(或事后分析),希望对每个数据包使用插件分析,smartsniff直接不支持,而wireshark编写插件成本太大,

需要安装visual studio和相关sdk,不论是插件本身的开发还是调试,都不是一件简单的事。

一些厂商对常见的协议(如rtsp),做了wireshark的插件,但是如果是自己的协议,只能手工分析。

PowerSniff是为快速解决数据分析而来,(即便不考虑数据分析,界面和可操作性也比wireshark, smartsniff更友好),

对捕获的网络数据包,即时脚本编写(语法高亮,即时编译,即时运行,也可以直接用编写好的脚本),

目前脚本支持lua和c语言,实测在i7上单核处理简单脚本插件调用,lua每秒1万次,c语言更快。

示例:QQ使用UDP协议,服务器端口8000,登录包的第49字节到53字节是qq号,解析并显示这个qq号

1

2

3

4

5

6

7

int handle_data(const char *protocol, unsigned char *data, int total_len, int data_len)

{

    unsigned int qq_number = data[49] * 256 * 256 * 256 + data[50] * 256 * 256 + data[51] * 256 + data[52];

    plugin_summary("debug-%d, qq_number is: %d", __LINE__, qq_number);

    return 0;

}

将上面代码保存为test1.c,然后再菜单的插件列表里面启用它即可。代码不需要编译,程序内置的TCC引擎会自动compile,然后relocate到当前进程。

等价于将c语言作为脚本,由于代码只在load时编译,所以插件执行速度非常快。(除了c语言支持,另外还支持lua脚本)

二、相关界面截图

(1)菜单

 

(2)设置

(3)lua编辑器

(4)c语言编辑器

(5)hex显示界面

(6)安装文件列表

(7)第三方插件xtrace

这个软件预计下周单独发布,目前PowerSniff已经集成c++的sdk。

xtrace程序提供了sdk函数xtrace(),可以看成一个printf函数(支持本机和网络),只是它的输出不是stdout,而是xtrace软件。

c++的版本只需要包含一个20k自己的头文件即可,不需要包含lib。另外也支持lua,python,javascript,c#,delphi等语言。

除了printf ,还支持xcounter功能,方便程序性能分析或计数用。(支持本机和网络)

c++最简用法:

#include "xtrace.h"

xtrace(_T("hello, %s, %d\n"), _T("world"), 99999999);

不需要其它任何改动,直接替换vc里面的TRACE宏,或者printf函数。开启xtrace主程序,即可看到相关输出。

三、插件编写说明(文件编码必须使用utf8)

程序需要调用的lua接口,参考plugin/demo_lua.lua
(1)init: 插件初始化
(2)handle_data: 当收到一个数据包时调用这个函数,函数return "delete"也可以起到过滤作用
(3)handle_click:当单击列表数据时调用这个函数
(4)handle_double: 当双击列表数据时调用这个函数

lua中增加的可以回调的程序接口:
plugin_output_clear: 清空plugin output窗口
plugin_output: 输出到plugin output窗口
plugin_summary: 输出到listview最右边的Plugin Summary项
trace: 输出到三方工具xtrace
trace_raw: 输出到三方工具xtrace

dbgview:输出到三方工具dbgview.exe

------------------------------------------------------------

c语言自定义函数参考:bin\3rd\libtcc\include\powersniff.h

复制代码

#ifndef __POWERSNIFF_DEFINE_H_
#define __POWERSNIFF_DEFINE_H_

int trace(const char *format, ...);
int TRACE(const char *format, ...);
int xtrace(const char *format, ...);
int XTRACE(const char *format, ...);

int trace_raw(int color, const char *format, ...);
int TRACE_RAW(int color, const char *format, ...);
int xtrace_raw(int color, const char *format, ...);
int XTRACE_RAW(int color, const char *format, ...);


int dbgview(const char *format, ...);
int DBGView(const char *format, ...);

int plugin_output(const char *format, ...);
int PLUGIN_OUTPUT(const char *format, ...);

int plugin_output_clear();
int PLUGIN_OUTPUT_CLEAR();

int plugin_summary(const char *format, ...);
int PLUGIN_SUMMARY(const char *format, ...);
#endif

复制代码

解析qq号的lua脚本:

复制代码

-- file encode must be UTF8
-- qq号码登录监视脚本(不支持手机号码登录,不支持webqq,只在pc上用qq2015测试通过)
-- 2015.9.14
require "base64"
require "tcp_ip"

function init()
    trace("plugin init:  ".._VERSION.."\n")
    trace("package path: "..package.path.."\n")
    trace("package path: "..package.cpath.."\n")
    --for k,v in pairs(_G) do
    --    trace(string.format("%s,%s\n", k, v))
    --end
end

-- protocol:    字符串如tcp,udp,icmp
-- data:    二进制数据
-- len_total:    总共数据长度
-- len_data:    有效数据长度(去除各种头之后的数据)
function handle_data(protocol,data,len_total,len_data)
    if 54 == len_total then
        return "delete"        -- remove handshake
    end
    src_port = tcp_ip_get_src_port(data)
    dst_port = tcp_ip_get_dst_port(data)
    -- if 8000 != src_port && 8000 != dst_port then
    if (8000 ~= dst_port) or (len_data < 100) then
        return "delete"
    end
    if 2 ~= data:byte(43) then    -- 0x2是qq udp协议magic number
        return "delete"
    end
    if 8 ~= data:byte(46) then    -- 8和37是 0x8和0x25是协议类型,表示登录
        return "delete"
    end
    if 37 ~= data:byte(47) then
        return "delete"
    end
    -- 50, 51, 52, 53字节是qq号(lua index从1开始而不是0)
    qq_number = data:byte(50) * 256 * 256 * 256 + data:byte(51) * 256 * 256 + data:byte(52) * 256 + data:byte(53)
    plugin_summary("qq_number is: " .. qq_number)
end

function handle_click(protocol,data,len_total,len_data)
    if 54 == len_total then
        return
    end
    src_port = tcp_ip_get_src_port(data)
    dst_port = tcp_ip_get_dst_port(data)
    -- if 8000 != src_port && 8000 != dst_port then
    if (8000 ~= dst_port) or (len_data < 100) then
        return
    end
    if 2 ~= data:byte(43) then    -- 0x2是qq udp协议magic number
        return 
    end
    if 8 ~= data:byte(46) then    -- 8和37是 0x8和0x25是协议类型,表示登录
        return
    end
    if 37 ~= data:byte(47) then
        return
    end
    -- 50, 51, 52, 53字节是qq号(lua index从1开始而不是0)
    qq_number = data:byte(50) * 256 * 256 * 256 + data:byte(51) * 256 * 256 + data:byte(52) * 256 + data:byte(53)
    plugin_output_clear()
    plugin_output("qq_number is: " .. qq_number.."\n")
end

function handle_double(protocol,data,len_total,len_data)
    handle_data(protocol,data,len_total,len_data)
end

复制代码

解析qq号的c脚本:

复制代码

// file encode must be UTF8
// sdk function:
// init()        
// handle_data()    
// handle_click()    
// handle_double()
// all data lock at background, you can use pointer any case,  but need ATTENTION also.
// out of memory or invalid pointer may crash the full virutal machine, or cause application fetal error.
// the follow thread .h file should include!
#include <winapi\windows.h>
#include <winapi\wingdi.h>
#include <powersniff.h>

void init()
{
    trace_raw(RGB(255, 0, 0), "do init here\n");
}

// if return -1, the packet will be deleted!
int handle_data(const char *protocol, unsigned char *data, int total_len, int data_len)
{
    if(0 != strcmp("udp", protocol)) {
        return -1;        // filter
    }

    if(data_len < 100) {
        return -1;        // filter
    }

    short src_port = data[34] * 256 + data[35];
    short dst_port = data[36] * 256 + data[37];
    if(8000 != dst_port)
        return -1;

    if(2 != data[42])        // 0x2是qq udp协议magic number
        return -1;
    if(8 != data[45])        // 8和37是 0x8和0x25是协议类型,表示登录
        return -1;
    if(37 != data[46])
        return -1;

    unsigned int qq_number = data[49] * 256 * 256 * 256 + data[50] * 256 * 256 + data[51] * 256 + data[52];
    plugin_summary("debug-%d, qq_number is: %d", __LINE__, qq_number);
    plugin_output_clear();
    plugin_output("debug-%d, qq_number is: %d\n", __LINE__, qq_number);
    trace("debug-%d, qq_number is: %d\n", __LINE__, qq_number);
    return 0;
}

// fixed return 0
int handle_click(const char *protocol, unsigned char *data, int total_len, int data_len)
{
    trace_raw(RGB(0, 0, 255), "blue output\n");
    return 0;
}

// fixed return 0
int handle_double(const char *protocol, unsigned char *data, int total_len, int data_len)
{
    trace("default color output\n");
    trace_raw(RGB(0, 0, 255), "blue output\n");
    dbgview("output to debugview.exe\n");
    
    plugin_summary("hello1: %d", 123);
    plugin_output_clear();
    plugin_output("hello2: %d", 123);
    plugin_output_clear();
    plugin_output("hello3: %d\n", 456);
    plugin_output("hello4: %d\n", 789);
    return 0;
}

void *my_memcpy(void * to, const void * from, int n)
{
int d0, d1, d2;
__asm__ __volatile__(
        "rep ; movsl\n\t"
        "testb $2,%b4\n\t"
        "je 1f\n\t"
        "movsw\n"
        "1:\ttestb $1,%b4\n\t"
        "je 2f\n\t"
        "movsb\n"
        "2:"
        : "=&c" (d0), "=&D" (d1), "=&S" (d2)
        :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
        : "memory");
return (to);
}

复制代码

四、其它抓包工具

重量级抓包工具:
wireshark(推荐);科来网络分析软件;ominipeek;etherpeek;Charles;(经过各种测试,还是wireshark最好用)

轻量级抓包工具:
smartsniff:只有230KB(推荐);minisniff:只有45KB(2006年停止维护);powersniff:2.3M(包含若干个第三方插件)

五、闭源。非商业使用无限制。收到BUG会解决(反馈qq群466507719)。无其它技术支持。

付费封闭协议分析及定制脚本。目前只支持ipv4(tcp,udp,icmp),已对pcap数十个样本测试,如需要其它协议可以定制。

六、借地找个工作,地点武汉光谷~~~

目标嵌入式,c++,服务器端开发,移动端或web开发等软件工作;也可以偏管理;

简历如下:resume

对web开发较有兴趣,熟练的同学可以技能交换

(只收3year+的同学,不闲聊,武汉光谷有定期线下交流,最好能提供一份带详细项目经验的简历,qq:80101277)

拥剑,中南民族大学附近2018.10.12

猜你喜欢

转载自blog.csdn.net/jinglexy/article/details/83023150