Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
basic relocations view
Browse files Browse the repository at this point in the history
  • Loading branch information
zodiacon committed Apr 25, 2022
1 parent cd3bb78 commit 8cb016d
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 4 deletions.
23 changes: 23 additions & 0 deletions TotalPE/PEStrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,29 @@ PCWSTR PEStrings::DebugTypeToString(DWORD type) {
return L"(Reserved)";
}

PCWSTR PEStrings::x64RelocationTypeToString(BYTE type) {
switch (type) {
case IMAGE_REL_AMD64_ABSOLUTE: return L"Absolute";
case IMAGE_REL_AMD64_ADDR64: return L"64-bit Address";
case IMAGE_REL_AMD64_ADDR32: return L"32-bit Address";
case IMAGE_REL_AMD64_ADDR32NB: return L"32-bit Address without Image Base";
case IMAGE_REL_AMD64_REL32: return L"32-bit relative";
case IMAGE_REL_AMD64_REL32_1: return L"32-bit relative (distance 1)";
case IMAGE_REL_AMD64_REL32_2: return L"32-bit relative (distance 2)";
case IMAGE_REL_AMD64_REL32_3: return L"32-bit relative (distance 3)";
case IMAGE_REL_AMD64_REL32_4: return L"32-bit relative (distance 4)";
case IMAGE_REL_AMD64_REL32_5: return L"32-bit relative (distance 5)";
case IMAGE_REL_AMD64_SECTION: return L"Section Index";
case IMAGE_REL_AMD64_SECREL: return L"32-bit Offset from Target Section";
case IMAGE_REL_AMD64_SECREL7: return L"7-bit Unsigned Offset from Target Section";
case IMAGE_REL_AMD64_TOKEN: return L"32-bit Metadata Token";
case IMAGE_REL_AMD64_SREL32: return L"32 bit Signed Span-Dependent";
case IMAGE_REL_AMD64_PAIR: return L"Pair";
case IMAGE_REL_AMD64_SSPAN32: return L"32 bit Signed Span-Dependent (Link Time)";
}
return L"";
}

std::wstring PEStrings::FileFlagsToString(DWORD flags) {
static const struct {
DWORD value;
Expand Down
1 change: 1 addition & 0 deletions TotalPE/PEStrings.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ struct PEStrings abstract final {
static std::wstring FileFlagsToString(DWORD flags);
static PCWSTR DebugTypeToString(DWORD type);
static PCWSTR CertificateTypeToString(DWORD type);
static PCWSTR x64RelocationTypeToString(BYTE type);
};

37 changes: 37 additions & 0 deletions TotalPE/RelocationsView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "pch.h"
#include "RelocationsView.h"
#include "PEStrings.h"
#include "SortHelper.h"

CString CRelocationsView::GetColumnText(HWND, int row, int col) const {
auto& item = m_Items[row];
switch (col) {
case 0: return PEStrings::x64RelocationTypeToString(item.type);
case 1: return std::format(L"0x{:X}", item.relative_virtual_address).c_str();
case 2: return std::to_wstring(item.relocation_id).c_str();
case 3: return std::format(L"0x{:X}", item.data).c_str();
}
return CString();
}

void CRelocationsView::DoSort(SortInfo const* si) {
}

LRESULT CRelocationsView::OnCreate(UINT, WPARAM, LPARAM, BOOL&) {
m_hWndClient = m_List.Create(m_hWnd, rcDefault, nullptr, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |
LVS_REPORT | LVS_OWNERDATA | LVS_SINGLESEL | LVS_SHOWSELALWAYS, WS_EX_CLIENTEDGE);
m_List.SetExtendedListViewStyle(LVS_EX_DOUBLEBUFFER | LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP);

auto cm = GetColumnManager(m_List);
cm->AddColumn(L"Type", LVCFMT_LEFT, 200);
cm->AddColumn(L"RVA", LVCFMT_RIGHT, 80);
cm->AddColumn(L"ID", LVCFMT_RIGHT, 100);
cm->AddColumn(L"Data", LVCFMT_RIGHT, 140);
cm->UpdateColumns();

m_Items = PE().get_relocations().get_entries();
m_List.SetItemCount((int)m_Items.size());

return 0;
}

27 changes: 27 additions & 0 deletions TotalPE/RelocationsView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "View.h"
#include <VirtualListView.h>

class CRelocationsView :
public CView<CRelocationsView>,
public CVirtualListView<CRelocationsView> {
public:
using CView::CView;

CString GetColumnText(HWND, int row, int col) const;
void DoSort(SortInfo const* si);

BEGIN_MSG_MAP(CRelocationsView)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
CHAIN_MSG_MAP(CView<CRelocationsView>)
CHAIN_MSG_MAP(CVirtualListView<CRelocationsView>)
END_MSG_MAP()

LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);

private:
CListViewCtrl m_List;
std::vector<pe_relocation_entry> m_Items;
};

17 changes: 15 additions & 2 deletions TotalPE/SecurityView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ bool CSecurityView::OnDoubleClickList(HWND, int row, int, CPoint const& pt) {
return false;

auto& item = m_Items[row];
// always fails :(
auto ctx = ::CertCreateCertificateContext(PKCS_7_ASN_ENCODING | X509_ASN_ENCODING, item.get_certificate_data().data(), (DWORD)item.get_certificate_data().size());
if (ctx) {
::CryptUIDlgViewContext(CERT_STORE_CERTIFICATE_CONTEXT, ctx, m_hWnd, nullptr, 0, nullptr);
Expand All @@ -45,8 +46,9 @@ bool CSecurityView::OnDoubleClickList(HWND, int row, int, CPoint const& pt) {
}

LRESULT CSecurityView::OnCreate(UINT, WPARAM, LPARAM, BOOL&) {
m_hWndClient = m_List.Create(*this, rcDefault, nullptr, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |
LVS_REPORT | LVS_OWNERDATA, WS_EX_CLIENTEDGE);
m_hWndClient = m_Splitter.Create(m_hWnd, rcDefault, nullptr, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPSIBLINGS);
m_List.Create(m_Splitter, rcDefault, nullptr, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |
LVS_REPORT | LVS_OWNERDATA | LVS_SINGLESEL | LVS_SHOWSELALWAYS, WS_EX_CLIENTEDGE);
m_List.SetExtendedListViewStyle(LVS_EX_DOUBLEBUFFER | LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP);

auto cm = GetColumnManager(m_List);
Expand All @@ -55,9 +57,20 @@ LRESULT CSecurityView::OnCreate(UINT, WPARAM, LPARAM, BOOL&) {
cm->AddColumn(L"Data Size", LVCFMT_RIGHT, 100);
cm->UpdateColumns();

m_HexView.Create(m_Splitter, rcDefault, nullptr, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPSIBLINGS);
m_HexView.SetDynamicAlloc(false);
m_Splitter.SetSplitterPanes(m_List, m_HexView);
m_Splitter.SetSplitterPosPct(15);

m_Items = PE().get_security().get_certificates();
ATLASSERT(PE().get_security().get_certificates_count() == (int)m_Items.size());
m_List.SetItemCount((int)m_Items.size());

return 0;
}

void CSecurityView::OnStateChanged(HWND h, int from, int to, DWORD oldState, DWORD newState) {
if (from >= 0 && (newState & LVIS_SELECTED)) {
m_HexView.SetData(m_Items[from].get_certificate_data());
}
}
6 changes: 5 additions & 1 deletion TotalPE/SecurityView.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

#include "View.h"
#include <VirtualListView.h>
#include "ReadOnlyHexView.h"

class CSecurityView :
public CView<CSecurityView>,
public CVirtualListView<CSecurityView> {
public:
using CView::CView;
CSecurityView(IMainFrame* frame, pe_image_full const& pe) : CView(frame, pe), m_HexView(frame) {}

CString GetColumnText(HWND, int row, int col) const;
void DoSort(SortInfo const* si);
bool OnDoubleClickList(HWND, int row, int, CPoint const& pt);
void OnStateChanged(HWND h, int from, int to, DWORD oldState, DWORD newState);

BEGIN_MSG_MAP(CSecurityView)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
Expand All @@ -22,7 +24,9 @@ class CSecurityView :
LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);

private:
CReadOnlyHexView m_HexView;
CListViewCtrl m_List;
CHorSplitterWindow m_Splitter;
std::vector<pe_security_entry> m_Items;
};

2 changes: 2 additions & 0 deletions TotalPE/TotalPE.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
<ClCompile Include="PEStrings.cpp" />
<ClCompile Include="ReadOnlyHexView.cpp" />
<ClCompile Include="RecentFilesManager.cpp" />
<ClCompile Include="RelocationsView.cpp" />
<ClCompile Include="ResourcesView.cpp" />
<ClCompile Include="SectionsView.cpp" />
<ClCompile Include="SecurityHelper.cpp" />
Expand Down Expand Up @@ -261,6 +262,7 @@
<ClInclude Include="PEStrings.h" />
<ClInclude Include="ReadOnlyHexView.h" />
<ClInclude Include="RecentFilesManager.h" />
<ClInclude Include="RelocationsView.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="ResourcesView.h" />
Expand Down
8 changes: 7 additions & 1 deletion TotalPE/TotalPE.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@
<ClCompile Include="SecurityView.cpp">
<Filter>Views</Filter>
</ClCompile>
<ClCompile Include="RelocationsView.cpp">
<Filter>Views</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h">
Expand Down Expand Up @@ -200,6 +203,9 @@
<ClInclude Include="SecurityView.h">
<Filter>Views</Filter>
</ClInclude>
<ClInclude Include="RelocationsView.h">
<Filter>Views</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="TotalPE.rc">
Expand Down Expand Up @@ -337,7 +343,7 @@
<Filter>Resource Files\Icons</Filter>
</Image>
<Image Include="res\relocation.ico">
<Filter>Resource Files</Filter>
<Filter>Resource Files\Icons</Filter>
</Image>
</ItemGroup>
<ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions TotalPE/ViewManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "DebugView.h"
#include "MessageTableView.h"
#include "SecurityView.h"
#include "RelocationsView.h"

ViewManager::ViewManager(IMainFrame* frame) : m_pFrame(frame) {
m_views.reserve(16);
Expand Down Expand Up @@ -88,6 +89,13 @@ HWND ViewManager::CreateOrGetView(TreeItemType type, HWND hParent, pe_image_full
break;
}

case IMAGE_DIRECTORY_ENTRY_BASERELOC:
{
auto view = new CRelocationsView(m_pFrame, pe);
hView = view->DoCreate(hParent);
break;
}

default:
// all other directories...

Expand Down

0 comments on commit 8cb016d

Please sign in to comment.