MIT 라이선스로 무료 공개된 Detours API 후킹 라이브러리
예전에 한번 소개해 드렸던,
Detours 라이브러리를 이용한 Win32 API - Sleep 호출 가로채기
; https://www.sysnet.pe.kr/2/0/631
마이크로소프트의 Detours가 소리 소문 없이 MIT 라이선스로 풀려 있는 것을 발견했습니다. ^^
Microsoft/Detours
; https://github.com/Microsoft/Detours
Can Detours be used in commercial applications?
Detours is licensed under the MIT license, which allows commercial use.
그동안, Detours 라이브러리가 무료 버전에서는 x86 만 지원하고 x64를 위해서는 유료인 Pro 버전을 구매해야만 했던 탓에 어쩔 수 없이 다른 후킹 라이브러리를 살펴봐야 했는데요,
API Hooking - 64비트를 고려해야 한다면? EasyHook!
; https://www.sysnet.pe.kr/2/0/1242
이젠 그럴 필요가 없어졌습니다. ^^
github에 공개된 Detours는 4.0.1 버전이지만 실은 기존 3.0 버전의 343 빌드와 동일한 것이라고 합니다. 즉, 라이선스의 개정으로 인해 버전을 올린 것일 뿐 공개로 인한 제약 사항은 없는 것입니다.
자, 그럼 ^^ 무료가 된 Detours를 빌드해 볼까요?
윈도우 "시작" 메뉴에서 "Developer Command Prompt for VS 2017" 명령행을 열고, 다음과 같은 명령어로 github의 소스 코드를 복제합니다.
c:\temp> git clone https://github.com/Microsoft/Detours.git
다운로드한 폴더로 이동해 nmake를 실행하면 빌드까지 모두 완료가 됩니다.
c:\temp> cd Detours
c:\temp\Detours> nmake
생성된 바이너리는 기본적으로 x86을 대상으로 합니다.
c:\temp\Detours> tree
Folder PATH listing for volume New Volume
Volume serial number is CC52-B5DD
C:.
├───bin.X86
├───include
├───lib.X86
├───samples
│ ├───comeasy
│ │ └───obj.X86
│ ├───commem
│ │ └───obj.X86
│ ├───cping
│ │ └───obj.X86
│ ├───disas
│ │ └───obj.X86
...[생략]...
│ ├───tracessl
│ │ └───obj.X86
│ ├───tracetcp
│ │ └───obj.X86
│ ├───tryman
│ │ └───obj.X86
│ └───withdll
│ └───obj.X86
└───src
└───obj.X86
물론 x64 버전도 빌드할 수 있습니다. 다음과 같이 환경 변수를 설정한 후 nmake를 실행하면 됩니다.
c:\temp\Detours> SET DETOURS_TARGET_PROCESSOR=X64
c:\temp\Detours> nmake
현재 x64의 경우에는 detours 라이브러리 자체는 빌드되지만, samples 프로젝트를 빌드 도중 iping 예제에서 다음과 같은 오류가 발생합니다.
iping_d.obj : error LNK2001: unresolved external symbol _iping_ProxyFileInfo
..\..\bin.X64\cping.exe : fatal error LNK1120: 1 unresolved externals
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x86\cl.EXE"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
예제 프로젝트가 필요하지 않은 경우라면 상관없으니 그냥 둬도 무방합니다.
테스트를 해볼까요? ^^
예전 글(
Detours 라이브러리를 이용한 Win32 API - Sleep 호출 가로채기)에서 실습했던 내용을 그대로 해보겠습니다.
우선 x86 먼저 해보면,
c:\temp\Detours\bin.X86> sleep5
sleep5.exe: Starting.
sleep5.exe: Done sleeping.
보는 바와 같이 변경이 안 된 상태에서 setdll을 실행해 detour 시킨 후,
c:\temp\Detours\bin.X86> setdll /d:simple32.dll sleep5.exe
Adding simple32.dll to binary files.
sleep5.exe:
simple32.dll
KERNEL32.dll -> KERNEL32.dll
다시 실행하면 결과가 달라진 것을 볼 수 있습니다.
c:\temp\Detours\bin.X86> sleep5
simple32.dll: Starting.
simple32.dll: Detoured SleepEx().
sleep5.exe: Starting.
sleep5.exe: Done sleeping.
simple32.dll: Removed SleepEx() (result=0), slept 5000 ticks.
당연히 x64도 동일하게 할 수 있습니다.
c:\temp\Detours\bin.X64> sleep5
sleep5.exe: Starting.
sleep5.exe: Done sleeping.
c:\temp\Detours\bin.X64> setdll /d:simple64.dll sleep5.exe
Adding simple64.dll to binary files.
sleep5.exe:
simple64.dll
KERNEL32.dll -> KERNEL32.dll
c:\temp\Detours\bin.X64> sleep5
simple32.dll: Starting.
simple32.dll: Detoured SleepEx().
sleep5.exe: Starting.
sleep5.exe: Done sleeping.
simple32.dll: Removed SleepEx() (result=0), slept 5000 ticks.
$9,999 유료 버전이었던 만큼, 안정성 측면에서는 이만한 라이브러리가 없을 것입니다. ^^
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]