Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 2개 있습니다.)

.NET Reflector + Reflexil 도구를 이용해 DLL 코드 변경

(비록 유료지만) .NET Reflector에는 "Reflexil"이라는 좋은 도구가 있습니다.

Reflexil - The .NET Assembly Editor
; http://reflexil.net/

설치 방법은 위의 사이트에서 다운로드 받아 (Reflexil 자체는 무료임) 압축을 해제하면 "Reflexil.Reflector.AIO.dll" 파일을 얻을 수 있는데 이것을 .NET Reflector의 "Tools" / "Add-Ins..." 메뉴에 추가해 줍니다.

자... 그럼 어떤 능력이 있는지 한번 실습해 볼까요? ^^

예제로 다음의 코드를 포함한 ConsoleApplication1.exe를 만들고,

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("test");
        }
    }
}

빌드된 ConsoleApplication1.exe 파일을 .NET Reflector에서 불러와 수정하고 싶은 코드가 담긴 메서드로 이동합니다.

reflexil_1.png

이 상태에서 "Tools" / "Reflexil v1.7" 메뉴를 선택하면 해당 메서드에 대해 IL 코드로 디컴파일한 창이 하단에 뜹니다.

reflexil_2.png

Reflexil의 훌륭한 점은, 바로 디컴파일 창에서 IL 코드를 새롭게 추가/변경/삭제할 수 있다는 것입니다.

reflexil_3.png

간단한 예를 들기 위해 Console.WriteLine("test")를 두번 수행하도록 할 텐데, "nop"을 삭제하고 "Create new..." 명령을 통해 "ret" 명령어 이전에 ldstr/call 명령어를 추가해서 다음과 같은 결과를 만들어 냈습니다.

reflexil_4.png

마지막으로 왼쪽의 "Object Browser" 창에 있는 "ConsoleApplication1 (1.0.0.0)" 노드를 마우스 우클릭하면 나오는 "Reflexil v1.7" / "Save as..." 명령을 실행해서 IL 코드 변경 사항이 반영된 exe로 파일을 출력합니다.

해당 파일을 실행하면, 당연히 화면에는 "test" 문자열이 두번 출력되는 것을 확인할 수 있습니다.




"Create new..."로 명령어 추가하는 것이 매우 불편한데요. 이를 위해 "Replace all with code..." 메뉴가 제공됩니다. 이를 선택하면 다음과 같은 창이 뜨는데,

reflexil_5.png

보시는 바와 같이 왼쪽의 창에 코드 편집기가 제공되고 변경을 하길 원하는 메서드의 코드를 완전히 새롭게 작성할 수 있습니다. 작성 후 "Compile" 메뉴를 누르면 성공한 경우 우측에 그에 해당하는 IL 코드 목록이 나옵니다. 이 정도면... Reflexil 측에서 많이 신경쓴 것 같습니다. ^^)




당연히! 코드 뿐 아니라 리소스 파일을 바꾸는 것도 가능합니다. 위의 예제 프로젝트에서 리소스로 다음의 내용을 포함한 "item.js"라는 파일을 추가하고,

alert('test');

Main 메서드는 해당 리소스를 출력하도록 코드를 변경합니다.

using ConsoleApplication1.Properties;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Resources.item); // 출력 결과: alert('test');
        }
    }
}

빌드한 후 ConsoleApplication1.exe 파일을 .NET Reflector로 로드하고 "Tools" / "Reflexil v1.7" 메뉴를 선택하면 다음과 같이 리소스를 편집할 수 있는 Hexa 에디터가 제공됩니다.

reflexil_6.png

Hexa 에디터에서 원하는 데로 alert('test'); 문장을 변경/추가/삭제하고 다시 "Reflexil v1.7" / "Save as..." 명령으로 변경 사항을 저장합니다.

그럼 새롭게 변경된 리소스를 포함한 EXE 파일이 생성됩니다.




참고로, 강력한 이름으로 서명된 어셈블리를 변경한다면? 다음의 글에 나온 경고 문구를 알아두세요.

Case of the Unknown Error App Crash ? Debugging & Patching Someone Else’s .NET Race Condition
; https://chentiangemalc.wordpress.com/2014/03/20/case-of-the-unknown-error-app-crash-debugging-patching-someone-elses-net-race-condition/

Warning: .NET DLL patching is more of a pain, as you need to use the “Strong Name Remover” feature of reflexil, which I have seen corrupt EXEs before…take a backup first, as strong name remover overwrites your existing files.




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 6/24/2014]

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

비밀번호

댓글 작성자
 



2014-07-04 02시35분
정성태
2014-09-19 03시07분
[테스트해보고] 아.. 지금 해봤는데 크기가 크면 저장시에 out of memory exception 이 뜨는군요
65메가짜리 dll 이었습니다. 작은건 잘 되네요 :)
[guest]
2014-09-19 10시03분
65메가면... 평범하진 않군요. ^^
정성태
2016-01-20 09시20분
예전에 이 글 보고 그때는 IL을 다룰 줄 몰라 그냥 지나쳤었는데 근래에 IL 사용법을 좀 익히고 이 글을 다시 보고는 급한 상황에 잘 써먹었습니다.
고객사가 거리도 멀고 보안이 까다로워선 배포된 DLL의 문제점을 보고 본사에 돌아와서 소스코드로 수정하는게 귀찮아선 혹시나 하고 가져갔던 Reflector와 ReflexIL을 써먹었네요.
좋은 글 감사드립니다.^^
Beren Ko

... 76  77  78  79  80  81  [82]  83  84  85  86  87  88  89  90  ...
NoWriterDateCnt.TitleFile(s)
11887정성태5/8/201921505.NET Framework: 829. C# - yield 문을 사용할 수 있는 메서드의 조건
11886정성태5/7/201919310오류 유형: 534. mstest.exe 실행 시 "Visual Studio Enterprise is required to execute the test." 오류 [2]
11885정성태5/7/201916260오류 유형: 533. mstest.exe 실행 시 "File extension specified '.loadtest' is not a valid test extension." 오류 발생
11884정성태5/5/201921053.NET Framework: 828. C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법 - 두 번째 이야기
11883정성태5/3/201926235.NET Framework: 827. C# - 인터넷 시간 서버로부터 받은 시간을 윈도우에 적용하는 방법파일 다운로드1
11882정성태5/2/201922522.NET Framework: 826. (번역글) .NET Internals Cookbook Part 11 - Various C# riddles파일 다운로드1
11881정성태4/28/201922636오류 유형: 532. .NET Core 프로젝트로 마이그레이션 시 "CS0579 Duplicate 'System.Reflection.AssemblyCompanyAttribute' attribute" 오류 발생
11880정성태4/25/201918487오류 유형: 531. 이벤트 로그 오류 - Task Scheduling Error: m->NextScheduledSPRetry 1547, m->NextScheduledEvent 1547
11879정성태4/24/201926907.NET Framework: 825. (번역글) .NET Internals Cookbook Part 10 - Threads, Tasks, asynchronous code and others파일 다운로드2
11878정성태4/22/201922686.NET Framework: 824. (번역글) .NET Internals Cookbook Part 9 - Finalizers, queues, card tables and other GC stuff파일 다운로드1
11877정성태4/22/201922746.NET Framework: 823. (번역글) .NET Internals Cookbook Part 8 - C# gotchas파일 다운로드1
11876정성태4/21/201921735.NET Framework: 822. (번역글) .NET Internals Cookbook Part 7 - Word tearing, locking and others파일 다운로드1
11875정성태4/21/201922784오류 유형: 530. Visual Studo에서 .NET Core 프로젝트를 열 때 "One or more errors occurred." 오류 발생
11874정성태4/20/201922969.NET Framework: 821. (번역글) .NET Internals Cookbook Part 6 - Object internals파일 다운로드1
11873정성태4/19/201921564.NET Framework: 820. (번역글) .NET Internals Cookbook Part 5 - Methods, parameters, modifiers파일 다운로드1
11872정성태4/17/201922351.NET Framework: 819. (번역글) .NET Internals Cookbook Part 4 - Type members파일 다운로드1
11871정성태4/16/201920931.NET Framework: 818. (번역글) .NET Internals Cookbook Part 3 - Initialization tricks [3]파일 다운로드1
11870정성태4/16/201919237.NET Framework: 817. Process.Start로 실행한 콘솔 프로그램의 출력 결과를 얻는 방법파일 다운로드1
11869정성태4/15/201925077.NET Framework: 816. (번역글) .NET Internals Cookbook Part 2 - GC-related things [2]파일 다운로드2
11868정성태4/15/201921061.NET Framework: 815. CER(Constrained Execution Region)이란?파일 다운로드1
11867정성태4/15/201920242.NET Framework: 814. Critical Finalizer와 SafeHandle의 사용 의미파일 다운로드1
11866정성태4/9/201923334Windows: 159. 네트워크 공유 폴더(net use)에 대한 인증 정보는 언제까지 유효할까요?
11865정성태4/9/201919152오류 유형: 529. 제어판 - C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools is not accessible.
11864정성태4/9/201917848오류 유형: 528. '...' could be '0': this does not adhere to the specification for the function '...'
11863정성태4/9/201917740디버깅 기술: 127. windbg - .NET x64 EXE의 EntryPoint
11862정성태4/7/201920255개발 환경 구성: 437. .NET EXE의 ASLR 기능을 끄는 방법
... 76  77  78  79  80  81  [82]  83  84  85  86  87  88  89  90  ...