发一个自己写的抓包软件,支持插件化脚本分析
市場上的抓包工具已經(jīng)足夠多,輕量級的,重量級的都有,典型的wireshark,smartsniff等,
各有優(yōu)缺點,PowerSniff是為程序員準(zhǔn)備的一款抓包工具,目標(biāo)是使協(xié)議解析插件編寫更簡單。文件格式完全兼容wiareshark和tcpdump。
原理:捕獲到數(shù)據(jù)就調(diào)用預(yù)設(shè)置的腳本,將數(shù)據(jù)的指針和長度傳遞給腳本分析,在腳本中也支持調(diào)用注冊函數(shù)。
無需編譯,支持即寫即用,目前支持c語言和lua兩種編程語言作為腳本,安裝文件自帶一些例子,歡迎測試并提供意見。
鏈接:?http://weiqiba.com/powersniff/PowerSniff-Setup-v0.9.exe
?
一、典型場景
抓包軟件運行時(或事后分析),希望對每個數(shù)據(jù)包使用插件分析,smartsniff直接不支持,而wireshark編寫插件成本太大,
需要安裝visual studio和相關(guān)sdk,不論是插件本身的開發(fā)還是調(diào)試,都不是一件簡單的事。
一些廠商對常見的協(xié)議(如rtsp),做了wireshark的插件,但是如果是自己的協(xié)議,只能手工分析。
?
PowerSniff是為快速解決數(shù)據(jù)分析而來,(即便不考慮數(shù)據(jù)分析,界面和可操作性也比wireshark, smartsniff更友好),
對捕獲的網(wǎng)絡(luò)數(shù)據(jù)包,即時腳本編寫(語法高亮,即時編譯,即時運行,也可以直接用編寫好的腳本),
目前腳本支持lua和c語言,實測在i7上單核處理簡單腳本插件調(diào)用,lua每秒1萬次,c語言更快。
?
示例:QQ使用UDP協(xié)議,服務(wù)器端口8000,登錄包的第49字節(jié)到53字節(jié)是qq號,解析并顯示這個qq號
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,然后再菜單的插件列表里面啟用它即可。代碼不需要編譯,程序內(nèi)置的TCC引擎會自動compile,然后relocate到當(dāng)前進程。
等價于將c語言作為腳本,由于代碼只在load時編譯,所以插件執(zhí)行速度非常快。(除了c語言支持,另外還支持lua腳本)
?
二、相關(guān)界面截圖
(1)菜單
?
(2)設(shè)置
?
(3)lua編輯器
?
(4)c語言編輯器
?
(5)hex顯示界面
?
(6)安裝文件列表
?
?
(7)第三方插件xtrace
?
這個軟件預(yù)計下周單獨發(fā)布,目前PowerSniff已經(jīng)集成c++的sdk。
xtrace程序提供了sdk函數(shù)xtrace(),可以看成一個printf函數(shù)(支持本機和網(wǎng)絡(luò)),只是它的輸出不是stdout,而是xtrace軟件。
c++的版本只需要包含一個20k自己的頭文件即可,不需要包含lib。另外也支持lua,python,javascript,c#,delphi等語言。
除了printf ,還支持xcounter功能,方便程序性能分析或計數(shù)用。(支持本機和網(wǎng)絡(luò))
c++最簡用法:
#include "xtrace.h"
xtrace(_T("hello, %s, %d\n"), _T("world"), 99999999);
不需要其它任何改動,直接替換vc里面的TRACE宏,或者printf函數(shù)。開啟xtrace主程序,即可看到相關(guān)輸出。
?
三、插件編寫說明(文件編碼必須使用utf8)
程序需要調(diào)用的lua接口,參考plugin/demo_lua.lua
(1)init: 插件初始化
(2)handle_data: 當(dāng)收到一個數(shù)據(jù)包時調(diào)用這個函數(shù),函數(shù)return "delete"也可以起到過濾作用
(3)handle_click:當(dāng)單擊列表數(shù)據(jù)時調(diào)用這個函數(shù)
(4)handle_double: 當(dāng)雙擊列表數(shù)據(jù)時調(diào)用這個函數(shù)
lua中增加的可以回調(diào)的程序接口:
plugin_output_clear: 清空plugin output窗口
plugin_output: 輸出到plugin output窗口
plugin_summary: 輸出到listview最右邊的Plugin Summary項
trace: 輸出到三方工具xtrace
trace_raw: 輸出到三方工具xtrace
dbgview:輸出到三方工具dbgview.exe
------------------------------------------------------------
c語言自定義函數(shù)參考: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號碼登錄監(jiān)視腳本(不支持手機號碼登錄,不支持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: 二進制數(shù)據(jù) -- len_total: 總共數(shù)據(jù)長度 -- len_data: 有效數(shù)據(jù)長度(去除各種頭之后的數(shù)據(jù)) function handle_data(protocol,data,len_total,len_data)if 54 == len_total thenreturn "delete" -- remove handshakeendsrc_port = tcp_ip_get_src_port(data)dst_port = tcp_ip_get_dst_port(data)-- if 8000 != src_port && 8000 != dst_port thenif (8000 ~= dst_port) or (len_data < 100) thenreturn "delete"endif 2 ~= data:byte(43) then -- 0x2是qq udp協(xié)議magic numberreturn "delete"endif 8 ~= data:byte(46) then -- 8和37是 0x8和0x25是協(xié)議類型,表示登錄return "delete"endif 37 ~= data:byte(47) thenreturn "delete"end-- 50, 51, 52, 53字節(jié)是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) endfunction handle_click(protocol,data,len_total,len_data)if 54 == len_total thenreturnendsrc_port = tcp_ip_get_src_port(data)dst_port = tcp_ip_get_dst_port(data)-- if 8000 != src_port && 8000 != dst_port thenif (8000 ~= dst_port) or (len_data < 100) thenreturnendif 2 ~= data:byte(43) then -- 0x2是qq udp協(xié)議magic numberreturn endif 8 ~= data:byte(46) then -- 8和37是 0x8和0x25是協(xié)議類型,表示登錄returnendif 37 ~= data:byte(47) thenreturnend-- 50, 51, 52, 53字節(jié)是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") endfunction 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協(xié)議magic numberreturn -1;if(8 != data[45]) // 8和37是 0x8和0x25是協(xié)議類型,表示登錄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(推薦);科來網(wǎng)絡(luò)分析軟件;ominipeek;etherpeek;Charles;(經(jīng)過各種測試,還是wireshark最好用)
輕量級抓包工具:
smartsniff:只有230KB(推薦);minisniff:只有45KB(2006年停止維護);powersniff:2.3M(包含若干個第三方插件)
?
五、閉源。非商業(yè)使用無限制。收到BUG會解決(反饋qq群466507719)。無其它技術(shù)支持。
付費封閉協(xié)議分析及定制腳本。目前只支持ipv4(tcp,udp,icmp),已對pcap數(shù)十個樣本測試,如需要其它協(xié)議可以定制。
?
六、借地找個工作,地點武漢光谷~~~
目標(biāo)嵌入式,c++,服務(wù)器端開發(fā),移動端或web開發(fā)等軟件工作;也可以偏管理;
簡歷如下:resume
對web開發(fā)較有興趣,熟練的同學(xué)可以技能交換
(只收3year+的同學(xué),不閑聊,武漢光谷有定期線下交流,最好能提供一份帶詳細(xì)項目經(jīng)驗的簡歷,qq:80101277)
?
擁劍,中南民族大學(xué)附近2018.10.12
?
轉(zhuǎn)載于:https://www.cnblogs.com/wjx0912/p/9772931.html
總結(jié)
以上是生活随笔為你收集整理的发一个自己写的抓包软件,支持插件化脚本分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux lightdm启动阶段黑屏,
- 下一篇: [Err] ORA-00979: not