컴퓨터/c#

c# 프로그램 난독화 (ConfuserEx, Obfuscar)

k1asd1 2021. 8. 10. 17:53
728x90
반응형

c#으로 만든 프로그램 중 난독화가 적용되지 않은 프로그램은 디컴파일러로 소스가 쉽게 노출될 수 있다는 사실을 접하고 기록으로 남기기 위해 작성합니다.

 

c#으로 개발하고 fody로 단일화하여 현재 사용 중인 프로그램을 디컴파일 프로그램 (DotPeek, ILSpy 등)으로 열어보니 정말 제가 코딩하였던 소스와 사용하였던 컨트롤들이 정확히 그대로 노출되었습니다.

  ILSpy로 확인한 난독화 전의 C#으로 개발된 프로그램

소스가 이렇게 쉽게 노출이 되니 난독화를 위해 'ConfuserEx'와 'Obfuscar' 이 두 프로그램을 사용하였으나 'ConfuserEx'의 경우 계속 오류가 발생하여 'Obfuscar'으로 진행하도록 하겠습니다.


사용한 디컴파일러 프로그램(DotPeek, ILSpy)과 난독화 프로그램(ConfuserEx, Obfuscar) 입니다.

 

* JetBrains DotPeek

https://www.jetbrains.com/ko-kr/decompiler/

 

dotPeek: JetBrains가 만든 무료 .NET 디컴파일러 및 어셈블리 브라우저

 

www.jetbrains.com

 

 

* ILSpy

https://github.com/icsharpcode/ILSpy

 

GitHub - icsharpcode/ILSpy: .NET Decompiler with support for PDB generation, ReadyToRun, Metadata (&more) - cross-platform!

.NET Decompiler with support for PDB generation, ReadyToRun, Metadata (&more) - cross-platform! - GitHub - icsharpcode/ILSpy: .NET Decompiler with support for PDB generation, ReadyToRun, Metada...

github.com

 

* ConfuserEx (바이러스 오진으로 인해 실행파일 형태로 실행할 것을 추천합니다.)

https://github.com/yck1509/ConfuserEx

 

GitHub - yck1509/ConfuserEx: An open-source, free protector for .NET applications

An open-source, free protector for .NET applications - GitHub - yck1509/ConfuserEx: An open-source, free protector for .NET applications

github.com

 

* Obfuscar (Nuget으로 설치)

https://www.obfuscar.com/

 

Obfuscar Copy on Strikingly

Obfuscar, simple and open source obfuscation tool for .NET assemblies.

obfuscar2.mystrikingly.com

 

* Obfuscar Visual Studio에서 사용 방법

https://stackoverflow.com/questions/43936803/how-to-install-and-use-obfuscar

 

How to install and use obfuscar?

I just finished my C# project(WPF), but now I meet problems on using "obfuscar" (another applicaion would be welcome, if easier to use). Of course I looked already on internet for it, but didn't f...

stackoverflow.com


난독화 방법은 컴파일 후 난독화를 하는 방법과 cmd에서 obfuscar 실행파일과 xml파일 지정하여 수동으로 하는 방법이 있는데 결과물은 똑같으니 전 컴파일 후 난독화를 하는 방법으로 진행하겠습니다.

 

우선 'obfuscar'를 설치하고 설정 파일을 지정해주도록 하겠습니다.

 

1. Visual Studio의 Nuget으로 'obfuscar'를 설치하고 난독화를 진행하고자 하는 프로젝트의 속성을 선택합니다.

 

2. 빌드 이벤트를 선택하고 빌드 후 이벤트 명령줄에 다음의 명령어를 삽입하고 저장합니다.

- "$(Obfuscar)" obfuscar.xml

빌드 이벤트

3. 컴파일 후 출력 폴더에 'obfuscar.xml' 파일을 생성하고 편집합니다.

- obfuscar.xml

<?xml version='1.0'?>
<Obfuscator>
    <Var name="InPath" value="." />
    <Var name="OutPath" value=".\Obfuscator_Output" />

<Module file="$(InPath)\my_file.exe" />
</Obfuscator>

* InPath : 난독화할 실행 파일 또는 DLL이 있는 경로 (전 실행파일과 같이 있으므로 '.')

* OutPath : 난독화 후 출력 폴더

* $(InPath)\my_file.exe -> 난독화 대상 파일입니다. (InPath의 경로 활용)

 

- 변수 추가

https://docs.obfuscar.com/getting-started/configuration.html#settings

 

Configuration — obfuscar 2.2 documentation

Obfuscar accepts a single command line argument: the path to its configuration file. The configuration file is used to specify what assemblies should be obfuscated, where to find the dependencies for the assemblies, and where the obfuscated assemblies shou

docs.obfuscar.com

 

- 변수 추가 예제

<?xml version='1.0'?>
<Obfuscator>
    <Var name="InPath" value="." />
    <Var name="OutPath" value=".\Obfuscator_Output" />

    <Var name="KeepPublicApi" value="false" />
    <Var name="HidePrivateApi" value="true" />
    <Var name="RenameProperties" value="true" />
    <Var name="RenameEvents" value="true" />
    <Var name="RenameFields" value="true" />
    <Var name="UseUnicodeNames" value="true" />
    <Var name="HideStrings" value="true" />
    <Var name="OptimizeMethods" value="true" />
    <Var name="SuppressIldasm" value="true" />
    <Module file="$(InPath)\my_file.exe" />
</Obfuscator>

 

4. 'obfuscar.xml'을 편집하고 컴파일합니다.

 

※ 난독화 후 실행 파일 실행 시 각종 오류가 발생하기도 합니다. 오류 발생 시 적절히 값을 바꿔가며 테스트가 필요해 보입니다.


난독화가 적절히 잘 되고 ILSpy로 확인하였습니다.

  ILSpy로 확인한 난독화 후의 C#으로 개발된 프로그램

사용된 DLL이나 메서드나 컨트롤 이름이 보이긴 하나 실제 사용한 값들은 빨간색 네모처럼 되어 있으니 성공했습니다.

 

이상입니다.

728x90
반응형