# Shellcode Reflective DLL Injection

Shellcode reflective DLL injection (sRDI) is a technique that allows converting a given DLL into a position independent shellcode that can then be injected using your favourite shellcode injection and execution technique. In this lab I wanted to try this technique as I think it is an amazing technique to have in your arsenal.

In this lab, I'm playing with the amazing <https://github.com/monoxgas/sRDI> written by monoxgas from Silent Break Security.

## Execution

Let's compile a simple x86 DLL - in my case, an odd DLL that pops 2 notepad processes when executed:

![](https://386337598-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFEMnER3fywgFHoroYn%2F-LjSvRKitqquU9hLq-KD%2F-LjSvUBjVqzB_3nceoWi%2Fimage.png?alt=media\&token=8f0e05ca-c4a1-44f4-a1d4-c505770b27e2)

Convert the DLL into shellcode. We will get an array of shellcode bytes represented in decimal values:

```csharp
$sc = ConvertTo-Shellcode \\VBOXSVR\Experiments\messagebox\messagebox\Debug\messagebox.dll
```

![](https://386337598-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFEMnER3fywgFHoroYn%2F-LjSc2tUq-UeoaSiDnaK%2F-LjScZ1IsecIta-eRGP9%2Fimage.png?alt=media\&token=7ee1e4ac-06fa-4b14-9b91-8918db89efbe)

Let's convert them to hex:

```csharp
$sc2 = $sc | % { write-output ([System.String]::Format('{0:X2}', $_)) }
```

![](https://386337598-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFEMnER3fywgFHoroYn%2F-LjSc2tUq-UeoaSiDnaK%2F-LjSdspQB8-dVUn0PySt%2Fimage.png?alt=media\&token=8ed55723-cde3-4410-b421-be390ff0117b)

Join them all and print to a text file:

```
$sc2 -join "" > shell.txt
```

![](https://386337598-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFEMnER3fywgFHoroYn%2F-LjSc2tUq-UeoaSiDnaK%2F-LjSpDUX4et_bGhXqEjU%2Fimage.png?alt=media\&token=566149af-3e0e-4ead-a602-9dd074ffb478)

Create a new binary file with the shellcode we got earlier - just copy the hex string (as seen in the above screenshot) and paste it to a new file using HxD hex editor:

![](https://386337598-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFEMnER3fywgFHoroYn%2F-LjSvRKitqquU9hLq-KD%2F-LjSw2nFPXwgHVVMttmd%2Fimage.png?alt=media\&token=09ff06f3-b85d-4484-a451-b1a30cf2a7be)

In order to load and execute the shellcode, we will place it in the binary as a resource as described in my other lab [Loading and Executing Shellcode From PE Resources](https://www.ired.team/offensive-security/code-injection-process-injection/loading-and-executing-shellcode-from-portable-executable-resources):

![](https://386337598-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFEMnER3fywgFHoroYn%2F-LjSvRKitqquU9hLq-KD%2F-LjSw86-sEyrM6zkGbz3%2Fimage.png?alt=media\&token=f9d16a62-976e-4b3a-8f40-264ffdc93976)

Compile and run the binary. If the shellcode runs successfully, we should see two notepad.exe processes popup:

![](https://386337598-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFEMnER3fywgFHoroYn%2F-LjSvRKitqquU9hLq-KD%2F-LjSwX8r33rMWYenuqnq%2Fpop-2notepads.gif?alt=media\&token=58cb1eef-4710-46ab-b7be-bc3baa5a5cb6)

## References

{% embed url="<https://github.com/monoxgas/sRDI/tree/master/PowerShell>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.ired.team/offensive-security/code-injection-process-injection/reflective-shellcode-dll-injection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
