Wireshark解析自定义加密协议Word文档下载推荐.docx
- 文档编号:21201063
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:13
- 大小:67.71KB
Wireshark解析自定义加密协议Word文档下载推荐.docx
《Wireshark解析自定义加密协议Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Wireshark解析自定义加密协议Word文档下载推荐.docx(13页珍藏版)》请在冰豆网上搜索。
localf_protoVersion=ProtoField.uint8("
multi.protoVersion"
Version"
base.DEC)
localf_protoCmdType=ProtoField.uint16("
multi.protoCmdType"
CmdType"
base.DEC,{
[1]="
CmdType_1"
[2]="
CmdType_2"
[3]="
CmdType_3"
})
localf_protoEncrytionType=ProtoField.uint8(
"
multi.protoEncrytionType"
EncrytionType"
EncrytionType_1"
EncrytionType_2"
EncrytionType_3"
localf_protoCompressionType=ProtoField.uint8(
multi.protoCompressionType"
CompressionType"
CompressionType_1"
CompressionType_2"
CompressionType_3"
localf_protoDataLength=ProtoField.uint32("
multi.protoDataLength"
DataLength"
p_multi.fields={f_protoVersion,f_protoCmdType,f_protoEncrytionType,
f_protoCompressionType,f_protoDataLength}
functionDecodeBufferFunction(protoEncrytionType,protoCompressionType,buf,pos,buf_len)
localdecodeBuf=buf
--TODO:
Thisarticaljob
returndecodeBuf
end
functionp_multi.dissector(buf,pkt,root)
localpos=0
localbuf_len=buf:
len()
localt=root:
add(p_multi,buf(0,buf_len))
--协议版本(1字节)
t:
add(f_protoVersion,buf(pos,1))
pos=pos+1
--协议命令类型(2字节)
add(f_protoCmdType,buf(pos,2))
pos=pos+2
--协议加密类型(1字节)
add(f_protoEncrytionType,buf(pos,1))
localprotoEncrytionType=buf(pos,1):
uint()
--协议压缩类型(1字节)
add(f_protoCompressionType,buf(pos,1))
localprotoCompressionType=buf(pos,1):
--协议数据长度(4字节)
add(f_protoDataLength,buf(pos,1))
localprotoDataLength=buf(pos,4):
pos=pos+4
--协议数据(protoDataLength字节)
buf=DecodeBuffer(protoEncrytionType,protoCompressionType,buf,pos,buf_len)
new_buf_len=buf:
addyourcode
本文的主要任务是DecodeBufferFunction的实现。
目前的任务是把原始数据(buf)
协议头
加密数据
变成(decodeBuf)
解密数据
上图实际上就是原始buf和decodeBuf。
这里buf、decodeBuf均为Tvb("
TestyVirtualBuffer"
).另外,为操持两个buf在偏移上的一致,decodeBuf的头部也添加了“协议头”
因而DecodeBufferFunction的任务有3个:
1.由buf转换成luac库的数据结构
2.Luac库完成解码,并返回数据
3.再由luac库的数据结构转换成decodeBuf.
当然,如果我们知道Tvb的数据结构格式,可以省去第1、3步。
三、Luac库的编写
Luac库的开发可以参考相关资料。
本文的开发需要用到LuaBinaries.可以在:
下载。
详细路径:
本文使用了lua-5.1.5_Win32_dll10_lib.zip
Luac库的结构定义如下:
typedefstructMyByteArray{
intsize;
unsignedcharvalues[1];
/*variablepart*/
}MyByteArray;
:
wireshark中的Tvb、ByteArray都是以0为起始索引的方式。
所以下面MyByteArray也以0为索引。
这一点和lua语言提倡的方式不一致。
本文采用vs2010生成一个win32dll的库,工程名称:
decode.工程向导设置如下:
删除decode.h和decode.cpp中的,示例代码。
并在decode.h中,把导出定义改成:
#ifdefDECODE_EXPORTS
#defineDECODE_APIextern"
C"
__declspec(dllexport)
#else
__declspec(dllimport)
#endif
并引入lua-5.1.5_Win32_dll10_lib的库到本工程。
本文使用了《PrograminLua》第28章的部分示例代码。
关键源代码如下:
Stdafx.h
//stdafx.h:
includefileforstandardsystemincludefiles,
//orprojectspecificincludefilesthatareusedfrequently,but
//arechangedinfrequently
//
#pragmaonce
#include"
targetver.h"
#defineWIN32_LEAN_AND_MEAN//Excluderarely-usedstufffromWindowsheaders
//WindowsHeaderFiles:
#include<
windows.h>
//TODO:
referenceadditionalheadersyourprogramrequireshere
include/lua.hpp"
#pragmacomment(lib,"
lib/lua51"
)
decode.h
//Thefollowingifdefblockisthestandardwayofcreatingmacroswhichmakeexporting
//fromaDLLsimpler.AllfileswithinthisDLLarecompiledwiththeDECODE_EXPORTS
//symboldefinedonthecommandline.Thissymbolshouldnotbedefinedonanyproject
//thatusesthisDLL.Thiswayanyotherprojectwhosesourcefilesincludethisfilesee
//DECODE_APIfunctionsasbeingimportedfromaDLL,whereasthisDLLseessymbols
//definedwiththismacroasbeingexported.
enumEncryptionType
{
EncryptionType_begin,
EncryptionType_1=EncryptionType_begin,
EncryptionType_2,
EncryptionType_3,
//new...
EncryptionType_end=EncryptionType_3,
};
enumCompressionType
CompressionType_begin,
CompressionType_1=CompressionType_begin,
CompressionType_2,
CompressionType_3,
CompressionType_end=CompressionType_3,
DECODE_APIintluaopen_decode(lua_State*L);
staticintnewarray(lua_State*L);
staticintsetarray(lua_State*L);
staticintgetarray(lua_State*L);
staticintgetsize(lua_State*L);
staticintdecodearray(lua_State*L);
staticMyByteArray*decryptarray(unsignedcharencryptionType,MyByteArray*myarray);
staticMyByteArray*decompressarray(unsignedcharencryptionType,MyByteArray*myarray);
decode.cpp
//decode.cpp:
DefinestheexportedfunctionsfortheDLLapplication.
stdafx.h"
decode.h"
staticconststructluaL_regdecodeLib[]={
{"
new"
newarray},
set"
setarray},
get"
getarray},
size"
getsize},
decode"
decodearray},
{NULL,NULL}
intluaopen_decode(lua_State*L){
luaL_openlib(L,"
decodeLib,0);
return1;
}
staticintnewarray(lua_State*L){
intn=luaL_checkint(L,1);
size_tnbytes=sizeof(MyByteArray)+(n-1)*sizeof(unsignedchar);
MyByteArray*a=(MyByteArray*)lua_newuserdata(L,nbytes);
a->
size=n;
/*newuserdatumisalreadyonthestack*/
}
staticintsetarray(lua_State*L){
MyByteArray*a=(MyByteArray*)lua_touserdata(L,1);
intindex=luaL_checkint(L,2);
unsignedcharvalue=(unsignedchar)luaL_checknumber(L,3);
luaL_argcheck(L,a!
=NULL,1,"
`array'
expected"
luaL_argcheck(L,0<
=index&
&
index<
size,2,
"
indexoutofrange"
values[index]=value;
return0;
staticintgetarray(lua_State*L){
'
array'
lua_pushnumber(L,a->
values[index]);
staticintgetsize(lua_State*L){
size);
staticintdecodearray(lua_State*L){
unsignedcharencryptionType=(unsignedchar)lua_touserdata(L,1);
unsignedcharcompressionType=(unsignedchar)lua_touserdata(L,2);
MyByteArray*a=(MyByteArray*)lua_touserdata(L,3);
luaL_argcheck(L,EncryptionType_begin<
=encryptionType&
encryptionType<
=EncryptionType_end,
2,"
undefineencryptiontype"
luaL_argcheck(L,CompressionType_begin<
=compressionType&
compressionType<
=CompressionType_end,
undefinecompressiontype"
=NULL,3,"
MyByteArray*a1=NULL;
MyByteArray*a2=NULL;
if(NULL!
=a){
a1=decryptarray(encryptionType,a);
=a1){
a2=decryptarray(compressionType,a1);
}
=a2){
lua_pushlightuserdata(L,a2);
else{
lua_pushnil(L);
//////////////////////////////////////////////////////////////////////////
staticMyByteArray*decryptarray(unsignedcharencryptionType,MyByteArray*myarray){
MyByteArray*retarray=NULL;
switch(encryptionType){
caseEncryptionType_1:
//TODO:
break;
caseEncryptionType_2:
caseEncryptionType_3:
default:
returnretarray;
staticMyByteArray*decompressarray(unsignedcharencryptionType,MyByteArray*myarray){
caseCompressionType_1:
caseCompressionType_2:
caseCompressionType_3:
4、调用luac库解码
Lua解析脚本的开头需要增加部分代码,以加载新开发的decode.dll库。
Path="
D:
\\Documents\\VisualStudio2010\\Projects\\decode\\Debug\\decode.dll"
decodelib=package.loadlib(path,"
luaopen_decode"
ifnil~=decodelibthen
decodelib()
DecodeBufferFunction的实现如下:
ifnil~=decodethen
--constructMyByteArray
localrawLen=buf_len-pos
array=decode.new(rawLen)
fori=0,rawLen-1,1do
decode.set(array,i,buf(pos,1):
uint())
end
--decode
array=decode.decode(protoEncrytionType,protoCompressionType,array)
ifnil~=arraythen
--constructdecodeBuftvb
localdecodeLen=decode.size(array)
--constructtvbthroughtwiresharkByteArray
localbArray=ByteArray.new(decodeLen+pos)
fori=0,pos-1,1do
bArray.set_index(i,buf(i,1):
end
fori=0,decodeLen-1,1do
bArray.set_index(pos+i,decode.get(array,i))
--returedecodebuffer
returnbArray.tvb("
decodeBuf"
end
returnnil
5、总结
通过上面的介绍,提供了wireshark中解析自定义加密协议的思路和大致实现。
需要注意的是:
1.LuaBinarires的版本问题。
Wireshark采用了lua5.1和lua5.2,因此需要使用对应的LuaBinarires版本进行开发;
2.开发luac库,需要使用LuaBinarires的动态库版本。
尤其是lua5.2,如果使用静态库进行开发,wireshark在加载deco
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Wireshark 解析 自定义 加密 协议