Microsoft MVP성태의 닷넷 이야기
.NET Framework: 216. 라이선스까지도 뛰어넘는 .NET Profiler [링크 복사], [링크+제목 복사],
조회: 30612
글쓴 사람
정성태 (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)
13818정성태11/15/20245264Windows: 272. Windows 11 24H2 - sudo 추가
13817정성태11/14/20244926Linux: 106. eBPF / bpf2go - (BPF_MAP_TYPE_HASH) Map을 이용한 전역 변수 구현
13816정성태11/14/20245386닷넷: 2312. C#, C++ - Windows / Linux 환경의 Thread Name 설정파일 다운로드1
13815정성태11/13/20244809Linux: 105. eBPF - bpf2go에서 전역 변수 설정 방법
13814정성태11/13/20245278닷넷: 2311. C# - Windows / Linux 환경에서 Native Thread ID 가져오기파일 다운로드1
13813정성태11/12/20245028닷넷: 2310. .NET의 Rune 타입과 emoji 표현파일 다운로드1
13812정성태11/11/20245248오류 유형: 933. Active Directory - The forest functional level is not supported.
13811정성태11/11/20244848Linux: 104. Linux - COLUMNS 환경변수가 언제나 80으로 설정되는 환경
13810정성태11/10/20245374Linux: 103. eBPF (bpf2go) - Tracepoint를 이용한 트레이스 (BPF_PROG_TYPE_TRACEPOINT)
13809정성태11/10/20245252Windows: 271. 윈도우 서버 2025 마이그레이션
13808정성태11/9/20245256오류 유형: 932. Linux - 커널 업그레이드 후 "error: bad shim signature" 오류 발생
13807정성태11/9/20244983Linux: 102. Linux - 커널 이미지 파일 서명 (Ubuntu 환경)
13806정성태11/8/20244901Windows: 270. 어댑터 상세 정보(Network Connection Details) 창의 내용이 비어 있는 경우
13805정성태11/8/20244736오류 유형: 931. Active Directory의 adprep 또는 복제가 안 되는 경우
13804정성태11/7/20245366Linux: 101. eBPF 함수의 인자를 다루는 방법
13803정성태11/7/20245319닷넷: 2309. C# - .NET Core에서 바뀐 DateTime.Ticks의 정밀도
13802정성태11/6/20245693Windows: 269. GetSystemTimeAsFileTime과 GetSystemTimePreciseAsFileTime의 차이점파일 다운로드1
13801정성태11/5/20245481Linux: 100. eBPF의 2가지 방식 - libbcc와 libbpf(CO-RE)
13800정성태11/3/20246321닷넷: 2308. C# - ICU 라이브러리를 활용한 문자열의 대소문자 변환 [2]파일 다운로드1
13799정성태11/2/20244906개발 환경 구성: 732. 모바일 웹 브라우저에서 유니코드 문자가 표시되지 않는 경우
13798정성태11/2/20245505개발 환경 구성: 731. 유니코드 - 출력 예시 및 폰트 찾기
13797정성태11/1/20245491C/C++: 185. C++ - 문자열의 대소문자를 변환하는 transform + std::tolower/toupper 방식의 문제점파일 다운로드1
13796정성태10/31/20245381C/C++: 184. C++ - ICU dll을 이용하는 예제 코드 (Windows)파일 다운로드1
13795정성태10/31/20245163Windows: 268. Windows - 리눅스 환경처럼 공백으로 끝나는 프롬프트 만들기
13794정성태10/30/20245261닷넷: 2307. C# - 윈도우에서 한글(및 유니코드)을 포함한 콘솔 프로그램을 컴파일 및 실행하는 방법
13793정성태10/28/20245134C/C++: 183. C++ - 윈도우에서 한글(및 유니코드)을 포함한 콘솔 프로그램을 컴파일 및 실행하는 방법
1  2  3  4  [5]  6  7  8  9  10  11  12  13  14  15  ...