Microsoft MVP성태의 닷넷 이야기
.NET Framework: 216. 라이선스까지도 뛰어넘는 .NET Profiler [링크 복사], [링크+제목 복사],
조회: 30778
글쓴 사람
정성태 (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분
정성태

... 106  [107]  108  109  110  111  112  113  114  115  116  117  118  119  120  ...
NoWriterDateCnt.TitleFile(s)
11249정성태7/12/201718545오류 유형: 410. LoadLibrary("[...].dll") failed - The specified procedure could not be found.
11248정성태7/12/201725036오류 유형: 409. pip install pefile - 'cp949' codec can't decode byte 0xe2 in position 208687: illegal multibyte sequence
11247정성태7/12/201719378오류 유형: 408. SqlConnection 객체 생성 시 무한 대기 문제파일 다운로드1
11246정성태7/11/201718144VS.NET IDE: 118. Visual Studio - 다중 폴더에 포함된 파일들에 대한 "Copy to Output Directory"를 한 번에 설정하는 방법
11245정성태7/10/201723765개발 환경 구성: 321. Visual Studio Emulator for Android 소개 [2]
11244정성태7/10/201723317오류 유형: 407. Visual Studio에서 ASP.NET Core 실행할 때 dotnet.exe 프로세스의 -532462766 오류 발생 [1]
11243정성태7/10/201720002.NET Framework: 666. dotnet.exe - 윈도우 운영체제에서의 .NET Core 버전 찾기 규칙
11242정성태7/8/201720276제니퍼 .NET: 27. 제니퍼 닷넷 적용 사례 (7) - 노후된 스토리지 장비로 인한 웹 서비스 Hang (멈춤) 현상
11241정성태7/8/201719023오류 유형: 406. Xamarin 빌드 에러 XA5209, APT0000
11240정성태7/7/201721954.NET Framework: 665. ClickOnce를 웹 브라우저를 이용하지 않고 쿼리 문자열을 전달하면서 실행하는 방법 [3]파일 다운로드1
11239정성태7/6/201723430.NET Framework: 664. Protocol Handler - 웹 브라우저에서 데스크톱 응용 프로그램을 실행하는 방법 [5]파일 다운로드1
11238정성태7/6/201720960오류 유형: 405. NT 서비스 시작 시 "Error 1067: The process terminated unexpectedly." 오류 발생 [2]
11237정성태7/5/201722603.NET Framework: 663. C# - PDB 파일 경로를 PE 파일로부터 얻는 방법파일 다운로드1
11236정성태7/4/201725846.NET Framework: 662. C# - VHD/VHDX 가상 디스크를 마운트하지 않고 파일을 복사하는 방법파일 다운로드1
11235정성태6/29/201719969Math: 20. Matlab/Octave로 Gram-Schmidt 정규 직교 집합 구하는 방법
11234정성태6/29/201717316오류 유형: 404. SharePoint 2013 설치 과정에서 "The username is invalid The account must be a valid domain account" 오류 발생
11233정성태6/28/201717232오류 유형: 403. SharePoint Server 2013을 Windows Server 2016에 설치할 때 .NET 4.5 설치 오류 발생
11232정성태6/28/201718229Windows: 144. Windows Server 2016에 Windows Identity Extensions을 설치하는 방법
11231정성태6/28/201718844디버깅 기술: 86. windbg의 mscordacwks DLL 로드 문제 - 세 번째 이야기 [1]
11230정성태6/28/201718022제니퍼 .NET: 26. 제니퍼 닷넷 적용 사례 (6) - 잦은 Recycle 문제
11229정성태6/27/201719259오류 유형: 402. Windows Server Backup 관리 콘솔이 없어진 경우
11228정성태6/26/201716718개발 환경 구성: 320. Visual Basic .NET 프로젝트에서 내장 Manifest 자원을 EXE 파일로부터 제거하는 방법파일 다운로드1
11227정성태6/19/201724474개발 환경 구성: 319. windbg에서 python 스크립트 실행하는 방법 - pykd [6]
11226정성태6/19/201716328오류 유형: 401. Microsoft Edge를 실행했는데 입력 반응이 없는 경우
11225정성태6/19/201715645오류 유형: 400. Outlook - The required file ExSec32.dll cannot be found in your path. Install Microsoft Outlook again.
11224정성태6/13/201718135.NET Framework: 661. Json.NET의 DeserializeObject 수행 시 속성 이름을 동적으로 바꾸는 방법파일 다운로드1
... 106  [107]  108  109  110  111  112  113  114  115  116  117  118  119  120  ...