1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
//get offsets through parameters and data to be inserted in place offset
editBinary(long offsetOfIP, long offsetOfPortNumber, const char *IPAddress, const char *portNumber)
{
const DWORD MAX_NUMBER_OF_BYTES_IN_IP = 15;
const DWORD MAX_NUMBER_OF_BYTES_IN_PORT = 5;
DWORD numOfBytesWritten = 0;
HANDLE hFile = CreateFile(L"application.exe", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE){
cout<<"CreateFile() error: "<<GetLastError()<<endl;
cout<<"*Make sure the file app.exe exists in the folder."<<endl;
cout<<"Please press any key to go back"<<endl;
cin.get();
//get back
}
//point to IP offset
DWORD successResult = SetFilePointer(hFile, offsetOfIP, NULL, FILE_CURRENT);
if(successResult == INVALID_SET_FILE_POINTER)
{
cout<<"SetFilePointer() error: "<<GetLastError()<<endl;
cout<<"Please press any key to go back"<<endl;
cin.get();
//get back
}
//overwrite the old IP - IPAddress contains the new IP
BOOL numberOfIPBytesWrittenToFile = WriteFile(hFile, IPAddress, MAX_NUMBER_OF_BYTES_IN_IP, &numOfBytesWritten, NULL);
if(numberOfIPBytesWrittenToFile == 0)
{
cout<<"WriteFile() error: "<<GetLastError()<<endl;
cout<<"Please press any key to go back"<<endl;
cin.get();
//get back
}
//point to where port is located
successResult = SetFilePointer(hFile, offsetOfPortNumber, NULL, FILE_BEGIN);
if(successResult == INVALID_SET_FILE_POINTER)
{
cout<<"SetFilePointer() error: "<<GetLastError()<<endl;
cout<<"Please press any key to go back"<<endl;
cin.get();
//get back
}
//overwrite the old port number - portNumber contains the new port number
numOfBytesWritten = 0;
BOOL numberOfPortBytesWrittenToFile = WriteFile(hFile, portNumber, MAX_NUMBER_OF_BYTES_IN_PORT, &numOfBytesWritten, NULL);
if(numberOfPortBytesWrittenToFile == 0)
{
cout<<"WriteFile() error: "<<GetLastError()<<endl;
cout<<"Please press any key to go back"<<endl;
cin.get();
//get back
}
//close file
BOOL isFileClosed = CloseHandle(hFile);
if(isFileClosed == 0)
{
cout<<"CloseHandle() error: "<<GetLastError()<<endl;
cout<<"Please press any key to go back"<<endl;
cin.get();
//get back
}
cout<<"Editing Succeeded."<<endl;
}
|