From 5f595c8fbd34110a51f5e748a70bafc629fd1c9d Mon Sep 17 00:00:00 2001 From: UIS Date: Fri, 24 Jul 2020 19:23:59 +0300 Subject: [PATCH] Working version --- .gitignore | 2 +- CMakeLists.txt | 5 +- LICENSE | 21 ------- VarUtils.c | 11 ---- VarUtils.h | 2 - mcpc.c | 157 ++++++++++++++++++++++++++++++++++++++++--------- 6 files changed, 132 insertions(+), 66 deletions(-) delete mode 100644 LICENSE delete mode 100644 VarUtils.c delete mode 100644 VarUtils.h diff --git a/.gitignore b/.gitignore index 23ce4ca..42fa774 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ +build/ CMakeLists.txt.user .directory *.autosave -build diff --git a/CMakeLists.txt b/CMakeLists.txt index 515f56f..0ba7558 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,9 @@ cmake_minimum_required(VERSION 2.8) - project(MCPC_dissect) + set(CMAKE_BUILD_TYPE DEBUG) #RelWithDebInfo) add_definitions("-DHAVE_CONFIG_H") -add_library(MCPC_dissect SHARED "mcpc.c" "VarUtils.c" "VarUtils.h") include_directories("/usr/include/glib-2.0" "/usr/lib/glib-2.0/include" "/usr/include/wireshark") + +add_library(MCPC_dissect SHARED "mcpc.c") diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 9d3921a..0000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2018 Igor Yourasov - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/VarUtils.c b/VarUtils.c deleted file mode 100644 index 1bf50e2..0000000 --- a/VarUtils.c +++ /dev/null @@ -1,11 +0,0 @@ -#include -uint8_t VarIntToInt(char *loc, int32_t *to){ - static uint8_t ind; - ind=0; - do{ - *to |= (loc[ind]&0x7F) << (ind*7); - if(ind>5) - break; - }while((loc[ind++]&0x80) != 0); - return ind; -} diff --git a/VarUtils.h b/VarUtils.h deleted file mode 100644 index 25222df..0000000 --- a/VarUtils.h +++ /dev/null @@ -1,2 +0,0 @@ -#include -uint8_t VarIntToInt(char *loc, int32_t *to); diff --git a/mcpc.c b/mcpc.c index 35b2ab2..588fbd0 100644 --- a/mcpc.c +++ b/mcpc.c @@ -9,54 +9,150 @@ #include #include #include +#include #ifndef ENABLE_STATIC -WS_DLL_PUBLIC_DEF const gchar plugin_version[] = "0.0.1-pre-b6"; -WS_DLL_PUBLIC_DEF const gchar plugin_release[] = "2.6"; //VERSION_RELEASE +WS_DLL_PUBLIC_DEF const gchar plugin_version[] = "0.0.2-pre-b1"; +WS_DLL_PUBLIC_DEF const gchar plugin_release[] = "3.2"; //VERSION_RELEASE +WS_DLL_PUBLIC_DEF const int plugin_want_major = WIRESHARK_VERSION_MAJOR; +WS_DLL_PUBLIC_DEF const int plugin_want_minor = WIRESHARK_VERSION_MINOR; #endif #define PROTO_PORT 25565 #define PROTO_TAG "MCPC" +#define PROTO_TAG_PARTIAL "MCPC partial" -#include "VarUtils.h" +//#include "VarUtils.h" #include static int proto_mcpc=-1; +static dissector_handle_t mcpc_handle; -struct delicious{ - size_t flat; - char *chest; -}; - -// "_U_" not using -static int dissect_mcpc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree _U_, void *data _U_){ -col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTO_TAG); -static int32_t u=0; -static uint8_t l; -static guint plen=tvb_reported_length(tvb); - -l=VarIntToInt(tvb_get_ptr(tvb, pinfo->desegment_offset, plen), &u); - -if(u>pinfo->fd->pkt_len){ - u=0; - static const char *gb; - gb=tvb_get_ptr(tvb, pinfo->desegment_offset, plen); - l=VarIntToInt(gb, &u); +int8_t VarIntToUint(const guint8 *varint, uint32_t *result, uint_fast8_t maxlen){ + int8_t i=0; + *result=0; + do{ + if(i>5) + break; + *result |= (varint[i]&0x7F) << (i*7); + if(i>maxlen) + return -1; + }while((varint[i++]&0x80) != 0); + return i; } -static char buf[32]; -if(l>5) - col_set_str(pinfo->cinfo, COL_INFO, "VarInt parse error"); -else{ + +static guint getlen(packet_info *pinfo, tvbuff_t *tvb, int offset _U_, void *data _U_){ + int8_t ret; + uint32_t len; + guint8 packet_length; + packet_length=tvb_reported_length(tvb);//To read + if(packet_length==0) + return 0; +// return tvb_captured_length(tvb); + + const guint8 *dt; + dt=tvb_get_ptr(tvb, pinfo->desegment_offset, packet_length); + ret=VarIntToUint(dt, &len, packet_length); + if(ret==-1) + return 0; + else if(ret==0){ + return 1; + }else + return len+ret; +} + +static int subdissect_mcpc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree _U_, void *data _U_){ + col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTO_TAG);//Set MCPC protocol tag + + uint32_t protocol_length, varint; + guint8 packet_length, readed; + int8_t varlen; + const guint8 *dt; + + packet_length=tvb_reported_length(tvb);//To read + + dt=tvb_get_ptr(tvb, pinfo->desegment_offset, packet_length); + + readed=VarIntToUint(dt, &protocol_length, packet_length); + if(readed<0) + return -1; + else if(packet_lengthfd->pkt_len){ + varlen=VarIntToUint(dt+readed, &varint, packet_length-readed); + if(varlen>0) + readed+=varlen; + else + return -1; + if(varlen>5) + return 0; + } + + static char buf[32]; // if(u>10000) // __asm("int $3"); - sprintf(buf, "Len: %u", u); + if(pinfo->destport==25565) + sprintf(buf, "Result: [C->S] %u bytes, 0x%X", packet_length, varint); + else + sprintf(buf, "Result: [S->C] %u bytes, 0x%X"/*Length: %u*/, packet_length, varint); col_set_str(pinfo->cinfo, COL_INFO, buf); + + return tvb_captured_length(tvb); } -return tvb_captured_length(tvb); +static int dissect_mcpc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data){ + tcp_dissect_pdus(tvb, pinfo, tree, TRUE, 0, + getlen, subdissect_mcpc, data); + return tvb_captured_length(tvb); } +/*static int dissect_mcpc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree _U_, void *data _U_){ + col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTO_TAG);//Set MCPC protocol tag -static dissector_handle_t mcpc_handle; + uint32_t protocol_length, varint; + guint8 packet_length, readed; + int8_t varlen; + + packet_length=tvb_reported_length(tvb);//To read + if(packet_length==0) + return tvb_captured_length(tvb); + + const guint8 *dt; + dt=tvb_get_ptr(tvb, pinfo->desegment_offset, packet_length); + + readed=VarIntToUint(dt, &protocol_length, packet_length); + if(readed<0) + return -1; + else if(packet_lengthfd->pkt_len){ + varlen=VarIntToUint(dt, &varint, packet_length-readed); + if(varlen>0) + readed+=varlen; + else + return -1; + if(varlen>5) + return 0; + } + + static char buf[32]; + if(varlen>5) + col_set_str(pinfo->cinfo, COL_INFO, "varint parse error"); + else{ +// if(u>10000) +// __asm("int $3"); + if(pinfo->destport==25565) + sprintf(buf, "[C->S] Length: %u", packet_length); + else + sprintf(buf, "[S->C] Length: %u", packet_length); + col_set_str(pinfo->cinfo, COL_INFO, buf); + } + return tvb_captured_length(tvb); +}*/ + + +//Protocol register functions static void proto_reg_handoff_mcpc(void) { mcpc_handle = create_dissector_handle(dissect_mcpc, proto_mcpc); @@ -68,6 +164,9 @@ static void proto_register_mcpc(){ "Minecraft", "mcpc"); } + + +//Plugin register function #ifndef ENABLE_STATIC //#if 0 WS_DLL_PUBLIC void plugin_register(){