以下のコードから生成した実行ファイルの実行結果がWindowsとWineで異なり、行き詰まってしまったのでお知恵を借りたく質問します。なぜWine環境でだけWriteProcessMemoryが失敗するのでしょうか?何かしら回避策につながるヒントはありませんでしょうか?
問題のコード
Windows環境(10 バージョン1803)ではWriteProcessMemoryの返り値が非ゼロで成功しますが、Wine環境(wine-3.0.4)ではゼロが返りGetLastError()でAccess denied.
が表示されます。
C
1#include <windows.h> 2#include <stdio.h> 3 4/* 5 * gcc poc.c -o poc.exe && poc.exe 6 */ 7 8 9void print_errmsg(DWORD error) { 10 char errmsg[512]; 11 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 12 NULL, 13 GetLastError(), 14 0, 15 errmsg, 16 sizeof(errmsg), 17 NULL); 18 printf("%hs", errmsg); 19} 20 21int main(int argc, char** argv) { 22 char* data = "123"; 23 DWORD access = PROCESS_VM_OPERATION|PROCESS_VM_WRITE; 24 25 HANDLE hProcess = OpenProcess(access, FALSE, GetCurrentProcessId()); 26 void* address = VirtualAllocEx(hProcess, 27 NULL, 28 sizeof(data), 29 MEM_COMMIT, 30 PAGE_READWRITE); 31 if (!WriteProcessMemory(hProcess, 32 address, 33 (void*)data, 34 sizeof(data), 35 NULL)) { 36 print_errmsg(GetLastError()); 37 return 1; 38 } else { 39 printf("OK!\n"); 40 } 41 42 return 0; 43}
コンパイラ情報
> gcc --version gcc (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 8.1.0 Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Wine情報
$ printenv | grep WINE WINEDLLOVERRIDES=mscoree,mshtml= WINEDEBUG=fixme-all WINEARCH=win32 WINEPREFIX=/home/wineuser/.wine
試したこと #1
WineのWriteProcessMemoryはこのブログの記述に反して、そのままNtWriteVirtualMemoryの呼び出しをしてそうなので、念のために以下の様なVirtualProtectを差し込んでみましたが結果は変わりませんでした。
C
1// 当方Cプログラミングは見よう見まねなのでaddressのアンパサンドあり・なしのどちらが正しいのかすら判別できないでいます。(多分前者が正解?) 2VirtualProtect(address, sizeof(data), PAGE_EXECUTE_READWRITE, &access); 3VirtualProtect(&address, sizeof(data), PAGE_EXECUTE_READWRITE, &access);
試したこと #2
WINEDLLOVERRIDES
やWINEDEBUG
を空にしてWINEPREFIX
に新しい場所を指定しても結果は変わりませんでした。
$ WINEPREFIX=/tmp/wine WINEDLLOVERRIDES= WINEDEBUG= wine poc.exe
試したこと #3
このスレッドで言及されているinjectdll.exeを試しても、Wine環境ではWriteProcessMemory
のところでcan't write to memory in that pid
となる。
試したこと #4
hProcess
が毎回同じ値(WriteProcessMemory(00000020,...
の00000020
)になるのが気になる。。。
wineuser@d2efe6cf1283:~$ WINEDEBUG=+all wine /opt/poc.exe 2>&1 | grep WriteProcess 30197.178:008a:008b:trace:imports:import_dll --- WriteProcessMemory KERNEL32.dll.1310 = 0x7b42854c 30197.332:008a:008b:Call KERNEL32.WriteProcessMemory(00000020,00230000,00404048,00000004,00000000) ret=004016de 30197.332:008a:008b:Ret KERNEL32.WriteProcessMemory() retval=00000000 ret=004016de wineuser@d2efe6cf1283:~$ WINEDEBUG=+all wine /opt/poc.exe 2>&1 | grep WriteProcess 30199.196:008c:008d:trace:imports:import_dll --- WriteProcessMemory KERNEL32.dll.1310 = 0x7b42854c 30199.359:008c:008d:Call KERNEL32.WriteProcessMemory(00000020,00230000,00404048,00000004,00000000) ret=004016de 30199.359:008c:008d:Ret KERNEL32.WriteProcessMemory() retval=00000000 ret=004016de wineuser@d2efe6cf1283:~$ WINEDEBUG=+all wine /opt/poc.exe 2>&1 | grep WriteProcess 30200.351:008e:008f:trace:imports:import_dll --- WriteProcessMemory KERNEL32.dll.1310 = 0x7b42854c 30200.521:008e:008f:Call KERNEL32.WriteProcessMemory(00000020,00230000,00404048,00000004,00000000) ret=004016de 30200.522:008e:008f:Ret KERNEL32.WriteProcessMemory() retval=00000000 ret=004016de wineuser@d2efe6cf1283:~$ WINEDEBUG=+all wine /opt/poc.exe 2>&1 | grep WriteProcess 30201.972:0090:0091:trace:imports:import_dll --- WriteProcessMemory KERNEL32.dll.1310 = 0x7b42854c 30202.132:0090:0091:Call KERNEL32.WriteProcessMemory(00000020,00230000,00404048,00000004,00000000) ret=004016de 30202.132:0090:0091:Ret KERNEL32.WriteProcessMemory() retval=00000000 ret=004016de
試したこと #5
VS2017/Ubuntu 16.04/wine-1.6.2で試してみましたが結果はAccess denied.
となりました。
> cl /EHsc poc.c Microsoft(R) C/C++ Optimizing Compiler Version 19.13.26128 for x86 Copyright (C) Microsoft Corporation. All rights reserved. poc.c Microsoft (R) Incremental Linker Version 14.13.26128.0 Copyright (C) Microsoft Corporation. All rights reserved. /out:poc.exe poc.obj
試したこと #6
VS2017 (スタティックリンク)/Ubuntu 16.04/wine-1.6.2
> cl /MT poc.c Microsoft(R) C/C++ Optimizing Compiler Version 19.13.26128 for x86 Copyright (C) Microsoft Corporation. All rights reserved. poc.c Microsoft (R) Incremental Linker Version 14.13.26128.0 Copyright (C) Microsoft Corporation. All rights reserved. /out:poc.exe poc.obj root@6c9ee3b182f1:/# ls -l /tmp/poc.exe -rwxr-xr-x 1 root root 97792 Dec 26 10:35 /tmp/poc.exe root@6c9ee3b182f1:/# wine /tmp/poc.exe Access denied.
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/27 02:42