tmxklab

DLL Injection using shim 본문

Security/07 Malware Technique

DLL Injection using shim

tmxk4221 2021. 4. 22. 01:04

0. 목차

1. shim이란

MS에서 윈도우 애플리케이션 호환성 인프라스트럭쳐/프레임(애플리케이션 shim)은 이전 버전의 OS 에서 동작하는 프로그램이 최신 버전의 OS에서 동작하도록 하는 기능이다. 이는 애플리케이션 호환성 수정(shim)을 통해 이뤄진다.

shim은 MS가 개발자에게 제공하는 것으로 개발자가 코드를 재작성하지 않고 프로그램에 수정사항을 적용하여 호환성을 유지할 수 있도록 도와준다. shim이 적용된 프로그램은 IAT에 있는 포인터를 shim 코드의 주소로 교체되므로, 실행될 때 shim엔진API 호출을 shim 코드로 리다이렉트한다.

즉, 윈도우 API를 후킹하여 DLL에 있는 API를 직접 호출하지 않고 shim코드로 호출을 리다이렉트한다.

Example)

예를 들어, Window XP에서 동작하는 애플리케이션이 Window 7에서 실행시키는 경우 애플리케이션은 GetVersion() API를 통해 Window Version이 다른 것을 확인하고 실행되지 않을 것이다. Windows 7에서 실행시키기 위해서는 코드를 수정하고 프로그램을 재빌드하거나 WinXPVersionLie를 호출하는 shim을 해당 애플리케이션에 적용하는 방법이 존재한다.

만약 shim이 적용된 애플리케이션이 Windows 7에서 실행하면 GetVersion()을 호출해 OS Version을 확인할 경우 shim엔진이 가로채 Window XP Version으로 반환한다.

(shim엔진이 IAT를 수정하여 GetVersion() API 호출을 shim 코드로 리다이렉션, WinXPVersionLie shim은 애플리케케이션 수정없이 Window XP에서 실행되고 있는 것으로 속임)

+) WinXPversionLie

WinXPVersionLie는 컴퓨터에서 실행중인 실제 운영 체제 버전에 관계없이 Windows XP 버전 정보를 응용 프로그램에 반환하므로 응용 프로그램이 계속 실행될 수 있다.

shim 관련 자료

Demystifying Shims - or - Using the App Compat Toolkit to make your old stuff work with your new stuff
First published on TECHNET on Jun 17, 2011 com·pat·i·ble adjective \kəm-ˈpa-tə-bəl\ : capable of existing together in harmony What is a Shim? A shim is one of the very few four-letter words in use by Microsoft that isn't an acronym of some sort. It's a metaphor based on the English l...
https://techcommunity.microsoft.com/t5/ask-the-performance-team/demystifying-shims-or-using-the-app-compat-toolkit-to-make-your/ba-p/374947

MS는 애플리케이션의 동작을 변경하고자 적용할 수 있는 수백 개의 shim을 제공하는데 이러한 shim들 중에서 일부를 공격자들이 악용해 지속성과 코드 인젝션, 권한 상승으로 악성코드를 실행한다.

2. DLL Injcetion을 위한 shim 생성 및 공격방법

Target Process에 DLL Injection을 위해 shim을 생성해보도록 하자.

1) shim 생성 방법

① shim을 적용할 애플리케이션 선택

② 애플리케이션을 위한 Shim Database(sdb) 만들기

③ DB(.sdb) 저장하기

④ DB 설치하기

shim을 생성하기 위해서는 애플리케이션 호환성 툴킷(ACT, Application Compatibility)을 다운로드해야 한다. Win 10에서는 ADK와 번들로 제공됨

ADK download for Windows 10
Windows Assessment and Deployment Kit (ADK) for Windows 10 provides new and improved deployment tools for automating large-scale deployments of Windows 10. Download the Windows Assessment and Deployment Kit (ADK) for Windows 10 Windows ADK for Windows 10 documentation
https://support.microsoft.com/ko-kr/windows/adk-download-for-windows-10-2a0b7ff2-79b7-b989-f727-43ae506e36ad

앞서 언급한 shim 생성 방법 및 DLL Injection하는 과정에 대해서는 다음 링크를 참고하자

shim을 이용한 DLL Injection 참고자료)

Windows Persistence using Application Shimming
In this article, we are going to describe the persistence of the Application Shimming and how vital it is in Windows Penetration Testing.
https://www.hackingarticles.in/windows-persistence-using-application-shimming/

2) 공격 방법

Target Application(공격 대상이 자주 사용하는 정상적인 서드파티 애플리케이션)에 대한 SDB를 생성한다. 공격자는 단일 InjectDLL과 같은 shim 또는 여러 개의 shim을 선택할 수 있다.

② 생성한 SDB파일을 드롭한 후, 일반적으로 sdbinst 유틸리티를 이용해 설치한다.

Target Application을 호출하거나 유저가 Target Application을 실행할 때까지 기다린다.

④ 공격자는 또한 SDB를 설치한 악성코드를 삭제할 수 있다. 이러한 경우 SDB파일만 남는다.

+) 공격자는 SDB파일을 파일 시스템의 특정 위치에 드롭하고, 최소한의 레지스트리 항목을 수정해 SDB를 설치할 수 있다. 이 기술은 sdbinst 유틸리티를 사용하지 않는다.

다음 링크에서는 programdata 디렉터리에 DLL을 드롭하고 sdbinst 유틸리티를 사용하지 않고 shim을 설치해 드롭한 DLL을 explorer.exe프로세스에 인젝션하는 PoC이다.

hasherezade/persistence_demos
Demos for the presentation "Wicked malware persistence methods".
https://github.com/hasherezade/persistence_demos

→ 들어가서 shim_persist 클릭

3) 주요 shim

악성코드 제작자는 shim을 이용하여 persistance, code Inejction, UAC bypass 등 다양한 목적으로 악용할 수 있다.

shim

shim NameDescription
RedirectEXE실행을 리다이렉션
InjectDLL애플리케이션에 DLL Injection
DisableNXShowUIDEP 비활성화
CorrectFilePaths파일 시스템 경로 리다이렉션
VirtualRegistry레지스트리 리다이렉션
RelaunchElevated상승된 권한으로 애플리케이션 실행
TerminateExe실행 중인 실행 파일 종료
DisableWindowsDefender윈도우 디펜더 비활성화
RunAsAdmin관리자 권한으로 애플리케이션 표시

3. shim Artifact

shim Artifact로는 레지스트리, \AppPatch, 제어판 설치 프로그램 항목이 있다.

SDB를 설치하면 설치 관리자는 GUID를 데이터베이스에 대해 생성하고 SDB파일을

%SystemRoot%\AppPatch\Custom\<GUID>.sdb (32bit) 또는

%SystemRoot%\AppPatch\Custom\Cutom64\<GUID>.sdb (64bit)에 복사한다.

  • C:\Windows\AppPatch\Custom\
  • C:\Windows\AppPatch\Custom\Custom64\

또한 다음 레지스트리 키에 2개의 항목을 생성한다.

  • HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Custom\
  • HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\InstalledSDB

shim을 적용한 애플리케이션이 실행될 때 로더가 이들 레지스트리 항목을 참고해 애플리케이션의 shim 적용 여부를 결정하고, shim을 적용하고자 AppPatch\ 에 위치한 SDB파일의 설정을 위해 사용되는 shim엔진을 호출하고자 만들어진다.

추가적인 아티팩트로는 SDB를 설치함으로써 제어판의 설치 프로그램 목록에 추가된다.

4. SDB 분석

공격자는 Application에 shim을 적용하기 위해 공격 대상의 FileSystem에 있는 shim DB(.sdb)를 설치한다. 따라서, 악의적인 행위로 사용된 sdb를 분석하기 위해서 다음과 같은 도구를 사용하여 .sdb파일을 분석할 수 있다.

evil-e/sdb-explorer
sdb-explorer is a tool that provides the ability to read and write Microsoft Fix-It In-memory patches, also known as SDB files. Note the version of apphelp.dll in Windows 10 does not include the function SeiApplyPatch. This was the function responsible for patching and flushing the instruction cache.
https://github.com/evil-e/sdb-explorer
williballenthin/python-sdb
Pure Python parser for Application Compatibility Shim Databases (.sdb files) Please see the included scripts in the samples/ directory. You can use these to dump and format shim database files for human consumption. They also serve as a reference to interacting with the module.
https://github.com/williballenthin/python-sdb

다음 링크를 참고하면 SDB Detection, Forensic, Analysis 등 다양한 툴들을 확인해볼 수 있다.

Shim Database Tools
https://sdb.tools/tools.html

추가 관련자료)

Hunting for Application Shim Databases - F-Secure Blog
You arrive at your desk, fire up your computer, and open up one of your daily applications, such as email. Could this simple act execute malicious code onto your computer? It could, using Application Shims. This post for threat hunters will explore how to hunt for malicious Shim installation and execution.
https://blog.f-secure.com/hunting-for-application-shim-databases/

+) 관련 악성코드 - dridex

RedirectEXE shim을 이용하여 UAC Bypass

Dridexが用いる新たなUAC回避手法 (2015-02-09) - JPCERT/CC Eyes
今回は、マルウエアDridexが2014年12月頃から用いるようになった、新しいUAC回避手法について紹介します。 Dridexは、インターネットバンキングの不正送金に関係するマルウエアの1種として知られている、C&CサーバとHTTPで通信するボットです。 JPCERT/CCで確認しているDridexの多くは、図 ...
https://blogs.jpcert.or.jp/ja/2015/02/uac-bypass.html

'Security > 07 Malware Technique' 카테고리의 다른 글

Obfuscation(encoding, crypto, packing)  (0) 2021.04.22
Hooking (악성코드 관련)  (0) 2021.04.22
다양한 인젝션 기술  (0) 2021.04.20
SetWindowsHookEx DLL Injection  (0) 2021.04.20
APC Injection  (4) 2021.04.19
Comments