This repository has been archived by the owner on Aug 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
pluginmain.cpp
147 lines (137 loc) · 4.11 KB
/
pluginmain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <windows.h>
#include "pluginsdk\_plugins.h"
#include "pluginsdk\_exports.h" // modified _exports.h to use _dbg_addrinfoget export
#include "pluginsdk\bridgemain.h"
#include "utf8\utf8.h"
#include "resource.h"
#ifdef _WIN64
#pragma comment(lib, "pluginsdk\\x64dbg.lib")
#pragma comment(lib, "pluginsdk\\x64bridge.lib")
#else
#pragma comment(lib, "pluginsdk\\x32dbg.lib")
#pragma comment(lib, "pluginsdk\\x32bridge.lib")
#endif
#ifndef DLL_EXPORT
#define DLL_EXPORT extern "C" __declspec(dllexport)
#endif //DLL_EXPORT
#define plugin_version 1
int pluginHandle;
HWND hwndDlg;
HINSTANCE hModule;
std::string LoadUTF8String(int index)
{
wchar_t p[512];
int len;
memset(p, 0, sizeof(p));
if((len = LoadString(hModule, index, (LPWSTR)p, 512)) == 0)
{
return "";
}
else
{
std::wstring utf16line(p, len);
std::string utf8line;
utf8::utf16to8(utf16line.begin(), utf16line.end(), std::back_inserter(utf8line));
return utf8line;
}
}
int compareFunc(const void* a, const void* b)
{
XREF_RECORD* A, *B;
A = (XREF_RECORD*)a;
B = (XREF_RECORD*)b;
if(A->type > B->type)
return 1;
else if(A->type < B->type)
return -1;
else if(A->addr > B->addr)
return 1;
else if(A->addr < B->addr)
return -1;
else
return 0;
}
void cbPlugin(CBTYPE cbType, LPVOID generic_param)
{
PLUG_CB_SELCHANGED* param = (PLUG_CB_SELCHANGED*)generic_param;
if(param->hWindow == GUI_DISASSEMBLY)
{
XREF_INFO info;
info.refcount = 0;
info.references = nullptr;
if(DbgXrefGet(param->VA, &info) && info.refcount > 0)
{
std::string output;
XREF_RECORD* data;
data = new XREF_RECORD[info.refcount];
memcpy(data, info.references, info.refcount * sizeof(XREF_RECORD));
std::qsort(data, info.refcount, sizeof(info.references[0]), &compareFunc);
int t = XREF_NONE;
duint i;
for(i = 0; i < info.refcount && i < 10; i++)
{
if(t != data[i].type)
{
switch(data[i].type)
{
case XREF_JMP:
output += LoadUTF8String(IDS_JMPFROM);
break;
case XREF_CALL:
output += LoadUTF8String(IDS_CALLFROM);
break;
default:
output += LoadUTF8String(IDS_REFFROM);
break;
}
t = data[i].type;
}
ADDRINFO label;
label.flags = flaglabel;
_dbg_addrinfoget(data[i].addr, SEG_DEFAULT, &label);
if(label.label[0] != '\0')
output += label.label;
else{
char temp[18];
sprintf_s(temp, "%p", data[i].addr);
output += temp;
}
if(i != info.refcount - 1)
output += ",";
}
delete[] data;
if(info.refcount > 10)
output += " ...";
GuiAddInfoLine(output.c_str());
}
if(info.refcount != 0)
BridgeFree(info.references);
}
}
DLL_EXPORT bool pluginit(PLUG_INITSTRUCT* initStruct)
{
initStruct->pluginVersion = plugin_version;
initStruct->sdkVersion = PLUG_SDKVERSION;
strcpy_s(initStruct->pluginName, LoadUTF8String(IDS_PLUGINAME).c_str());
pluginHandle = initStruct->pluginHandle;
return true;
}
DLL_EXPORT bool plugstop()
{
_plugin_unregistercallback(pluginHandle, CB_SELCHANGED);
return true;
}
DLL_EXPORT void plugsetup(PLUG_SETUPSTRUCT* setupStruct)
{
hwndDlg = setupStruct->hwndDlg;
_plugin_registercallback(pluginHandle, CB_SELCHANGED, &cbPlugin);
}
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if(fdwReason == DLL_PROCESS_ATTACH)
{
hModule = hinstDLL;
DisableThreadLibraryCalls(hinstDLL);
}
return TRUE;
}