본문 바로가기

해킹/IOS 앱 취약점 진단

LLDB를 이용하여 IOS(아이폰) 원격 디버깅 세팅하기

취약점을 찾기위해서는 분석은 필수입니다. 

그러나 모바일 환경은 PC와 다르기 때문에 약간 다른 방법을 사용하여 디버깅해야 합니다. 

이번 글에서는 IOS 앱을 분석하기 위한 원격 디버깅 준비 과정을 설명합니다. 

 

준비물은 아래와 같습니다. 

탈옥된 IOS, XCODE가 설치된 맥북, USB케이블

 

만약 XCODE 또는 맥북이 없다면 아래의 바이너리를 사용해보세요. 

아이폰5S IOS 12.4에서 테스트된 바이너리입니다. 다른 버전에서는 동작할지 모르겠습니다.

debugserver
9.32MB

 

먼저 XCODE를 켜고 아래와 같이 새로운 프로젝트를 아무 이름으로 생성합니다. 

그리고 아래와 같이 연결된 핸드폰으로 설정하고 핸드폰에 맞는 버전에 맞춰줍니다.

 

그리고 핸드폰 설정하는 부분 왼쪽에 있는 재생 비슷한 버튼을 누르게 되면

USB로 연결된 아이폰에 앱이 설치가 되고 실행됩니다. 

(이 과정에 Signing 이슈가 발생할 수 있습니다. 아래 링크를 참고하거나 구글 검색해보시면 나올겁니다.)

https://dundinstudio.com/tutorial-imessage-sticker-xcode-build-fail/

 

[iMessageSticker/튜토리얼] Xcode 빌드실패 Provisioning 설정이 꼬였을 때 해결법

iMessageSticker를 등록하고자 하는 사람 중에는 개발은 전혀 해 본적 없는 디자이너나 일러스트레이터인 경우가 많다. 이런 사람들이 갑자기 Xcode니 빌드 타겟, provisioning, 그리고 certificate과 같은 생

dundinstudio.com

 

IOS에서 프로세스를 출력해보면 다음과 같은 프로세스를 찾을 수 있습니다.

Chanui-iPhone:~ root# ps -ef |grep debug
  501  3077     1   0 12:02AM ??         0:00.70 /Developer/usr/bin/debugserver --lockdown --launch=frontboard
    0  3083  2449   0 12:02AM ttys001    0:00.01 grep debug

 

먼저 debugserver 바이너리를 홈 디렉토리로 옮깁니다.

Chanui-iPhone:~ root# cp /Developer/usr/bin/debugserver ./

 

그리고 맥북으로 돌아와서 test.xml 파일을 생성하여 아래의 내용을 저장합니다.

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>com.apple.springboard.debugapplications</key>
	<true/>
	<key>get-task-allow</key>
	<true/>
	<key>task_for_pid-allow</key>
	<true/>
	<key>run-unsigned-code</key>
	<true/>
</dict>
</plist>

 

그리고 해당 파일을 아이폰에 업로드합니다.

unknownui-MacBook-Pro:~ unknown1$ scp test.xml root@172.30.1.36:/var/root

 

다시 아이폰으로 돌아와서 아래의 명령어를 사용해 바이너리를 변경하고 /usr/bin 디렉토리로 옮깁니다.

Chanui-iPhone:~ root# ldid -Stest.xml debugserver 
Chanui-iPhone:~ root# cp ./debugserver /usr/bin/

 

그 후 아래의 명령을 사용해 1234포트로 AppStore 디버깅 서버를 열 수 있습니다.

Chanui-iPhone:~ root# /usr/bin/debugserver *:1234 -a AppStore
debugserver-@(#)PROGRAM:LLDB  PROJECT:lldb-900.3.87
 for arm64.
Attaching to process AppStore...
Listening to port 1234 for a connection from *...

 

아래와 같이 핸드셰이크 오류가 난다면 디버그 서버를 실행할 때 아이폰IP가 아니라 

디버깅할 IP, 즉 LLDB를 실행시킬 PC의 아이피를 입력하여 디버그 서버를 열면 해결됩니다.

(lldb) process connect connect://172.30.1.36:4444
error: failed to get reply to handshake packet

 

debugserver 뒤에 LLDB를 실행시킬 IP를 넣고, 포트는 임의로 아무거나 넣으셔도 됩니다.

Chanui-iPhone:~ root# /usr/bin/debugserver 172.30.1.32:4444 -a AppStore
debugserver-@(#)PROGRAM:LLDB  PROJECT:lldb-900.3.87
 for arm64.
Attaching to process AppStore...
Listening to port 4444 for a connection from 172.30.1.32...

 

그리고 연결할 때는 아이폰의 IP와 위에서 입력한 포트로 접속합니다.

(lldb) process connect connect://172.30.1.36:4444
Process 3270 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
    frame #0: 0x00000001b997c0f4 libsystem_kernel.dylib`mach_msg_trap + 8
libsystem_kernel.dylib`mach_msg_trap:
->  0x1b997c0f4 <+8>: ret    

libsystem_kernel.dylib`mach_msg_overwrite_trap:
    0x1b997c0f8 <+0>: mov    x16, #-0x20
    0x1b997c0fc <+4>: svc    #0x80
    0x1b997c100 <+8>: ret    
Target 0: (AppStore) stopped.
(lldb) c
Process 3270 resuming

 

아마도 버그같은데 금방 고쳐지겠죠!

즐거운 아이폰 해킹되시길 바랍니다.