Microsoft MVP성태의 닷넷 이야기
.NET Framework: 216. 라이선스까지도 뛰어넘는 .NET Profiler [링크 복사], [링크+제목 복사]
조회: 22490
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 3개 있습니다.)
(시리즈 글이 9개 있습니다.)
.NET Framework: 216. 라이선스까지도 뛰어넘는 .NET Profiler
; https://www.sysnet.pe.kr/2/0/1046

.NET Framework: 336. .NET Profiler가 COM 개체일까?
; https://www.sysnet.pe.kr/2/0/1352

.NET Framework: 576. 기본적인 CLR Profiler 소스 코드 설명
; https://www.sysnet.pe.kr/2/0/10950

.NET Framework: 582. CLR Profiler - 별도 정의한 .NET 코드를 호출하도록 IL 코드 변경
; https://www.sysnet.pe.kr/2/0/10959

.NET Framework: 808. .NET Profiler - GAC 모듈에서 GAC 비-등록 모듈을 참조하는 경우의 문제
; https://www.sysnet.pe.kr/2/0/11810

오류 유형: 672. AllowPartiallyTrustedCallers 특성이 적용된 어셈블리의 struct 멤버 메서드를 재정의하면 System.Security.VerificationException 예외 발생
; https://www.sysnet.pe.kr/2/0/12384

.NET Framework: 987. .NET Profiler - FunctionID와 연관된 ClassID를 구할 수 없는 문제
; https://www.sysnet.pe.kr/2/0/12465

.NET Framework: 1041. C# - AssemblyID, ModuleID를 관리 코드에서 구하는 방법
; https://www.sysnet.pe.kr/2/0/12605

닷넷: 2228. .NET Profiler - IMetaDataEmit2::DefineMethodSpec 사용법
; https://www.sysnet.pe.kr/2/0/13576




라이선스까지도 뛰어넘는 .NET Profiler

.NET Profiler에 대해 공부해 본 분이라면, 아래의 글에 대해서 읽어보셨을 텐데요.

No Code Can Hide from the Profiling API in the .NET Framework 2.0
; https://learn.microsoft.com/en-us/archive/msdn-magazine/2005/january/no-code-can-hide-from-the-profiling-api-in-the-net-framework-2-0

혹시나, 위의 글을 못 보셨다고 해도... "No Code"의 범위가 어디까지인지를 실감할 수 있도록 이번 글에서 한번 다뤄볼까 하는데요. 이를 위해 제가 선택한 사례는 바로 (어찌 보면 가장 민감할 수 있는) '라이선스'입니다.

지난번에, .NET에서 기본 제공되는 라이선스 체계에 대한 설명을 했었지요.

닷넷 System.ComponentModel.LicenseManager를 이용한 라이선스 적용
; https://www.sysnet.pe.kr/2/0/1045

사실, 위의 글은 원래 이번 글을 위해 미리 라이선스 지식을 전달하기 위해 씌여진 것입니다. 이제 본격적으로, 위의 지식을 바탕으로 라이선스를 우회할 수 있는 .NET Profiler를 제작해 보겠습니다.




복습 겸 해서, 닷넷의 System.ComponentModel.LicenseManager 기능에 대해 정리를 해볼까요? 단적인 예로, System.ComponentModel.LicenseManager를 사용하는 대부분의 '상용 컴포넌트'들은 그들이 만든 클래스의 생성자에 보통 다음과 같은 구문을 넣어둡니다.

private MyLicensedComponent()
{
    LicenseManager.Validate(typeof(MyLicensedComponent), this);

    // 이후 구성 요소 초기화 작업 코드 실행
}

LicenseManager.Validate 메서드는 전달된 Type에 지정된 LicenseProviderAttribute 특성을 읽어들여 LicenseProvider를 상속받은 타입을 생성하고 System.ComponentModel.LicenseProvider.GetLicense 메서드를 불러서 License 개체를 반환받게 되어 있습니다. 만약, 이 과정에서 정상적으로 License 개체를 반환받지 못하면 보통 System.ComponentModel.LicenseException 예외가 발생하게 되고, Validate 메서드를 호출한 생성자에서는 예외 발생으로 인해 이후의 초기화 코드가 실행될 수 없어 개체 자체가 정상적인 역할을 하지 못하게 되는 것입니다.

라이선스 우회를 하기 위해 .NET Profiler에서 해야 할 일은 간단합니다. 바로 LicenseManager.Validate의 코드를 아무것도 하지 않도록 바꿔주면 됩니다.

.NET Reflector를 통해서 원래의 LicenseManager.Validate 정의를 보면 다음과 같습니다.

public static License Validate(Type type, object instance)
{
    License license;
    if (!ValidateInternal(type, instance, true, out license))
    {
        throw new LicenseException(type, instance);
    }
    return license;
}

그렇다면, 위의 메서드 정의를 이렇게 바꿔버리면 되는 것입니다.

public static License Validate(Type type, object instance)
{
    return null;
}

이번 글에서는, 실제로 위와 같은 동작을 하는 .NET Profiler를 ATL COM 프로젝트로 만들어 보겠습니다.




우선, ATL Simple 유형의 프로젝트를 생성하고, COM 개체를 하나 추가합니다. (이 예제에서는 COM 개체 이름을 CIgnore로 정했습니다.) 그런 후, .NET Profiler가 구현해야할 인터페이스인 ICorProfilerCallback, ICorProfilerCallback2, ICorProfilerCallback3의 뼈대를 담은 Impl 헤더를 마련해줍니다.

  • ICorProfilerCallbackImpl.h
  • ICorProfilerCallbackImpl2.h
  • ICorProfilerCallbackImpl3.h

위의 파일들은 대략 아래와 같은 내용의 헤더 파일입니다. (선언해 주어야 할 메서드 목록은 MSDN 도움말에서 ICorProfilerCallback, ICorProfilerCallback2, ICorProfilerCallback3 인터페이스를 찾아보시면 됩니다.)

template<class T>
class ATL_NO_VTABLE ICorProfilerCallbackImpl : public ICorProfilerCallback
{
public:
    ICorProfilerCallbackImpl() {};
    virtual ~ICorProfilerCallbackImpl() {};

    STDMETHOD(QueryInterface)(REFIID riid, void** ppvObject) = 0;
    _ATL_DEBUG_ADDREF_RELEASE_IMPL(ICorProfilerCallbackImpl)

    STDMETHOD(Initialize)(IUnknown * pICorProfilerInfoUnk)
    {
        return E_NOTIMPL;
    }

    STDMETHOD(Shutdown)()
    {
        return E_NOTIMPL;
    }
...[생략]...
};

이제 Ignore.h 헤더 파일에서 ICorProfilerCallback3Impl을 상속시켜 주고, COM_INTERFACE_ENTRY에도 추가해줍니다.

#include "ICorProfilerCallback3Impl.h"

class ATL_NO_VTABLE CIgnore :
    public CComObjectRootEx<CComMultiThreadModel>,
    public CComCoClass<CIgnore, &CLSID_Ignore>,
    public IDispatchImpl<IIgnore, &IID_IIgnore, &LIBID_OverrideLicenseLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
    public ICorProfilerCallback3Impl<CIgnore>
{
...[생략]...

BEGIN_COM_MAP(CIgnore)
    COM_INTERFACE_ENTRY(IIgnore)
    COM_INTERFACE_ENTRY(IDispatch)
    COM_INTERFACE_ENTRY(ICorProfilerCallback)
    COM_INTERFACE_ENTRY(ICorProfilerCallback2)
    COM_INTERFACE_ENTRY(ICorProfilerCallback3)
END_COM_MAP()
...[생략]...

ICorProfilerCallback::Initialize 구현


.NET Profiler가 CLR에 의해 로드되면 최초로 ICorProfilerCallback::Initialize 함수를 호출해 주는데, 이 때 임의의 초기화 코드를 넣어줄 수 있습니다. 따라서, CIgnore 클래스의 .h/.cpp 파일에 각각 Initialize 선언과 정의 추가를 추가해 줍니다.

==== Ignore.h ====

STDMETHOD(Initialize)(IUnknown * pICorProfilerInfoUnk);

CComQIPtr<ICorProfilerInfo> m_pICorProfilerInfo;   // .NET 1.0 때부터 지원되는 프로파일러 인터페이스
CComQIPtr<ICorProfilerInfo2> m_pICorProfilerInfo2; // .NET 2.0 때부터 지원되는 프로파일러 인터페이스
CComQIPtr<ICorProfilerInfo3> m_pICorProfilerInfo3; // .NET 4.0 때부터 지원되는 프로파일러 인터페이스

==== Ignore.cpp ====

HRESULT CIgnore::Initialize(IUnknown * pICorProfilerInfoUnk)
{
    if (pICorProfilerInfoUnk == NULL)
    { 
        return S_OK;
    }

    m_pICorProfilerInfo = pICorProfilerInfoUnk;
    if (m_pICorProfilerInfo == NULL)
    {
        return S_OK;
    }
 
    m_pICorProfilerInfo2 = pICorProfilerInfoUnk;
    m_pICorProfilerInfo3 = pICorProfilerInfoUnk;

    DWORD dwEventMask = COR_PRF_MONITOR_JIT_COMPILATION | COR_PRF_USE_PROFILE_IMAGES;

    m_pICorProfilerInfo->SetEventMask(dwEventMask);

    ::OutputDebugString(L"Profiler Initialize - End!\r\n");

    return S_OK;
}

위의 코드에서 중요한 것은 ICorProfilerInfo::SetEventMask 메서드에 전달되는 COR_PRF_MONITOR_JIT_COMPILATION, COR_PRF_USE_PROFILE_IMAGES 값입니다.

COR_PRF_MONITOR_JIT_COMPILATION 값을 전달해 주면 CLR 엔진은 메서드에 대한 JIT 컴파일 관련 콜백을 호출해 줍니다. 사실 사용자 코드를 프로파일링 하는 정도라면 이 옵션만으로도 충분한데 애석하게도 우리가 다루려는 LicenseManager.Validate 메서드는 SYSTEM.dll에 정의되어 있고 이 파일은 NGen에 의해서 미리 기계어로 컴파일 된 것을 사용하기 때문에 JIT 컴파일이 발생하지 않게 됩니다.

이 때문에, 기존에 NGen되어 있는 DLL들도 JIT 컴파일이 발생하도록 하기 위해 COR_PRF_USE_PROFILE_IMAGES 값을 함께 OR 연산 처리해서 SetEventMask에 넘겨주는 것입니다.

ICorProfilerCallback::JITCompilationStarted 구현


이제, 우리의 Profiler 개체(CIgnore)는 CLR 엔진이 JIT 컴파일을 하기 바로 직전에 JITCompilationStarted 콜백 함수를 불러주게 됩니다.

STDMETHOD(JITCompilationStarted)(FunctionID functionId, BOOL fIsSafeToBlock);

바로 이 단계에서 해당 메서드의 IL 코드를 변경해 줄 수가 있는데요. 우리가 원하는 것은 System.ComponentModel.LicenseManager라는 클래스의 Validate라는 이름의 메서드가 JIT 컴파일 될 때 IL 코드를 변경하는 것이기 때문에 JITCompilationStarted 콜백 함수 호출 시에 넘어오는 functionId 인자를 통해서 현재 JIT 컴파일 되는 메서드의 정보를 알아내서 Validate 메서드인지를 판단해 주어야 합니다.

그래서, 다음과 같이 해주면 클래스 및 메서드에 대한 이름을 구할 수 있습니다.

ClassID classId;
ModuleID moduleId = 0;
mdToken methodToken = 0;

HRESULT hr = m_pICorProfilerInfo->GetFunctionInfo(functionId, &classId, &moduleId, &methodToken);

// ========= 1. 필요한 인터페이스를 구하고,
IMetaDataImport* pMetaDataImport = NULL;
IMetaDataAssemblyImport* pMetaDataAssemblyImport = NULL;

hr = m_pICorProfilerInfo->GetModuleMetaData(moduleId, ( ofRead | ofWrite ), IID_IMetaDataAssemblyImport, (LPUNKNOWN *)&pMetaDataAssemblyImport );
pMetaDataAssemblyImport->QueryInterface(IID_IMetaDataImport, (LPVOID *)&pMetaDataImport);

ULONG cchFunction;
mdToken typeToken;
wchar_t szFunction[2048];
wchar_t szClass[2048];

// ========= 2. 메서드 이름을 구하고,
hr = pMetaDataImport->GetMethodProps(methodToken,
                                &typeToken,
                                szFunction,
                                2048, 
                                &cchFunction,
                                NULL, NULL, NULL, NULL, NULL);

// ========= 3. 타입명을 구하고,
ULONG cchClass;
hr = pMetaDataImport->GetTypeDefProps (typeToken,
                                szClass,
                                2048,
                                &cchClass,
                                0, 0);

타입과 메서드 이름을 구했으니 "System.ComponentModel.LicenseManager.Validate" 문자열을 비교해서 같을 때에만 IL 코드를 교체하는 다음 단계로 넘어가게 해주면 됩니다.

wchar_t buf[2048];
_snwprintf_s(buf, 2048, 2048, L"%s.%s", szClass, szFunction);

if (wcscmp(buf, L"System.ComponentModel.LicenseManager.Validate") != 0)
{
    break;
}

// 이하, rewrite IL code

이제, IL 코드를 교체하기 위해 IMetaDataEmit, IMethodMalloc 인터페이스를 각각 구하고,

hr = pMetaDataAssemblyImport->QueryInterface(IID_IMetaDataEmit, (LPVOID *)&pMetaDataEmit );
if (S_OK != hr)
{
    break;
}

hr = m_pICorProfilerInfo->GetILFunctionBodyAllocator(moduleId, &pMalloc);
if (S_OK != hr)
{
    break;
}

교체해 줄 IL 코드를 바이트 배열로 보관해 둡니다.

vector<BYTE> ilCodes;

ilCodes.push_back(CEE_LDNULL);
ilCodes.push_back(CEE_RET);

위의 코드에서 사용된 CEE_LDNULL이나 CEE_RET와 같은 상수 정의를 위해 다음과 같은 #include 문을 cpp 파일 상단에 추가해 주어야 합니다.

#define OPDEF( id, s, pop, push, args, type, l, OpCode1, OpCode2, ctrl ) id,
typedef enum enumOpcode
{
#include "opcode.def"
  CEE_COUNT,  /* number of instructions and macros pre-defined */
} OPCODE;
#undef OPDEF

그다음, 우리는 단순히 "return null;" 코드만을 포함한 메서드를 만드는 것이기 때문에 .NET Method 헤더를 Tiny 유형으로 정의해 줄 수 있습니다. 그렇게, Tiny 헤더 및 IL 코드를 포함하여 BYTE 배열에 담아주는 작업을 합니다.

IMAGE_COR_ILMETHOD ilHeader;
ilHeader.Tiny.Flags_CodeSize = (BYTE)((ilCodes.size() << 2) | CorILMethod_TinyFormat);

size_t allocationSize = ilCodes.size() + sizeof(ilHeader.Tiny);

BYTE *pAllocated = (BYTE *)pMalloc->Alloc((ULONG)allocationSize);

BYTE *pBytes = pAllocated;
memcpy(pBytes, &ilHeader.Tiny, sizeof(ilHeader.Tiny));
pBytes += sizeof(ilHeader.Tiny);

memcpy(pBytes, ilCodes.data(), ilCodes.size());
pBytes += ilCodes.size();

마지막 단계로, 기존 메서드를 무시하는 새로운 IL 코드로 된 메서드 정의로 교체해 주기만 하면 됩니다.

m_pICorProfilerInfo->SetILFunctionBody(moduleId, methodToken, (LPCBYTE)pAllocated);

정상적으로 빌드해 주면 끝!

프로파일러를 이용한 라이선스 우회 테스트


라이선스를 우회하는 .NET Profiler를 테스트 하기 위해 지난번 글에서 작성한 ConsoleApplication1.zip을 다운로드 받습니다.

압축을 풀고, *.lic, *.licx 파일을 제거한 후 다시 빌드 합니다. 그런 후 ConsoleApplication1.exe 파일을 실행해 주면 다음과 같이 '정상적으로' 라이선스 예외가 발생합니다.

D:\...[생략]...\bin\Debug>ConsoleApplication1.exe

Unhandled Exception: System.ComponentModel.LicenseException: An instance of type 'CommercialLibrary.LicFileLicensedClass1' was being created, and a valid license could not be granted for the type 'CommercialLibrary.LicFileLicensedClass1'. Please,  contact the manufacturer of the component for more information.
   at System.ComponentModel.LicenseManager.Validate(Type type, Object instance)
   at CommercialLibrary.LicFileLicensedClass1..ctor() in D:\...[생략]...\CommercialLibrary\Class1.cs:line 14
   at ConsoleApplication1.Program.Main(String[] args) in D:\...[생략]...\ConsoleApplication1\Program.cs:line 12

이제 명령행 창을 열고, 다음과 같이 .NET Profiler를 CLR 엔진에 알리는 환경 변수를 설정해 줍니다. (물론, 위에서 빌드한 CIgnore 개체는 regsvr32.exe에 의해서 레지스트리에 등록되어 있어야 합니다. x86/x64 용 DLL 구분도 주의하시고!)

SET COR_ENABLE_PROFILING=1
SET COR_PROFILER={F54F8382-DB3F-4847-A2ED-84DCA14B7433}   // CIgnore COM 개체의 CLSID 값

다시 ConsoleApplication1.exe를 실행시키면, Validate 메서드에서 예외가 발생하지 않고 통과하는 것을 확인할 수 있습니다.

이 정도면... .NET 세상에서 가히 무소불위의 권력이라고 할 수 있겠지요. (여러분들이 알고 있는 Visual Studio의 IntelliTrace 기능이 .NET Profiler의 실 사용예입니다.)



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

[연관 글]






[최초 등록일: ]
[최종 수정일: 2/15/2024]

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

비밀번호

댓글 작성자
 



2011-06-23 10시20분
Signature 분석을 직접 하려고 하지 마세요. 결국, 아래의 코드와 동일한 코드를 만들고 있는 자신을 발견하게 될 것입니다. ^^;

Sample: A Signature Blob Parser for your Profiler
; (broken) http://blogs.msdn.com/b/davbr/archive/2005/10/13/sample-a-signature-blob-parser-for-your-profiler.aspx

SigParse uploaded to MSDN Code Gallery
; (broken) http://blogs.msdn.com/b/davbr/archive/2010/08/25/sigparse-uploaded-to-msdn-code-gallery.aspx

Rewrite MSIL Code on the Fly with the .NET Framework Profiling API
; https://learn.microsoft.com/en-us/archive/msdn-magazine/2003/september/write-msil-code-on-the-fly-with-the-net-framework-profiling-api
정성태
2012-09-08 02시42분
정성태
2012-11-20 12시38분
New sample code for rewriting IL: ILRewrite Profiler
; (broken) http://blogs.msdn.com/b/davbr/archive/2012/11/19/new-sample-code-for-rewriting-il-ilrewrite-profiler.aspx
정성태
2013-09-07 03시37분
정성태
2014-01-31 03시28분
정성태

[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13607정성태4/25/2024185닷넷: 2248.C# - 인터페이스 타입의 다중 포인터를 인자로 갖는 C/C++ 함수 연동
13606정성태4/24/2024200닷넷: 2247. C# - tensorflow 연동 (MNIST 예제)파일 다운로드1
13605정성태4/23/2024352닷넷: 2246. C# - Python.NET을 이용한 파이썬 소스코드 연동파일 다운로드1
13604정성태4/22/2024392오류 유형: 901. Visual Studio - Unable to set the next statement. Set next statement cannot be used in '[Exception]' call stack frames.
13603정성태4/21/2024698닷넷: 2245. C# - IronPython을 이용한 파이썬 소스코드 연동파일 다운로드1
13602정성태4/20/2024801닷넷: 2244. C# - PCM 오디오 데이터를 연속(Streaming) 재생 (Windows Multimedia)파일 다운로드1
13601정성태4/19/2024850닷넷: 2243. C# - PCM 사운드 재생(NAudio)파일 다운로드1
13600정성태4/18/2024874닷넷: 2242. C# - 관리 스레드와 비관리 스레드
13599정성태4/17/2024868닷넷: 2241. C# - WAV 파일의 PCM 사운드 재생(Windows Multimedia)파일 다운로드1
13598정성태4/16/2024889닷넷: 2240. C# - WAV 파일 포맷 + LIST 헤더파일 다운로드2
13597정성태4/15/2024878닷넷: 2239. C# - WAV 파일의 PCM 데이터 생성 및 출력파일 다운로드1
13596정성태4/14/20241065닷넷: 2238. C# - WAV 기본 파일 포맷파일 다운로드1
13595정성태4/13/20241051닷넷: 2237. C# - Audio 장치 열기 (Windows Multimedia, NAudio)파일 다운로드1
13594정성태4/12/20241069닷넷: 2236. C# - Audio 장치 열람 (Windows Multimedia, NAudio)파일 다운로드1
13593정성태4/8/20241084닷넷: 2235. MSBuild - AccelerateBuildsInVisualStudio 옵션
13592정성태4/2/20241219C/C++: 165. CLion으로 만든 Rust Win32 DLL을 C#과 연동
13591정성태4/2/20241198닷넷: 2234. C# - WPF 응용 프로그램에 Blazor App 통합파일 다운로드1
13590정성태3/31/20241079Linux: 70. Python - uwsgi 응용 프로그램이 k8s 환경에서 OOM 발생하는 문제
13589정성태3/29/20241154닷넷: 2233. C# - 프로세스 CPU 사용량을 나타내는 성능 카운터와 Win32 API파일 다운로드1
13588정성태3/28/20241268닷넷: 2232. C# - Unity + 닷넷 App(WinForms/WPF) 간의 Named Pipe 통신 [2]파일 다운로드1
13587정성태3/27/20241170오류 유형: 900. Windows Update 오류 - 8024402C, 80070643
13586정성태3/27/20241335Windows: 263. Windows - 복구 파티션(Recovery Partition) 용량을 늘리는 방법
13585정성태3/26/20241131Windows: 262. PerformanceCounter의 InstanceName에 pid를 추가한 "Process V2"
13584정성태3/26/20241249개발 환경 구성: 708. Unity3D - C# Windows Forms / WPF Application에 통합하는 방법파일 다운로드1
13583정성태3/25/20241471Windows: 261. CPU Utilization이 100% 넘는 경우를 성능 카운터로 확인하는 방법
[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...