简体   繁体   English

在 Excel 中挂钩 ReadFile function

[英]Hook ReadFile function in Excel

I use XOR each byte with 'A' to generate encrypted file.我使用 XOR 每个字节与“A”来生成加密文件。 And do that again to decrypt file -> Excel open decrypted file normally.然后再次执行解密文件-> Excel 正常打开解密文件。

Now I want to open an encrypted file by Excel.现在我想通过 Excel 打开一个加密文件。

I hook the ReadFile API to decrypt the buffer before returning it.我挂钩 ReadFile API 以在返回缓冲区之前对其进行解密。

But Excel displays但是 Excel 显示

Excel cannot be open the file 'filename.xlsx' because the file format or file extension is not valid Excel 无法打开文件“filename.xlsx”,因为文件格式或文件扩展名无效

after reading the first 8 bytes.读取前 8 个字节后。

The decrypted 8 bytes are 50 4B 03 04 14 00 06 00 , which is the correct Open Office XML Signature and same with the original file.解密后的 8 个字节为50 4B 03 04 14 00 06 00 ,这是正确的 Open Office XML 签名,与原始文件相同。

Here is the myReadFile function:这是myReadFile function:

BOOL WINAPI MyReadFile(
_In_ HANDLE hFile,
_Out_writes_bytes_to_opt_(nNumberOfBytesToRead, *lpNumberOfBytesRead) __out_data_source(FILE) LPVOID lpBuffer,
_In_ DWORD nNumberOfBytesToRead,
_Out_opt_ LPDWORD lpNumberOfBytesRead,
_Inout_opt_ LPOVERLAPPED lpOverlapped){

BOOL result = ReadFile(hFile, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped);
if (!result) {
    return result;
}

if (*lpNumberOfBytesRead == 0) {
    return result;
}
char* Buf = (char*)lpBuffer;
for (long i = 0; i < (*lpNumberOfBytesRead); i++)
{
    Buf[i] = Buf[i] ^ 'A';
}
return result;}

Does need hook other API to open encrypted file perfectly.是否需要 hook 其他 API 才能完美打开加密文件。

Does need hook other API to open encrypted file perfectly.是否需要 hook 其他 API 才能完美打开加密文件。

Might be.可能。 A file migt be read other way after reading by ReadFile you've observed.在您观察到的ReadFile读取后,文件可能会以其他方式读取。

There are ReadFileEx and ReadFileScatter , though the first is rarely called, the second is even more rare.ReadFileExReadFileScatter ,虽然第一个很少被调用,但第二个更罕见。 An application might directly call NtReadFile , though it is not much likely too.应用程序可能会直接调用NtReadFile ,但可能性不大。

What is more likely, is creating file mapping using CreateFileMapping or equivalent, and then using MapViewOfFile or equivalent.更有可能的是,使用CreateFileMapping或等效项创建文件映射,然后使用MapViewOfFile或等效项。 In this case I don't see a clean way of doing it via hooking.在这种情况下,我看不到通过钩子实现它的干净方法。

It is also possilbe that is is read from some other process.也有可能从其他进程中读取。

My suggestion is to getprocmon tool, filter accesses to the target file path, and see all file operations.我的建议是获取procmon工具,过滤对目标文件路径的访问,并查看所有文件操作。 You can see call stacks of each operation and see what you need to hook.您可以查看每个操作的调用堆栈,并查看您需要挂钩的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM