ورود

View Full Version : تزریق قفل به یه فایل اجرایی



saied7468
چهارشنبه 25 دی 1387, 01:10 صبح
سلام
من یه قفل تلفنی با دلفی نوشتم حالا من چطور می تونم کد های قفل خودم رو به یه فایل اجرایی دیگه الحاق کنم . ( کاری که قفل نرم افزاری شتاب انجام می ده کد های محافظتی خودش رو به فایل انتخابی الحاق می کنه)

saied7468
چهارشنبه 25 دی 1387, 12:45 عصر
بعد از یه کم جستجو دیگه فهمیدم که باید با کد زیر یک سکشن به فایل مورد نظرم اضافه کنم و کد های قفلم رو به اون سکشن تزریق کنم



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.


حالا یه سوال دیگه مطرح میشه که چطوری من کدم رو به این سکشن تزریق کنم ؟

saied7468
چهارشنبه 25 دی 1387, 15:39 عصر
این برنامه کارش دستکاری فایل هست یا به قول سازندش برای گول زدن PeID وRDG.
تا اونجایی که دیدم وقتی فایل رو می گیره با کد هایی که در یونیت untPeFile فایل رو آنالیز می کنه ( آدرس EntryPoint رو می گیره و ایمیج بیس هم همین طور و ... )بعد یه سکشن درست می کنه و اگه اشتباه نگم کد مربوطه رو که در فایل Sig.dat هست رو به سکشن تزریق می کنه .

اگه کسی بلده راهنمایی کنه تا این سورس رو برای قفل خودم تغییر بدم
فکر کنم اگه سورس تغییر کنه به درد خیلی ها بخوره .

saied7468
پنج شنبه 26 دی 1387, 00:13 صبح
واقعا ممنون از این همه توجه!
این تاپیک تو این یه روزی که ایجاد شده تا این لحظه 84 بار بازدید داشته یعنی از این 84 نفری که ازش بازدید کردن کسی چیزی نمی دونسته؟!!!!!!!!!!!!!:متفکر:

باتوجه به تعداد بازدید فکر کنم موضوع برای همه جالب بوده پس از اساتید هم درخواست می شه ما را یاری کنند.

Nima NT
پنج شنبه 26 دی 1387, 10:41 صبح
یه سری به سایت CodeProject بزن اونجا چند تا مقاله و سورس ( البته به زبان ++C ) برای تزریق کد وجود داره , البته توضیحش از کدهاش جالب تره.
امیدوارم که به دردت بخوره.