Skip to content

Commit f5d2ff1

Browse files
authored
Merge pull request NytroRST#2 from kozlovLG/master
Exception handling when testing the shellcode.
2 parents f1c139f + 5ee9402 commit f5d2ff1

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

ShellcodeCompiler/DebugUtils.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11

22
#include "DebugUtils.h"
3+
#include "SEHUtils.h"
34

45
// Dump all data - debug purposes
56

7+
8+
69
void DebugUtils::DumpAllData()
710
{
811
cout << endl;
@@ -53,7 +56,16 @@ void DebugUtils::TestShellcode(string p_sFilename)
5356
}
5457

5558
// Copy shellcode and execute it
56-
57-
memcpy(sc, p, size);
58-
(*(int(*)()) sc)();
59+
try
60+
{
61+
_set_se_translator(CxxTranslateSehException);
62+
memcpy(sc, p, size);
63+
(*(int(*)()) sc)();
64+
}
65+
catch (const seh_exception& e)
66+
{
67+
cout << "Error when executing shellcode: "
68+
<< e.what() << endl;
69+
}
5970
}
71+

ShellcodeCompiler/SEHUtils.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef SEHUTILIS_H
2+
#define SEHUTILIS_H
3+
#include <windows.h>
4+
#include <exception>
5+
6+
struct seh_exception : public std::exception
7+
{
8+
seh_exception(const char* message, DWORD code) : exception(message), m_code(code) {}
9+
10+
DWORD Code() const { return m_code; }
11+
12+
private:
13+
const DWORD m_code;
14+
};
15+
16+
void CxxTranslateSehException(unsigned int, EXCEPTION_POINTERS* pointers)
17+
{
18+
DWORD exceptionCode = pointers->ExceptionRecord->ExceptionCode;
19+
const char* msg = nullptr;
20+
21+
switch (exceptionCode) {
22+
case EXCEPTION_ACCESS_VIOLATION:
23+
msg = "Access violation.";
24+
break;
25+
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
26+
msg = "Out of bound access.";
27+
break;
28+
case EXCEPTION_ILLEGAL_INSTRUCTION:
29+
msg = "Illegal instruction.";
30+
break;
31+
default:
32+
break;
33+
}
34+
throw seh_exception(msg, exceptionCode);
35+
}
36+
37+
38+
#endif

ShellcodeCompiler/ShellcodeCompiler.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
<FunctionLevelLinking>true</FunctionLevelLinking>
135135
<IntrinsicFunctions>true</IntrinsicFunctions>
136136
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
137+
<ExceptionHandling>Async</ExceptionHandling>
137138
</ClCompile>
138139
<Link>
139140
<SubSystem>Console</SubSystem>
@@ -157,6 +158,7 @@
157158
<ClInclude Include="DLLBaseAddress.h" />
158159
<ClInclude Include="FunctionCalls.h" />
159160
<ClInclude Include="FunctionOffsetAddress.h" />
161+
<ClInclude Include="SEHUtils.h" />
160162
<ClInclude Include="stdafx.h" />
161163
<ClInclude Include="StringOffsetAddress.h" />
162164
<ClInclude Include="targetver.h" />

ShellcodeCompiler/ShellcodeCompiler.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
<ClInclude Include="Compile.h">
6464
<Filter>Header Files</Filter>
6565
</ClInclude>
66+
<ClInclude Include="SEHUtils.h">
67+
<Filter>Header Files</Filter>
68+
</ClInclude>
6669
</ItemGroup>
6770
<ItemGroup>
6871
<ClCompile Include="stdafx.cpp">

0 commit comments

Comments
 (0)