Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)
(시리즈 글이 16개 있습니다.)
VC++: 36. Detours 라이브러리를 이용한 Win32 API - Sleep 호출 가로채기
; https://www.sysnet.pe.kr/2/0/631

.NET Framework: 187. 실행 시에 메서드 가로채기 - CLR Injection: Runtime Method Replacer 개선
; https://www.sysnet.pe.kr/2/0/942

디버깅 기술: 40. 상황별 GetFunctionPointer 반환값 정리 - x86
; https://www.sysnet.pe.kr/2/0/1027

VC++: 56. Win32 API 후킹 - Trampoline API Hooking
; https://www.sysnet.pe.kr/2/0/1231

VC++: 57. 웹 브라우저에서 Flash만 빼고 다른 ActiveX를 차단할 수 있을까?
; https://www.sysnet.pe.kr/2/0/1232

VC++: 58. API Hooking - 64비트를 고려해야 한다면? EasyHook!
; https://www.sysnet.pe.kr/2/0/1242

개발 환경 구성: 419. MIT 라이선스로 무료 공개된 Detours API 후킹 라이브러리
; https://www.sysnet.pe.kr/2/0/11764

.NET Framework: 883. C#으로 구현하는 Win32 API 후킹(예: Sleep 호출 가로채기)
; https://www.sysnet.pe.kr/2/0/12132

.NET Framework: 890. 상황별 GetFunctionPointer 반환값 정리 - x64
; https://www.sysnet.pe.kr/2/0/12143

.NET Framework: 891. 실행 시에 메서드 가로채기 - CLR Injection: Runtime Method Replacer 개선 - 두 번째 이야기
; https://www.sysnet.pe.kr/2/0/12144

디버깅 기술: 163. x64 환경에서 구현하는 다양한 Trampoline 기법
; https://www.sysnet.pe.kr/2/0/12148

.NET Framework: 895. C# - Win32 API를 Trampoline 기법을 이용해 C# 메서드로 가로채는 방법
; https://www.sysnet.pe.kr/2/0/12150

.NET Framework: 896. C# - Win32 API를 Trampoline 기법을 이용해 C# 메서드로 가로채는 방법 - 두 번째 이야기 (원본 함수 호출)
; https://www.sysnet.pe.kr/2/0/12151

.NET Framework: 897. 실행 시에 메서드 가로채기 - CLR Injection: Runtime Method Replacer 개선 - 세 번째 이야기(Trampoline 후킹)
; https://www.sysnet.pe.kr/2/0/12152

.NET Framework: 898. Trampoline을 이용한 후킹의 한계
; https://www.sysnet.pe.kr/2/0/12153

.NET Framework: 968. C# 9.0의 Function pointer를 이용한 함수 주소 구하는 방법
; https://www.sysnet.pe.kr/2/0/12409




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 유료 버전이었던 만큼, 안정성 측면에서는 이만한 라이브러리가 없을 것입니다. ^^




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 10/26/2018]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2020-06-10 09시27분
[cnsghk12] 디버그 모드로 빌드 가능한가요? 위 결과는 릴리즈 버전이겠죠?
[guest]
2020-06-10 10시10분
@cnsghk12 당연히 가능하지 않을까요? 어차피 IAT 후킹이기 때문에 디버그 빌드라고 달라질 것이 없습니다.
정성태

1  2  3  4  [5]  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13509정성태1/3/20242123오류 유형: 887. .NET Core 2 이하의 프로젝트에서 System.Runtime.CompilerServices.Unsafe doesn't support netcoreapp2.0.
13508정성태1/3/20242142오류 유형: 886. ORA-28000: the account is locked
13507정성태1/2/20242820닷넷: 2191. C# - IPGlobalProperties를 이용해 netstat처럼 사용 중인 Socket 목록 구하는 방법파일 다운로드1
13506정성태12/29/20232391닷넷: 2190. C# - 닷넷 코어/5+에서 달라지는 System.Text.Encoding 지원
13505정성태12/27/20232940닷넷: 2189. C# - WebSocket 클라이언트를 닷넷으로 구현하는 예제 (System.Net.WebSockets)파일 다운로드1
13504정성태12/27/20232519닷넷: 2188. C# - ASP.NET Core SignalR로 구현하는 채팅 서비스 예제파일 다운로드1
13503정성태12/27/20232382Linux: 67. WSL 환경 + mlocate(locate) 도구의 /mnt 디렉터리 검색 문제
13502정성태12/26/20232482닷넷: 2187. C# - 다른 프로세스의 환경변수 읽는 예제파일 다운로드1
13501정성태12/25/20232295개발 환경 구성: 700. WSL + uwsgi - IPv6로 바인딩하는 방법
13500정성태12/24/20232360디버깅 기술: 194. Windbg - x64 가상 주소를 물리 주소로 변환
13498정성태12/23/20233046닷넷: 2186. 한국투자증권 KIS Developers OpenAPI의 C# 래퍼 버전 - eFriendOpenAPI NuGet 패키지
13497정성태12/22/20232448오류 유형: 885. Visual Studiio - error : Could not connect to the remote system. Please verify your connection settings, and that your machine is on the network and reachable.
13496정성태12/21/20232431Linux: 66. 리눅스 - 실행 중인 프로세스 내부의 환경변수 설정을 구하는 방법 (gdb)
13495정성태12/20/20232400Linux: 65. clang++로 공유 라이브러리의 -static 옵션 빌드가 가능할까요?
13494정성태12/20/20232532Linux: 64. Linux 응용 프로그램의 (C++) so 의존성 줄이기(ReleaseMinDependency) - 두 번째 이야기
13493정성태12/19/20232626닷넷: 2185. C# - object를 QueryString으로 직렬화하는 방법
13492정성태12/19/20232311개발 환경 구성: 699. WSL에 nopCommerce 예제 구성
13491정성태12/19/20232258Linux: 63. 리눅스 - 다중 그룹 또는 사용자를 리소스에 권한 부여
13490정성태12/19/20232384개발 환경 구성: 698. Golang - GLIBC 의존을 없애는 정적 빌드 방법
13489정성태12/19/20232166개발 환경 구성: 697. GoLand에서 ldflags 지정 방법
13488정성태12/18/20232095오류 유형: 884. HTTP 500.0 - 명령행에서 실행한 ASP.NET Core 응용 프로그램을 실행하는 방법
13487정성태12/16/20232401개발 환경 구성: 696. C# - 리눅스용 AOT 빌드를 docker에서 수행 [1]
13486정성태12/15/20232214개발 환경 구성: 695. Nuget config 파일에 값 설정/삭제 방법
13485정성태12/15/20232095오류 유형: 883. dotnet build/restore - error : Root element is missing
13484정성태12/14/20232171개발 환경 구성: 694. Windows 디렉터리 경로를 WSL의 /mnt 포맷으로 구하는 방법
13483정성태12/14/20232314닷넷: 2184. C# - 하나의 resource 파일을 여러 프로그램에서 (AOT 시에도) 사용하는 방법파일 다운로드1
1  2  3  4  [5]  6  7  8  9  10  11  12  13  14  15  ...