有没有一种方法来确定是否已使用UPX压缩了exe文件?
确定exe文件是否已被压缩的函数非常好,除了我发现代码有问题.如果调用函数IsUPXCompressed,则尝试运行upx,upx无法保存它修改的文件.在函数中没有正确共享权限的东西.我已经测试了几个小时.如果我不调用该方法,那么UPX可以毫无问题地编写文件.你调用它然后尝试运行UPX它不会保存文件.尝试写入文件时,UPX报告IOException权限被拒绝错误.
谢谢
确定exe文件是否已被压缩的函数非常好,UPX报告IOException权限被拒绝错误.
谢谢
解决方法@H_502_16@
另一个方法,当用UPX工具打包exe时,PE头部分包含名为UPX0,UPX1等的部分.因此,如果读取这些部分并将名称与字符串UPX进行比较,则可以确定是否使用UPX包装机.
检查这个功能
uses
Windows;
function IsUPXCompressed(const Filename:TFileName): Boolean;
var
i : integer;
pBaseAddress : PByte;
pDosHeader : PImageDosHeader;
pNtHeaders : PImageNtHeaders;
hFile : Cardinal;
hFileMap : Cardinal;
pSectionHeader: PImageSectionHeader;
dwOffset : Cardinal;
SectName : AnsiString;
begin
Result:=False;
hFile := CreateFile(PChar(Filename),GENERIC_READ,FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if (hFile = INVALID_HANDLE_VALUE) then Exit;
hFileMap := CreateFileMapping(hFile,PAGE_READONLY or SEC_IMAGE,nil);
if (hFileMap = 0) then
begin
CloseHandle(hFile);
Exit;
end;
pBaseAddress := MapViewOfFile(hFileMap,FILE_MAP_READ,0);
if (pBaseAddress = nil) then
begin
CloseHandle(hFileMap);
CloseHandle(hFile);
Exit;
end;
try
dwOffset := Cardinal(pBaseAddress);
pDosHeader := PImageDosHeader(pBaseAddress);
pNtHeaders := PImageNtHeaders(dwOffset + Cardinal(pDosHeader._lfanew));
pSectionHeader := pImageSectionHeader(Cardinal(pNtHeaders) + SizeOf(TImageNtHeaders));
for i := 0 to pNtHeaders.FileHeader.NumberOfSections-1 do
begin
SetString(SectName,PAnsiChar(@pSectionHeader.Name),SizeOf(pSectionHeader.Name));
Result:=Pos('UPX',SectName)>0;
If Result then break;
Inc(pSectionHeader);
end;
finally
UnmapViewOfFile(pBaseAddress);
CloseHandle(hFileMap);
CloseHandle(hFile);
end;
end;
检查这个功能
uses Windows; function IsUPXCompressed(const Filename:TFileName): Boolean; var i : integer; pBaseAddress : PByte; pDosHeader : PImageDosHeader; pNtHeaders : PImageNtHeaders; hFile : Cardinal; hFileMap : Cardinal; pSectionHeader: PImageSectionHeader; dwOffset : Cardinal; SectName : AnsiString; begin Result:=False; hFile := CreateFile(PChar(Filename),GENERIC_READ,FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); if (hFile = INVALID_HANDLE_VALUE) then Exit; hFileMap := CreateFileMapping(hFile,PAGE_READONLY or SEC_IMAGE,nil); if (hFileMap = 0) then begin CloseHandle(hFile); Exit; end; pBaseAddress := MapViewOfFile(hFileMap,FILE_MAP_READ,0); if (pBaseAddress = nil) then begin CloseHandle(hFileMap); CloseHandle(hFile); Exit; end; try dwOffset := Cardinal(pBaseAddress); pDosHeader := PImageDosHeader(pBaseAddress); pNtHeaders := PImageNtHeaders(dwOffset + Cardinal(pDosHeader._lfanew)); pSectionHeader := pImageSectionHeader(Cardinal(pNtHeaders) + SizeOf(TImageNtHeaders)); for i := 0 to pNtHeaders.FileHeader.NumberOfSections-1 do begin SetString(SectName,PAnsiChar(@pSectionHeader.Name),SizeOf(pSectionHeader.Name)); Result:=Pos('UPX',SectName)>0; If Result then break; Inc(pSectionHeader); end; finally UnmapViewOfFile(pBaseAddress); CloseHandle(hFileMap); CloseHandle(hFile); end; end;