Microsoft MVP성태의 닷넷 이야기
.NET Framework: 216. 라이선스까지도 뛰어넘는 .NET Profiler [링크 복사], [링크+제목 복사],
조회: 22537
글쓴 사람
정성태 (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)
13518정성태1/9/20242397닷넷: 2197. C# - ClientWebSocket의 Ping, Pong 처리
13517정성태1/8/20242226스크립트: 63. Python - 공개 패키지를 이용한 위성 이미지 생성 (pystac_client, odc.stac)
13516정성태1/7/20242347닷넷: 2196. IIS - AppPool의 "Disable Overlapped Recycle" 옵션의 부작용
13515정성태1/6/20242630닷넷: 2195. async 메서드 내에서 C# 7의 discard 구문 활용 사례 [1]
13514정성태1/5/20242327개발 환경 구성: 702. IIS - AppPool의 "Disable Overlapped Recycle" 옵션
13513정성태1/5/20242229닷넷: 2194. C# - WebActivatorEx / System.Web의 PreApplicationStartMethod 특성
13512정성태1/4/20242185개발 환경 구성: 701. IIS - w3wp.exe 프로세스의 ASP.NET 런타임을 항상 Warmup 모드로 유지하는 preload Enabled 설정
13511정성태1/4/20242202닷넷: 2193. C# - ASP.NET Web Application + OpenAPI(Swashbuckle) 스펙 제공
13510정성태1/3/20242122닷넷: 2192. C# - 특정 실행 파일이 있는지 확인하는 방법 (Linux)
13509정성태1/3/20242172오류 유형: 887. .NET Core 2 이하의 프로젝트에서 System.Runtime.CompilerServices.Unsafe doesn't support netcoreapp2.0.
13508정성태1/3/20242245오류 유형: 886. ORA-28000: the account is locked
13507정성태1/2/20242907닷넷: 2191. C# - IPGlobalProperties를 이용해 netstat처럼 사용 중인 Socket 목록 구하는 방법파일 다운로드1
13506정성태12/29/20232488닷넷: 2190. C# - 닷넷 코어/5+에서 달라지는 System.Text.Encoding 지원
13505정성태12/27/20233015닷넷: 2189. C# - WebSocket 클라이언트를 닷넷으로 구현하는 예제 (System.Net.WebSockets)파일 다운로드1
13504정성태12/27/20232595닷넷: 2188. C# - ASP.NET Core SignalR로 구현하는 채팅 서비스 예제파일 다운로드1
13503정성태12/27/20232469Linux: 67. WSL 환경 + mlocate(locate) 도구의 /mnt 디렉터리 검색 문제
13502정성태12/26/20232583닷넷: 2187. C# - 다른 프로세스의 환경변수 읽는 예제파일 다운로드1
13501정성태12/25/20232333개발 환경 구성: 700. WSL + uwsgi - IPv6로 바인딩하는 방법
13500정성태12/24/20232429디버깅 기술: 194. Windbg - x64 가상 주소를 물리 주소로 변환
13498정성태12/23/20233117닷넷: 2186. 한국투자증권 KIS Developers OpenAPI의 C# 래퍼 버전 - eFriendOpenAPI NuGet 패키지
13497정성태12/22/20232514오류 유형: 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/20232524Linux: 66. 리눅스 - 실행 중인 프로세스 내부의 환경변수 설정을 구하는 방법 (gdb)
13495정성태12/20/20232445Linux: 65. clang++로 공유 라이브러리의 -static 옵션 빌드가 가능할까요?
13494정성태12/20/20232621Linux: 64. Linux 응용 프로그램의 (C++) so 의존성 줄이기(ReleaseMinDependency) - 두 번째 이야기
13493정성태12/19/20232801닷넷: 2185. C# - object를 QueryString으로 직렬화하는 방법
13492정성태12/19/20232443개발 환경 구성: 699. WSL에 nopCommerce 예제 구성
1  2  3  4  [5]  6  7  8  9  10  11  12  13  14  15  ...