Und3r__Score__

Memory dump without frida 본문

취약점진단/Mobile (Android, iOS)

Memory dump without frida

_underscore_ 2024. 2. 19. 17:13

Android APP 진단 시 메모리 내 중요정보가 노출되고 있는지 확인하는 항목이 있습니다. 해당 항목을 확인하기 위해 일반적으로 Frida를 이용해서 메모리 덤프를 수행하는데, 최근 Frida 없이 진단해야 하는 CASE가 존재했습니다.
그래서 대안방안으로 gdb를 이용하여 메모리 덤프를 수행했고, 관련 스크립트 및 절차는 아래와 같습니다.
 

스크립트 및 파일

- gdb.exe
/ Windows에서 실행시킬 gdb client 파일
다운로드 페이지 (Windows 버전에 맞게 다운로드)

Debugger, gdb, for Windows

GNU Debugger for Windows         This web page provides 32-bit and 64-bit binaries of gdb for Windows for download. Equation Solution build the debugger from GNU gdb. It is a free software under General Public License. Distribution of GNU compilers

www.equation.com

 
- gdbserver
/ Android 에뮬레이터에서 실행시킬 gdb server 파일
다운로드 페이지 (adb shell getprop 명령어로 android 버전 확인) 

ndk/prebuilt - android_tools - Git at Google

chromium.googlesource.com

 
- gdbdump.bat 
/ line 2의 [process]를 덤프하고자하는 앱의 프로세스명으로 수정
/ memory를 덤프하려는 앱의 PID를 이용하여 메모리 매핑 정보를 가져오는 batch 파일

:::::::::: Get application pid
FOR /F "tokens=*" %%a IN ('adb shell ps -ef ^| findstr [process] ^| head -1 ^| awk "{print $2}"') do (SET pid=%%a)

@echo off
echo %pid%

:::::::::: Get application smaps info
adb shell grep "rw-p" /proc/%pid%/smaps | awk -F"[- ]" "{print $1, $2}" > maps.txt


::::/proc/[pid]/maps=Memory maps to executables and library files (2.4)
::::/proc/[pid]/smaps=An extension based on maps, showing the memory consumption of each mapping and flags associated with it

 
- gdbdump.py
/ Windows에서 실행시킬 python 파일 
/ gdbdump.bat 실행 결과인 maps.txt 파일을 이용하여 실행 중인 gdb.exe에 dump memory 명령어 입력하여 메모리 덤프
/ 메모리 덤프 완료 후 strings를 이용하여 문자열만 출력

import pyautogui
import subprocess
import time
import string
import os

######Run Batch file
subprocess.call([r'gdbdump.bat'])

######Activate gdb.exe window
win = pyautogui.getWindowsWithTitle("gdb.exe")[0]
print(win) 

win.activate()

######Dump memory function
def dumpMem(filenm, startAddr, endAddr):
    cmd = "dump memory " + filenm + " 0x" + startAddr + " 0x" + endAddr + "\n"
    pyautogui.write(cmd)
    
def stringsFile(filenm):
    cmd = "strings64.exe " + filenm + ">> strings.txt"
    os.system(cmd)
    
######Read maps.txt file
mapsFile = open("maps.txt", "r")
lines = mapsFile.readlines()

for line in lines[1:]:
    addr = line.split(' ')
    startAddr = addr[0].strip()
    endAddr = addr[1].strip()
    filenm = ".\\dump\\" + startAddr + "-" + endAddr + ".dump"
    
    dumpMem(filenm, startAddr, endAddr)
    
    time.sleep(2)

 
- strings.exe, strings64.exe / 다운로드 페이지
 

절차

① Windows에서 gdb.exe 파일 위치에 dump 폴더를 생성
 
② gdbserver 파일을 안드로이드 디바이스 내로 push, 권한 설정
     앱의 PID를 확인/이용하여 gdbserver 실행

> adb push gdbserver /data/local/tmp
> adb shell
# cd /data/local/tmp
# chmod 777 gdbserver
# ps -ef | grep [process] 
# ./gdbserver :9999 --attach [PID]

 
③ Windows에서 포트포워딩 수행

> adb forward tcp:9999 tcp:9999

 
④ gdb.exe 실행 및 원격 디버깅 설정

(gdb) target remote :9999

 
⑤ gdbdump.py 실행

> python gdbdump.py

strings.txt 파일과 dump 폴더 하위의 파일들 확인 가능