From c9620caa20ba8309ed4c2d4d6de0d2f2acc93d10 Mon Sep 17 00:00:00 2001 From: _xeroxz Date: Thu, 3 Jun 2021 21:36:34 -0700 Subject: [PATCH] jcc's are working good, fixed a bug with RSI-0x1 --- .../framelesswindow/framelesswindow.ui | 2 +- src/qvirt_instrs.cpp | 11 ++ src/qvirt_instrs.h | 16 ++ src/qvminspector.cpp | 138 +++++++++++++++++- src/qvminspector.h | 8 +- src/vmprofiler-qt.vcxproj | 4 +- src/vmprofiler-qt.vcxproj.filters | 6 + 7 files changed, 179 insertions(+), 6 deletions(-) create mode 100644 src/qvirt_instrs.cpp create mode 100644 src/qvirt_instrs.h diff --git a/src/DarkStyle/framelesswindow/framelesswindow.ui b/src/DarkStyle/framelesswindow/framelesswindow.ui index a98d7ae..3da636e 100644 --- a/src/DarkStyle/framelesswindow/framelesswindow.ui +++ b/src/DarkStyle/framelesswindow/framelesswindow.ui @@ -6,7 +6,7 @@ 0 0 - 1484 + 1642 983 diff --git a/src/qvirt_instrs.cpp b/src/qvirt_instrs.cpp new file mode 100644 index 0000000..3918bd3 --- /dev/null +++ b/src/qvirt_instrs.cpp @@ -0,0 +1,11 @@ +#include "qvirt_instrs.h" + +qvirt_instrs_t::qvirt_instrs_t( qvminspector_t *vminspector ) : vminspector( vminspector ), ui( &vminspector->ui ) +{ + connect( ui->virt_instrs, &QTreeWidget::itemExpanded, this, &qvirt_instrs_t::on_expand ); +} + +void qvirt_instrs_t::on_expand( qtree_widget_item_t *item ) +{ + +} \ No newline at end of file diff --git a/src/qvirt_instrs.h b/src/qvirt_instrs.h new file mode 100644 index 0000000..6636ae3 --- /dev/null +++ b/src/qvirt_instrs.h @@ -0,0 +1,16 @@ +#pragma once +#include "qvminspector.h" + +class qvirt_instrs_t : public QObject +{ + Q_OBJECT + public: + explicit qvirt_instrs_t( qvminspector_t *vminspector ); + + private: + Ui::QVMProfilerClass *ui; + qvminspector_t *vminspector; + + private slots: + void on_expand( qtree_widget_item_t *item ); +}; \ No newline at end of file diff --git a/src/qvminspector.cpp b/src/qvminspector.cpp index 7ceda64..c29f6d0 100644 --- a/src/qvminspector.cpp +++ b/src/qvminspector.cpp @@ -3,7 +3,7 @@ qvminspector_t::qvminspector_t( qwidget_t *parent ) : qmain_window_t( parent ), file_header( nullptr ), vmctx( nullptr ) { ui.setupUi( this ); - ui.virt_instrs->setColumnWidth( 0, 115 ); + ui.virt_instrs->setColumnWidth( 0, 175 ); ui.virt_instrs->setColumnWidth( 1, 140 ); ui.virt_instrs->setColumnWidth( 2, 200 ); ui.virt_instrs->setColumnWidth( 3, 200 ); @@ -125,6 +125,117 @@ bool qvminspector_t::init_data() return true; } +void qvminspector_t::add_branch_children( qtree_widget_item_t *item, std::uintptr_t branch_addr ) +{ + if ( std::find( code_block_addrs.begin(), code_block_addrs.end(), branch_addr ) != code_block_addrs.end() ) + return; + + code_block_addrs.push_back( branch_addr ); + + // for each code block find the one that starts with the desired branch... + for ( auto [ code_block, code_block_num ] = std::tuple{ first_block, 0u }; + code_block_num < file_header->code_block_count; + code_block = reinterpret_cast< vmp2::v3::code_block_t * >( reinterpret_cast< std::uintptr_t >( code_block ) + + code_block->next_block_offset ), + ++code_block_num ) + { + // skip if this code block begin address is not branch_addr... + if ( code_block->vip_begin != branch_addr && code_block->vip_begin != branch_addr - 2 && + code_block->vip_begin != branch_addr - 1) + continue; + + dbg_print( qstring_t( "> code block %1 (block_%2), number of vinstrs = %3..." ) + .arg( code_block_num ) + .arg( ( code_block->vip_begin - file_header->module_base ) + file_header->image_base, 0, 16 ) + .arg( code_block->vinstr_count ) ); + + // for each virtual instruction inside of this code block... + for ( auto idx = 0u; idx < code_block->vinstr_count; ++idx ) + { + const auto virt_instr = &code_block->vinstr[ idx ]; + const auto profile = vm::handler::get_profile( virt_instr->mnemonic_t ); + auto virt_instr_entry = new qtree_widget_item_t; + + // virtual instruction image base'ed rva... (column 1)... + virt_instr_entry->setText( 0, qstring_t::number( ( virt_instr->trace_data.vip - file_header->module_base ) + + file_header->image_base, + 16 ) ); + + // virtual instruction operand bytes... (column 2)... + qstring_t operand_bytes; + operand_bytes.append( QString( "%1" ).arg( virt_instr->opcode, 0, 16 ) ); + + // if virt instruction has an imm... grab its bytes... + if ( virt_instr->operand.has_imm ) + { + operand_bytes.append( " - " ); + for ( auto _idx = 0u; _idx < virt_instr->operand.imm.imm_size / 8; ++_idx ) + operand_bytes.append( QString( "%1 " ).arg( + reinterpret_cast< std::uint8_t * >( &virt_instr->operand.imm.u )[ _idx ], 0, 16 ) ); + } + + virt_instr_entry->setText( 1, operand_bytes ); + + // virtual instruction string, includes imm... (colume 3)... + QString decoded_instr( QString( "%1" ).arg( + profile ? profile->name : QString( "UNK(%1)" ).arg( virt_instr->opcode, 0, 16 ) ) ); + + if ( virt_instr->operand.has_imm ) // if there is a second operand (imm) put its value... + decoded_instr.append( QString( " %1" ).arg( virt_instr->operand.imm.u, 0, 16 ) ); + + virt_instr_entry->setText( 2, decoded_instr ); + + // add comments to the virtual instruction... (colume 4)... + if ( virt_instr->mnemonic_t == vm::handler::LREGQ || virt_instr->mnemonic_t == vm::handler::SREGQ ) + virt_instr_entry->setText( 3, QString( "; vreg%1..." ) + .arg( virt_instr->operand.imm.u ? ( virt_instr->operand.imm.u / 8 ) - + 1 /* zero based vreg... */ + : 0u ) ); + + if ( virt_instr->mnemonic_t == vm::handler::JMP && code_block->jcc.has_jcc ) + { + virt_instr_entry->setText( + 3, + QString( "; { %1, %2 }" ) + .arg( ( code_block->jcc.block_addr[ 0 ] - file_header->module_base ) + file_header->image_base, + 0, 16 ) + .arg( ( code_block->jcc.block_addr[ 1 ] - file_header->module_base ) + file_header->image_base, + 0, 16 ) ); + + auto branch_entry1 = new qtree_widget_item_t(), branch_entry2 = new qtree_widget_item_t(); + const auto block1_addr = + ( code_block->jcc.block_addr[ 0 ] - file_header->module_base ) + file_header->image_base; + + const auto block2_addr = + ( code_block->jcc.block_addr[ 1 ] - file_header->module_base ) + file_header->image_base; + + branch_entry1->setText( 0, QString( "%1" ).arg( block1_addr, 0, 16 ) ); + branch_entry1->setText( 3, QString( "; block_%1" ).arg( block1_addr, 0, 16 ) ); + + branch_entry2->setText( 0, QString( "%1" ).arg( block2_addr, 0, 16 ) ); + branch_entry2->setText( 3, QString( "; block_%1" ).arg( block2_addr, 0, 16 ) ); + + add_branch_children( branch_entry1, code_block->jcc.block_addr[ 0 ] ); + add_branch_children( branch_entry2, code_block->jcc.block_addr[ 1 ] ); + + virt_instr_entry->addChildren( { branch_entry1, branch_entry2 } ); + // if its a JMP with branches we want to insert the next code block + // instructions into the child widget entries... + item->addChild( virt_instr_entry ); + return; + } + else if ( virt_instr->mnemonic_t == vm::handler::JMP ) + { + // else if this jmp doesnt have two branches add the next code block to it... + item->addChild( virt_instr_entry ); + add_branch_children( item, code_block->jcc.block_addr[ 0 ] ); + } + + item->addChild( virt_instr_entry ); + } + } +} + void qvminspector_t::update_ui() { // for each code block insert their virtual instructions @@ -138,6 +249,7 @@ void qvminspector_t::update_ui() code_block->next_block_offset ), ++code_block_num ) { + code_block_addrs.push_back( ( code_block->vip_begin - file_header->module_base ) + file_header->image_base ); dbg_print( qstring_t( "> code block %1 (block_%2), number of vinstrs = %3..." ) .arg( code_block_num ) .arg( ( code_block->vip_begin - file_header->module_base ) + file_header->image_base, 0, 16 ) @@ -187,6 +299,7 @@ void qvminspector_t::update_ui() : 0u ) ); if ( virt_instr->mnemonic_t == vm::handler::JMP && code_block->jcc.has_jcc ) + { virt_instr_entry->setText( 3, QString( "; { %1, %2 }" ) @@ -195,6 +308,29 @@ void qvminspector_t::update_ui() .arg( ( code_block->jcc.block_addr[ 1 ] - file_header->module_base ) + file_header->image_base, 0, 16 ) ); + auto branch_entry1 = new qtree_widget_item_t(), branch_entry2 = new qtree_widget_item_t(); + const auto block1_addr = + ( code_block->jcc.block_addr[ 0 ] - file_header->module_base ) + file_header->image_base; + + const auto block2_addr = + ( code_block->jcc.block_addr[ 1 ] - file_header->module_base ) + file_header->image_base; + + branch_entry1->setText( 0, QString( "%1" ).arg( block1_addr, 0, 16 ) ); + branch_entry1->setText( 3, QString( "; block_%1" ).arg( block1_addr, 0, 16 ) ); + + branch_entry2->setText( 0, QString( "%1" ).arg( block2_addr, 0, 16 ) ); + branch_entry2->setText( 3, QString( "; block_%1" ).arg( block2_addr, 0, 16 ) ); + + add_branch_children( branch_entry1, code_block->jcc.block_addr[ 0 ] ); + add_branch_children( branch_entry2, code_block->jcc.block_addr[ 1 ] ); + + virt_instr_entry->addChildren( { branch_entry1, branch_entry2 } ); + // if its a JMP with branches we want to insert the next code block + // instructions into the child widget entries... + ui.virt_instrs->addTopLevelItem( virt_instr_entry ); + return; + } + ui.virt_instrs->addTopLevelItem( virt_instr_entry ); } } diff --git a/src/qvminspector.h b/src/qvminspector.h index c4c813d..3c67d84 100644 --- a/src/qvminspector.h +++ b/src/qvminspector.h @@ -9,8 +9,8 @@ #include #include "ia32.hpp" -#include "vmp2.hpp" #include "ui_qvminspector.h" +#include "vmp2.hpp" using qmain_window_t = QMainWindow; using qwidget_t = QWidget; @@ -21,8 +21,8 @@ using qmsg_box_t = QMessageBox; class qvminspector_t : public qmain_window_t { + friend class qvirt_instrs_t; Q_OBJECT - public: qvminspector_t( qwidget_t *parent = Q_NULLPTR ); @@ -34,6 +34,7 @@ class qvminspector_t : public qmain_window_t void dbg_print( qstring_t DbgOutput ); void dbg_msg( qstring_t DbgOutput ); void update_ui(); + void add_branch_children( qtree_widget_item_t *item, std::uintptr_t branch_addr ); bool init_data(); Ui::QVMProfilerClass ui; @@ -44,4 +45,5 @@ class qvminspector_t : public qmain_window_t vmp2::v3::file_header *file_header; vmp2::v3::code_block_t *first_block; -}; + std::vector< std::uintptr_t > code_block_addrs; +}; \ No newline at end of file diff --git a/src/vmprofiler-qt.vcxproj b/src/vmprofiler-qt.vcxproj index b08284e..13a199e 100644 --- a/src/vmprofiler-qt.vcxproj +++ b/src/vmprofiler-qt.vcxproj @@ -103,6 +103,8 @@ + + @@ -112,7 +114,6 @@ - @@ -168,6 +169,7 @@ + diff --git a/src/vmprofiler-qt.vcxproj.filters b/src/vmprofiler-qt.vcxproj.filters index 2de0714..fd82560 100644 --- a/src/vmprofiler-qt.vcxproj.filters +++ b/src/vmprofiler-qt.vcxproj.filters @@ -16,6 +16,9 @@ Source Files + + Source Files + @@ -90,6 +93,9 @@ Header Files + + Header Files +