FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

ShellcodeConverter finished · hsheric0210/StealthModule.NET@0bd60c2 · GitHub

Commit 0bd60c2

Browse files
committed
ShellcodeConverter finished
This will replace the old and errorous Out-Shellcode.ps1 script
1 parent 0425c74 commit 0bd60c2

2 files changed

Lines changed: 102 additions & 2 deletions

File tree

‎lib/ShellcodeConverter/Program.cs‎

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,102 @@
1-
using System;
1+
using StealthModule;
2+
using StealthModule.Native.PE;
3+
using System;
4+
using System.Globalization;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Runtime.InteropServices;
8+
using System.Text.RegularExpressions;
29

310
namespace ShellcodeConverter
411
{
512
internal class Program
613
{
14+
private static void PrintSyntax()
15+
{
16+
Console.WriteLine("ShellCodeConverter (Out-Shellcode.ps1 ported to C# and StealthModule.NET) [original Out-Shellcode.ps1 by Matt Graeber @mattifestation]");
17+
Console.WriteLine("Syntax: ShellcodeConverter <input PE file path> <input .map file> <output file path>");
18+
}
19+
720
static void Main(string[] args)
821
{
9-
Console.WriteLine("Hello, World!");
22+
if (args.Length != 3)
23+
{
24+
PrintSyntax();
25+
return;
26+
}
27+
28+
var inputPEFile = args[0];
29+
var inputMapFile = args[1];
30+
var outputFile = args[2];
31+
32+
if (!File.Exists(inputPEFile))
33+
{
34+
Console.WriteLine("Input PE file does not exist.");
35+
return;
36+
}
37+
38+
if (!File.Exists(inputMapFile))
39+
{
40+
Console.WriteLine("Input .map file does not exist.");
41+
return;
42+
}
43+
44+
var peBytes = File.ReadAllBytes(inputPEFile);
45+
var peBuffer = Marshal.AllocHGlobal(peBytes.Length);
46+
try
47+
{
48+
Marshal.Copy(peBytes, 0, peBuffer, peBytes.Length);
49+
var header = new PEHeader(peBuffer);
50+
51+
var found = false;
52+
ImageSectionHeader textSection = default;
53+
foreach (var section in header.Sections)
54+
{
55+
var sectionName = Structs.LongTo8byteString(section.Name);
56+
Console.WriteLine("Section: " + sectionName);
57+
if (sectionName.StartsWith(".text", StringComparison.Ordinal))
58+
{
59+
found = true;
60+
textSection = section;
61+
break;
62+
}
63+
}
64+
65+
if (!found)
66+
{
67+
Console.WriteLine("'.text' section not found.");
68+
return;
69+
}
70+
71+
var lineMatcher = new Regex(@".*([\dabcdef]{8})H.*\.text(?:\$\w+)?.*\W+CODE", RegexOptions.Compiled, TimeSpan.FromMilliseconds(100));
72+
var mapLines = File.ReadAllLines(inputMapFile);
73+
var textSectionLengthText = mapLines
74+
.Select(line => lineMatcher.Match(line))
75+
.Where(match => match.Success)
76+
.Select(match => match.Groups[1].Value)
77+
.FirstOrDefault();
78+
79+
Console.WriteLine("'.text' section length is " + textSectionLengthText);
80+
81+
if (textSectionLengthText == null || !int.TryParse(textSectionLengthText, NumberStyles.HexNumber, NumberFormatInfo.CurrentInfo, out var shellCodeLength))
82+
{
83+
Console.WriteLine("'.text' section length is unavailable. (" + textSectionLengthText + ")");
84+
return;
85+
}
86+
87+
var shellCode = new byte[shellCodeLength];
88+
var textSectionBegin = textSection.PointerToRawData;
89+
Buffer.BlockCopy(peBytes, (int)textSectionBegin, shellCode, 0, shellCodeLength);
90+
91+
Console.WriteLine("Shellcode extracted: offset=" + textSectionBegin + " length=" + shellCodeLength);
92+
93+
File.WriteAllBytes(outputFile, shellCode);
94+
Console.WriteLine("Done writing to " + outputFile);
95+
}
96+
finally
97+
{
98+
Marshal.FreeHGlobal(peBuffer);
99+
}
10100
}
11101
}
12102
}

‎lib/ShellcodeConverter/ShellcodeConverter.csproj‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,14 @@
66
<Platforms>x64;x86</Platforms>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<PackageReference Include="Costura.Fody" Version="5.7.0">
11+
<PrivateAssets>all</PrivateAssets>
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
</PackageReference>
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\..\StealthModule\StealthModule.csproj" />
18+
</ItemGroup>
919
</Project>

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL