Below is an example of a complete Delphi DLL project that incorporates your code and automatically creates a thread when injected. In this example the DLL defines two procedures—one that sends a packet “raw” (using inline assembly to call the encrypt and send functions at fixed addresses) and one that writes an array of bytes to the fixed packet‐buffer and then sends it. When the DLL is loaded (i.e. when it is injected into the target process) the DLL’s entry point spawns a new thread (running the demo thread procedure that sends your packet once). You can adjust the thread’s behavior as needed.
Below is the full source (save it as, for example, “PacketDLL.dpr”) that you can compile with Delphi:
library PacketDLL;
uses
Windows, SysUtils;
{--------------------------------------------------------------------------
Constants – these addresses and packet bytes come from your original code.
--------------------------------------------------------------------------}
const
PACKETADDRESS = $04DDBCF0;
ADR_PacketOut_Temp = $1C978AB8;
Fn_PacketOut_Encrypt = $006687A6;
Fn_PacketOut_Send = $00668C3A;
// The packet bytes to send.
MyPacket: array[0..13] of Byte = (
$E2, $B7, $0E, $00, $00, $00, $00, $00,
$E5, $01, $B4, $10, $00, $00
);
{--------------------------------------------------------------------------
WriteIt – a helper routine to copy the packet data into the target memory.
(In your code this was called with WriteIt(ptr(PACKETADDRESS), packet);)
--------------------------------------------------------------------------}
procedure WriteIt(Dest: Pointer; Src: PByte; Size: DWORD);
begin
// Direct memory copy – note that Dest is assumed to be in the target’s process
// (and writable) because the DLL is injected.
Move(Src^, Dest^, Size);
end;
{--------------------------------------------------------------------------
FixedSleep – helper to pause execution.
--------------------------------------------------------------------------}
procedure FixedSleep(Delay: DWORD);
begin
Sleep(Delay);
end;
{--------------------------------------------------------------------------
Send_Packet_Raw – sends a packet of the given size.
It uses inline assembly to call the encrypt and send functions at fixed addresses.
--------------------------------------------------------------------------}
procedure Send_Packet_Raw(packetsize: DWORD); stdcall;
const
// Using the constants so that the inline assembly uses the appropriate addresses.
EncFunc: DWORD = Fn_PacketOut_Encrypt;
SendFunc: DWORD = Fn_PacketOut_Send;
begin
asm
pushad // Save all registers
mov ecx, packetsize // ECX holds the packet size
mov eax, PACKETADDRESS // The packet data should be at PACKETADDRESS
push ecx // Push packetsize (parameter to EncFunc)
push eax // Push address of the packet data
mov ecx, dword ptr [ADR_PacketOut_Temp]
call EncFunc // call the encryption routine
mov ecx, dword ptr [ADR_PacketOut_Temp]
call SendFunc // call the sending routine
popad // restore registers
end;
end;
{--------------------------------------------------------------------------
Send_Packet_Array – writes the provided packet data into the target
memory and sends it. An optional delay parameter can be specified.
(This version accepts a pointer and size; in your original code you used an array.)
--------------------------------------------------------------------------}
procedure Send_Packet_Array(packet: PByte; packetSize: DWORD; Delay: DWORD = 0); stdcall;
begin
// Write the packet into the (fixed) packet buffer.
WriteIt(Pointer(PACKETADDRESS), packet, packetSize);
// Now send the packet.
Send_Packet_Raw(packetSize);
// Optional delay.
if Delay > 0 then
FixedSleep(Delay);
end;
{--------------------------------------------------------------------------
ThreadProc – the thread’s main function. In this demo it simply sends
the pre-defined MyPacket once.
You can modify this procedure to change the thread’s behavior.
--------------------------------------------------------------------------}
function ThreadProc(lpParameter: Pointer): DWORD; stdcall;
begin
// For demonstration, send MyPacket once.
Send_Packet_Array(@MyPacket[0], SizeOf(MyPacket));
Result := 0; // Exit the thread.
end;
{--------------------------------------------------------------------------
DLL Entry Point
When the DLL is loaded into a process (DLL_PROCESS_ATTACH), it creates
a new thread which automatically executes our ThreadProc.
--------------------------------------------------------------------------}
function DllMain(hinstDLL: HMODULE; fdwReason: DWORD; lpvReserved: Pointer): BOOL; stdcall;
var
hThread: THandle;
ThreadID: DWORD;
begin
if fdwReason = DLL_PROCESS_ATTACH then
begin
// Create a thread that runs ThreadProc.
hThread := CreateThread(nil, 0, @ThreadProc, nil, 0, ThreadID);
if hThread <> 0 then
CloseHandle(hThread);
end;
Result := True;
end;
{--------------------------------------------------------------------------
Exports
Optionally you can export the routines if you want to call them from
other programs. (Not required for an auto‑starting injected DLL.)
--------------------------------------------------------------------------}
exports
Send_Packet_Raw,
Send_Packet_Array;
begin
// The library’s initialization code automatically calls DllMain.
end.
Explanation
-
Constants & Packet Data:
The DLL defines the fixed addresses for the packet buffer, temporary encryption buffer, and the two functions (encrypt and send). It also defines the packet bytes (stored in MyPacket).
-
Helper Routines:
WriteIt uses the Move procedure to copy packet bytes into the target memory at PACKETADDRESS.
FixedSleep is a simple wrapper around the Windows Sleep function.
-
Core Packet Functions:
Send_Packet_Raw uses inline assembly to:
- Push all registers.
- Pass the packet size and the address of the packet.
- Call the encryption function (located at
Fn_PacketOut_Encrypt) and then the send function (at Fn_PacketOut_Send).
Send_Packet_Array takes a pointer to a packet and its size, writes it into the packet buffer, then calls Send_Packet_Raw. It also allows an optional delay.
-
ThreadProc:
This is the thread function that gets executed immediately on DLL injection. For demo purposes it calls Send_Packet_Array to send the pre-defined packet.
-
DllMain:
On DLL_PROCESS_ATTACH the DLL creates a new thread running ThreadProc. This means that as soon as the DLL is injected into the target process, it begins its operation automatically.
-
Exports:
The procedures Send_Packet_Raw and Send_Packet_Array are exported in case you’d like to call them externally. (This is optional if you only plan on using the auto‑starting thread.)
Usage
Compile this project in Delphi to produce PacketDLL.dll. Then, when you inject this DLL into the target process, the DLL’s entry point will create a thread that immediately sends your packet.
Feel free to modify the thread procedure, add error handling, or extend functionality as required.