대내외활동/2023년 1학기 도전학기제

[NLP / BCSA] UniASM 코드 실행(BCSA 논문 : Official implementation for UniASM: Binary Code Similarity Detection without Fine-tuning.)

LiaLi_1997 2023. 3. 29. 16:13
conda activate uniasm # 위에서 이미 uniasm 환경에 접속했으면 실행안해도 됨
sudo apt install radare2 # bin to asm, asm to bin 등을 지원하는 프로그램이다.

코드 유사도 검사시스템을 제작하면서 하나의 방법론으로 BCSA 를 담당하여 조사하고 코드를 돌려보게 되었다.

BCSA 에 대한 설명은 아래 논문리뷰에 있다.

https://rond-o.tistory.com/325

 

[논문 번역] A Survey of Binary Code Similarity

논문: A Survey of Binary Code Similarity본 논문은 binary diffing이 아닌 binary code diffing에 대한 survey를 제공한다.하지만 binary diffing과 공유될 수 있는 주요 개념/용어를 정의하고 있기도 하고, binary diffing과

rond-o.tistory.com

여기서는 BCSA 논문 중 "Binary Code Similarity Detection without Fine-tuning." 의 코드를 돌려보는 것을 정리한다.

깃허브 링크는 아래와 같다.

https://github.com/gym07/UniASM

 

GitHub - gym07/UniASM: Official implementation for UniASM: Binary Code Similarity Detection without Fine-tuning.

Official implementation for UniASM: Binary Code Similarity Detection without Fine-tuning. - GitHub - gym07/UniASM: Official implementation for UniASM: Binary Code Similarity Detection without Fin...

github.com

 


1. 환경 구축(Requirements)

기본적인 컴파일러(clang, clang++, gcc, g++ 같은 경우 깔려 있다고 가정 했다.)

conda create -n uniasm python=3.8
conda activate uniasm
pip install numpy==1.19.5 tensorflow==2.5.0 bert_pytorch scikit-learn==1.2.0 pandas==1.4.4 transformers==4.25.1 matplotlib==3.6.2 tqdm

 anaconda 가 깔려 있다는 가정하에 위의 코드를 입력하면 uniasm 이라는 가상환경을 생성해 준다.

 


2. ASM 파일 생성

실행파일(바이너리 파일) 생성

clang++ <source-file> -o <output-file>
# ex)
clang++ test1.cpp -o test1BinFile

위의 clang을 사용하여 binary 코드를 생성하고 아래를 실행하면 된다.

 

바이너리코드를 Assembly File 로 생성해 주는 Radare2 라고 하는 라이브러리가 있다.

필자는 docker ubuntu 20.04 환경에서 작업했다.

conda activate uniasm # 위에서 콘다 환경 만들 때 이 환경으로 접속했으면 생략


# radare2 는 바이너리코드 분석을 위한 도구이다.
# bin2asm.py 를 실행할 때 r2pipe 를 쓰기 위해 설치한다.
sudo apt install radare2 

# 위의 환경설정에서 아래 2개의 모듈이 빠져있었다. radare 설치 후 밑의 2개의 모듈 설치하면 문제없이 동작한다.
pip install r2pipe
pip install click

# ASM 파일 생성
# {실행파일} 에 대한 asm 파일을 testdata/{파일이름} 에 저장
python bin2asm.py -i testdata/bin/{실행파일} -o testdata/{asm이 저장될 폴더}
# ex)
python bin2asm.py -i testdata/bin/elfedit-gcc-o0 -o testdata/out-elfedit-gcc-o0
python bin2asm.py -i testdata/bin/elfedit-gcc-o1 -o testdata/out-elfedit-gcc-o1

1. 위의 {실행파일} 은 gcc, clang 등에서 뽑아낸 binary 파일, 즉 실행파일이다.

2. bin2asm.py 는 이러한 바이너리 파일을 함수단위로 어셈블리를 추출하여 저장해 준다.

3. 위에서 binary 파일은 testdata/bin 에 넣으면 된다.

 


3. ASM 파일에서 Embedding 추출 

# 바이너리에서 추출된 함수들이 testdata/{실행파일}/ 안에 저장된다.

# embedding.py 를 통해 asm 에서 embedding 추출
python embedding.py testdata/{실행파일}/{추출된 ASM 함수}
# ex)
python embedding.py testdata/out-elfedit-gcc-o0/dbg.adjust_relative_path

 위의 embedding.py 를 실행하면 각 함수에 대한 embedding 이 추출된다.

(similarity.py 에서 embedding 을 뽑는 함수가 따로 있는데 이 파이썬파일이 왜 있는지 아직 모르겠다. 

추후에 조사가 끝나면 수정하도록 하겠다.)

 


 

4. 2개의 ASM 함수에서 유사도 추출하기

# 유사도 측정
python similarity.py testdata/{실행파일1}/{실행파일1에서 추출한 특정함수의 ASM} testdata/{실행파일2}/{실행파일2에서 추출한 특정함수의 ASM}

# EX1)
python similarity.py testdata/out-elfedit-gcc-o0/dbg.adjust_relative_path testdata/out-elfedit-gcc-o1/dbg.adjust_relative_path
# EX2)
python similarity.py testdata/out-elfedit-gcc-o0/main testdata/out-elfedit-gcc-o1/main

위에서 함수단위로 asm 을 추출하는데 실행파일1, 실행파일2 에 각각 비교하고자 하는 함수를 비교하면 된다.

위의 명령어를 치면 아래와 같은 결과가 나온다.

100%|███████████████████████████████████| 1/1 [00:00<00:00, 977.01it/s]
100%|███████████████████████████████████| 1/1 [00:00<00:00, 972.25it/s]
0.91649896