## Exercise: refactor a loader

Refactor the code in this way that it will be loading the PE into a remote process. Fill the given template, basing on the hints in the comments.

You are given templates for two elements:
+ [payload](payload) : a DLL to be loaded/injected
+ [injector](injector) : the executable that will be doing the injection

> 📌 The code  contains hints that will be guiding you through the process of refactoring.
> 
> 📌 Reuse relevant fragments of [shellcode_task_step1.cpp](../../lesson5_shellcode/c_to_shellcode/shellcode_task_step1.cpp) 
> 
### Task 0)
🏁 Start by refactoring the DLL. It should be altered in such a way that it will be able to load its own imports after it was injected to the (potentially remote) target.
Fill the function:
```cpp
bool MYAPI SelfLoadImports(LPVOID currentImageBase);
```
with the relevant content

### Task 1) Self-injection with loading imports payload-side

We need to refactor the [injector](injector) in such a way, that it will allow for loading imports payload-side.
Hints to follow:
1. This time we need to load the [payload](payload) DLL manually, and then call its exports (also manually). Copy the relevant function (`get_func_by_name`) from the  [shellcode_task_step1.cpp](../../lesson5_shellcode/c_to_shellcode/shellcode_task_step1.cpp)
2. Remove the function [`load_imports`](https://github.com/hasherezade/malware_training_vol1/blob/main/exercises/module1/lesson6_pe_loaders/pe_inject_load_tpl/injector/main.cpp#L96), and instead of this, call the function [`SelfLoadImports`](https://github.com/hasherezade/malware_training_vol1/blob/main/exercises/module1/lesson6_pe_loaders/pe_inject_load_tpl/payload/dll_main.cpp#L23) from the DLL (you will fetch it via exports lookup)
3. Then, call fetch and call the function [`SayHello`](https://github.com/hasherezade/malware_training_vol1/blob/main/exercises/module1/lesson6_pe_loaders/pe_inject_load_tpl/payload/dll_main.cpp#L29). If the `MessageBox` pops up, it means the task is solved.

### Task 2) Remote-injection with loading imports payload-side
*In case of remote injection of manually loaded PEs, imports have to be loaded payload-side - which we prepared by doing the previous task.*
Hints to follow:
In addition to the hints from the Task 1, there are some additional steps:
1. Create a new process to which you will be injecting.
2. This time we need to also allocate memory in the remote process, using `VirtualAllocEx`.
3. First, we load the image locally: yet, relocating it to the remote base. Then, we copy it to the remote memory.
4. We will call the exported functions via remote threads
+ fetch the export from a local image, but remeber that in the remote image it is at the different base, so you need to rebase it manually.
