https://blog.system32.kr/286

 

[Python] Pyinstaller Decompile (암호화되어 있는 바이너리 디컴파일)

오늘은 Pyinstaller를 통해서 암호화된 바이너리 디컴파일을 해보도록하겠습니다. 먼저 아래 사진은 그냥 심심해서 만들어 놓은 트위터 자동 글쓰기 프로그램입니다. 하지만 Pyinstaller을 통해서 암

blog.system32.kr

 

오늘은 Pyinstaller를 통해서 암호화된 바이너리 디컴파일을 해보도록하겠습니다.

먼저 아래 사진은 그냥 심심해서 만들어 놓은 트위터 자동 글쓰기 프로그램입니다.

하지만 Pyinstaller을 통해서 암호화 키를 탑재해서 컴파일을 했었지만, 암호가 기억이 안나 리버싱(?)을 통해서 디컴파일을 성공했습니다.

먼저 파일을 분해하기 위해서 pyinstallerextractor 을 사용하도록 하겠습니다. 다운로드 링크는 아래와 같습니다.

https://sourceforge.net/projects/pyinstallerextractor/

 

PyInstaller Extractor

Download PyInstaller Extractor for free. Extract contents of a Windows executable file created by pyinstaller. MIGRATED TO GITHUB https://github.com/extremecoders-re/pyinstxtractor This is a python script to extract the contents of a PyInstaller generated

sourceforge.net

먼저 해당 파일과 다운받은 Pyinstaller Extractor을 한 폴더에 집어 넣고, 다음과 같이 커맨드라인을 수행합니다.

python pyinstxtractor.py twitter.exe

실행한 결과 파일에 암호화가 되어 있다는 메시지와 함께 추출에 실패 했다는 메시지를 발견 할 수 있습니다.

 
[!] Error: Failed to decompress OpenSSL, probably encrypted. Extracting as is.
 
[!] Error: Failed to decompress OpenSSL.SSL, probably encrypted. Extracting as is.
 
[!] Error: Failed to decompress OpenSSL._util, probably encrypted. Extracting as is.
 
[!] Error: Failed to decompress OpenSSL.crypto, probably encrypted. Extracting as is.
 
[!] Error: Failed to decompress OpenSSL.version, probably encrypted. Extracting as is.
 
[!] Error: Failed to decompress PyQt5, probably encrypted. Extracting as is.
 
[!] Error: Failed to decompress PyQt5.uic, probably encrypted. Extracting as is.
 
[!] Error: Failed to decompress PyQt5.uic.Compiler, probably encrypted. Extracting as is.
 
[!] Error: Failed to decompress PyQt5.uic.Compiler.compiler, probably encrypted. Extracting as is.

해당 파일에 암호화가 되어 있는것은 기정 사실이고, 이 암호화를 우회하기 위해서는 몇가지 작업이 필요합니다.

커맨드 라인을 실행하면 다음과 같이 폴더 하나가 생성됩니다.

이 폴더내에는 제가 사용했던 함수들과 외부 모듈등이 함께 존재하며, 암호화 키를 입력하지 않았기 때문에 정상적으로 추출이 되지 않았습니다.

암호화 키를 찾기 위해서 검색과 삽질을 한 결과 암호화 키가 특정 부분에 저장된다는 사실을 알게 되었습니다.

다음과 같이 pyimod00_crypto_key 파일을 찾아줍니다.

HxD 프로그램을 통해서 열어보면 다음과 같이 암호화 키를 발견할 수 있습니다.

해당 키가 항상 저자리에 있는가에 대한 의문을 가졌고, 여러 파일을 만들어 테스트 해본결과 4E 29( N) ) 전 16자리는 항상 암호화 키를 나타내고 있습니다.

해당 키를 바탕으로 다시 디컴파일을 진행합니다. 디컴파일을 진행하기 위해서 다음과 같이 커맨드 라인을 작성합니다.

python pyinstxtractor.py twitter.exe --key=1q2w3e4r5t6y7u8i

동일하게 폴더를 들어가 보면, 바이너리 파일을 확인할 수 있습니다.

현재 twitter 파일은 pyc파일로 존재합니다. 약간의 수정을 더해줍니다.

42 0D 0D 0A 01 00 00 00 00 00 00 00 00 00 00 00

해당 hex값들을 twitter 파일 헤더에 넣습니다. (수정이 아닌 덧붙이기.)

그후 파일 확장자를 pyc를 붙여줍니다.

이제 남은건 디컴파일밖에 남지 않았습니다.

https://github.com/rocky/python-decompile3

 

GitHub - rocky/python-decompile3: Python decompiler for 3.7-3.8 Stripped down from uncompyle6 so we can refactor and start to fi

Python decompiler for 3.7-3.8 Stripped down from uncompyle6 so we can refactor and start to fix up some long-standing problems - GitHub - rocky/python-decompile3: Python decompiler for 3.7-3.8 Stri...

github.com

rocky님의 python-decompile3 도구를 받습니다. 설치까지 슥삭 해주고.

decompyle3 twitter.pyc > test.py

다음과 같이 실행을 해주면 py 코드로 반환된 결과를 확인할 수 있습니다.

난독화가 되어 나오는 이유는 제가 난독화해서 컴파일 했기 때문입니다.

 

긴글 읽어주셔서 감사합니다!

문의나 오탈자 등은 pental@kakao.com 또는 아래 댓글을 통해서 남겨주시면 감사하겠습니다.



출처: https://blog.system32.kr/286 [Pental - Forensics Story]

'경로 및 정보' 카테고리의 다른 글

Tomcat 에러 정보 제거  (0) 2022.03.23
bug bounty epub  (0) 2022.03.18
Netsparker LDAP 인젝션  (0) 2022.01.20
WinDbg 6.12 버전  (0) 2022.01.20
Sonarqube 소스 분석 및 owasp top 10 점검  (0) 2022.01.19
블로그 이미지

wtdsoul

,