Skip to content

Commit

Permalink
Create IMAGE_FILE_UP_SYSTEM_ONLY.c
Browse files Browse the repository at this point in the history
  • Loading branch information
gtworek authored Oct 1, 2024
1 parent e56535d commit 31d8055
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions Misc2/IMAGE_FILE_UP_SYSTEM_ONLY.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <Windows.h>
#include <tchar.h>
#include <imagehlp.h>
#include <stdio.h>

#pragma comment(lib, "Imagehlp.lib")

int _tmain(int argc, _TCHAR** argv, _TCHAR** envp)
{
UNREFERENCED_PARAMETER(envp);

DWORD dwFileAttributes;
dwFileAttributes = GetFileAttributes(argv[1]);
if (dwFileAttributes == INVALID_FILE_ATTRIBUTES)
{
_tprintf(_T("Error: Cannot find file. GetFileAttributes() returned %i\r\n"), GetLastError());
return (int)GetLastError();
}

CHAR pszFileName[MAX_PATH];
TCHAR ptszBackupFileName[MAX_PATH];
if (argc != 2)
{
_tprintf(_T("Usage: %s <file name>\r\n"), argv[0]);
return 1;
}

int iRes;
iRes = sprintf_s(pszFileName, _countof(pszFileName), "%ls", argv[1]);
if (iRes < 0)
{
_tprintf(_T("Error: sprintf_s() failed!\r\n"));
return 1;
}


iRes = _stprintf_s(ptszBackupFileName, _countof(ptszBackupFileName), _T("%s.bak"), argv[1]);
if (iRes < 0)
{
_tprintf(_T("Error: _stprintf_s() failed!\r\n"));
return 1;
}

_tprintf(_T("File name: %hs\r\n"), pszFileName);

BOOL bRes;
LOADED_IMAGE LoadedImage;
bRes = MapAndLoad(pszFileName, NULL, &LoadedImage, FALSE, FALSE);
if (bRes == FALSE)
{
_tprintf(_T("Error: Cannot load image. MapAndLoad() returned %i\r\n"), GetLastError());
return (int)GetLastError();
}

if (LoadedImage.FileHeader->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC)
{
UnMapAndLoad(&LoadedImage);
_tprintf(_T("The application works only on 64bit EXE files.\r\n"));
return ERROR_UNSUPPORTED_TYPE;
}

bRes = CopyFile(argv[1], ptszBackupFileName, TRUE);
if (bRes == FALSE)
{
UnMapAndLoad(&LoadedImage);
_tprintf(_T("Error: Cannot create backup file. CopyFile() returned %i\r\n"), GetLastError());
return (int)GetLastError();
}
_tprintf(_T("Backup file created: %s\r\n"), ptszBackupFileName);

//real stuff
LoadedImage.FileHeader->FileHeader.Characteristics |= IMAGE_FILE_UP_SYSTEM_ONLY;

UnMapAndLoad(&LoadedImage);
_tprintf(_T("Done.\r\n"));
}

0 comments on commit 31d8055

Please sign in to comment.