Obfuscar 사용법 (코드 난독화 도구, obfuscator, obfuscation) (C#)

Obfuscar 코드 난독화 도구를 소개해 드리려고 합니다. 코드 난독화는 obfuscator, obfuscation라고 하며, 오늘 소개해드릴 프로그램의 이름은 Obfuscar랍니다.

NuGet 설치

코드 난독화 도구를 사용하기 위해 NuGet을 설치해 주십시오. 본 게시물은 난독화가 어떻게 이루어지는가에 대한 담론이 아니라 사용법에 대한 안내문이니 바로 본론으로 넘어가겠습니다~!

Obfuscar

.NET 코드 난독화 오픈소스 Obfuscar를 사용해 보도록 합시다. 누겟 패키지 관리에서 설치를 진행해 주세요. (프로젝트를 우클릭 하신 다음에 [NuGet 패키지 관리] 를 눌러주세요.)
obfuscar 를 검색하신 다음에 설치를 진행합니다.

<참고사항>

패키지 관리자 콘솔 명령어로 바로 진행하시려면 다음을 입력하시면 됩니다.
Install-Package Obfuscar
해당 게시물에서는 패키지 관리 방식으로 진행하겠습니다.

xml 파일 추가하기

add xml file

이제 obfuscar 실행 시 매개변수로 사용할 xml 파일을 추가하겠습니다. 프로젝트 > 추가 > 새 항목 > xml 파일을 추가해 주세요.

xml 파일 설정하기

xml

설정 값을 원하시는대로 설정하시면 됩니다. 너무 많이 설정하면 프로그램이 작동하지 않기 때문에 적당히 하셔야 됩니다!
설정정보는 공식 홈페이지에 있습니다. 아래는 공식 홈페이지 설정 주소입니다.
Obfuscar는 오픈소스이므로 프로그램소스를 공부하시려면 깃허브에 가보시면 도움이 많이 된답니다. 
obfuscar 매개변수를 수정합니다.
매개변수 목록은 아까 소개해드린 GitHub 에 있습니다.

xml 파일을 우클릭하신 다음에, [출력 디렉터리에 복사] 를 [새 버전이면 복사] 로 해주세요.
[빌드 작업] 은 [내용] 으로 합니다.

빌드 후 이벤트로 난독화 실행파일 바로 만들기

post build event


저는 obfuscar.xml 파일을 빌드 출력 경로 2단계 위쪽 파일로 설정을 해놓아서 이렇게 추가하였습니다.

"$(Obfuscar)" ../../obfuscar.xml

다른 방법을 소개해드립니다.
프로젝트 속성으로 이동합니다.
빌드 이벤트 > 빌드 후 이벤트 명령줄에 다음을 입력합니다.

"$(Obfuscar)" obfuscar.xml

이것으로 설정을 완료했습니다. 결과를 확인해 보겠습니다.

난독화 테스트

테스트 소스

난독화 테스트 소스입니다. 소스코드는 매우 간단하게 만들어보았습니다.

private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
    Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
    e.Handled = true;
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (MessageBox.Show("Do You Want To Close?", "Closing", MessageBoxButton.YesNo) == MessageBoxResult.No)
    {
        e.Cancel = true;
    }
}

ILSpy 테스트 1차

ILSpy

ILSpy 프로그램(디컴파일러, 리버싱 프로그램)으로 빌드된 본래 소스 파일을 열어보았습니다. 값이 거의 그대로 보이실 겁니다.

ILSpy 테스트 2차

ILSpy-test

난독화로 만들어진 파일을 열어보겠습니다. Window_Closing 부분에 문자열이 일부가 변형이 되어 알아보기 힘들게 되었습니다. 읽기 어렵게 되어 원래의 형태를 알아볼 수가 없는 것을 확인할 수 있답니다.

아래는 게시일 기준 매개변수 목록입니다.

Name

Description

InPath

Directory containing the input assemblies, such as c:\\in.

OutPath

Directory to contain the obfuscated assemblies, such as c:\\out.

LogFile

Obfuscation log file path (mapping.txt).

XmlMapping

Whether the log file should be of XML format.

KeyFile

Key file path, such as c:\folder\key.pfx.

KeyContainer

Key container name.

RegenerateDebugInfo

Whether to generate debug symbols for obfuscated assemblies.

MarkedOnly

Whether to only obfuscate marked items. All items are obfuscated when set to false.

RenameProperties

Whether to rename properties.

RenameEvents

Whether to rename events.

RenameFields

Whether to rename fields.

KeepPublicApi

Whether to exclude public types and type members from obfuscation.

HidePrivateApi

Whether to include private types and type members from obfuscation.

ReuseNames

Whether to reuse obfuscated names.

UseUnicodeNames

Whether to use Unicode characters as obfuscated names.

UseKoreanNames

Whether to use Korean characters as obfuscated names.

HideStrings

Whether to hide strings.

OptimizeMethods

Whether to optimize methods.

SuppressIldasm

Whether to include an attribute for ILDASM to indicate that assemblies are obfuscated.

AnalyzeXaml

Whether to analyze XAML related metadata for obfuscation.

댓글