PDA

View Full Version : inject code in execut file



حمیدرضاصادقیان
جمعه 19 مهر 1387, 18:53 عصر
سلام دوستان.من نیاز دارم به انتهای یک فایل اجرایی یک section جدید اضافه کنم و کد مربوطه رو به اون اضافه کنم.میخوام فایل اجراییم رو encrypt کنم و کد مربوط به decrypt رو در انتهای فایل اضافه کنم. میخواستم ببینم چطور میشه یک بخش جدید به فایل اضافه کرد؟
یا میشه در بخش های دیگه فایل اجرایی این کارو انجام داد.
در زبان سی با دستور pragma میتونیم یک بخش جدید در فایل اجرایی ایجاد کنیم ولی در دلفی نمیدونم چطوریه؟

lord_viper
شنبه 20 مهر 1387, 14:29 عصر
این یه کد برای اضافه کردن یک سکشن هست


program AKAV;

{$APPTYPE CONSOLE}

uses
Windows;

function AddSection(FileName: String; SectionName: String): Boolean;
const
SectionCode: Array[0..6] of Byte = (
$B8, $00, $00, $00, $00, // MOV EAX, $00000000
$FF, $E0); // JMP EAX
var
i: ShortInt;
hFile: THandle;
DosHeader: TImageDosHeader;
NtHeaders: TImageNtHeaders;
SectionHeader, NewSection: TImageSectionHeader;
dwOldEntryPoint, dwReadBytes, dwWrittenBytes: DWORD;
begin
Result := False;
// Read DOS Header
hFile := CreateFile(PChar(FileName), GENERIC_ALL, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
if hFile = 0 then
begin
CloseHandle(hFile);
Exit;
end;
SetFilePointer(hFile, 0, nil, FILE_BEGIN);
ReadFile(hFile, DosHeader, sizeof(DosHeader), dwReadBytes, nil);
if dwReadBytes = sizeof(DosHeader) then
begin
// Read Nt Header
SetFilePointer(hFile, DosHeader._lfanew, nil, FILE_BEGIN);
ReadFile(hFile, NtHeaders, sizeof(NtHeaders), dwReadBytes, nil);
if dwReadBytes = sizeof(NtHeaders) then
begin
// Read Section Header
SetFilePointer(hFile, sizeof(SectionHeader) * (NtHeaders.FileHeader.NumberOfSections -1), nil, FILE_CURRENT);
ReadFile(hFile, SectionHeader, sizeof(SectionHeader), dwReadBytes, nil);
if dwReadBytes = sizeof(SectionHeader) then
begin
// New Section
if SectionName = '' then
SectionName := '.EDI';
// Section Name
for i := 0 to 7 do
NewSection.Name[i] := Byte(SectionName[i +1]);
// The options of the Section
NewSection.VirtualAddress := NtHeaders.OptionalHeader.SizeOfImage;
NewSection.Misc.VirtualSize := $200;
NewSection.SizeOfRawData := (NewSection.VirtualAddress div NtHeaders.OptionalHeader.FileAlignment +1) *
NtHeaders.OptionalHeader.FileAlignment - NtHeaders.OptionalHeader.SizeOfImage;
NewSection.PointerToRawData := SectionHeader.SizeOfRawData + SectionHeader.PointerToRawData;
NewSection.Characteristics := $E0000020;
Inc(NtHeaders.FileHeader.NumberOfSections);

// Write new Section
WriteFile(hFile, NewSection, sizeof(NewSection), dwWrittenBytes, nil);
if dwWrittenBytes = sizeof(NewSection) then
begin
// New Entrypoint
dwOldEntryPoint := NtHeaders.OptionalHeader.AddressOfEntryPoint + NtHeaders.OptionalHeader.ImageBase;
NtHeaders.OptionalHeader.AddressOfEntryPoint := NewSection.VirtualAddress;
// change the SectionCode "EntryPoint"
PDWORD(DWORD(@SectionCode) +1)^ := dwOldEntryPoint;
// write the new sizeofimage
NtHeaders.OptionalHeader.SizeOfImage := NtHeaders.OptionalHeader.SizeOfImage + NewSection.Misc.VirtualSize;
SetFilePointer(hFile, DosHeader._lfanew, nil, FILE_BEGIN);
WriteFile(hFile, NtHeaders, sizeof(NtHeaders), dwWrittenBytes, nil);
if dwWrittenBytes = sizeof(NtHeaders) then
begin
// write the new section
SetFilePointer(hFile, GetFileSize(hFile, nil), nil, FILE_BEGIN);
WriteFile(hFile, SectionCode, NewSection.Misc.VirtualSize, dwWrittenBytes, nil);
if dwWrittenBytes = NewSection.Misc.VirtualSize then
begin
CloseHandle(hFile);
Result := True;
end else
CloseHandle(hFile);
end else
CloseHandle(hFile);
end else
CloseHandle(hFile);
end else
CloseHandle(hFile);
end else
CloseHandle(hFile);
end else
CloseHandle(hFile);
end;

begin
WriteLn;
WriteLn('A[nti]KAV by ErazerZ');
WriteLn('5th January 2006');
WriteLn('Web: http://www.gateofgod.com');
WriteLn('E-Mail: ErazerZ@gmail.com');
WriteLn;
if (ParamStr(1) <> '') then
begin
if AddSection(ParamStr(1), ParamStr(2)) then
WriteLn('File is patched!')
else
WriteLn('Error while patching!');
end else
begin
WriteLn('Usage:' +#9#9+ 'AKAV.exe <your Application''s path> <your Section Name>');
WriteLn('Example:' +#9+ 'AKAV.exe "c:\server.exe" ".ErazerZ"');
end;
end.

برای درک مطلب بهتر کتاب ساختار فایلهای pe مربوط به تیم AR رو مطالعه کنین

حمیدرضاصادقیان
یک شنبه 21 مهر 1387, 08:07 صبح
سلام.ممنون از کمکت.این کتاب رو شما داری برام ایمیل کنی؟
hmdsadeghian@gmail.com