[캡스톤] Slither 개념, 사용 방법, 결과 분석

2-pi-r·2025년 6월 2일
0

나는 캡스톤 프로젝트를 진행하면서 스마트 컨트랙트의 보안 취약점을 탐지하기 위해 Slither를 사용했다.

Slither란?

스마트 컨트랙트에서 보안 취약점을 검사할 수 있는 오픈 소스 도구
https://github.com/crytic/slither

  • Python3로 작성된, Solidity & Vyper 정적 분석 프레임워크

    • Solidity, Vyper: 스마트 컨트랙트를 작성하는 데 사용되는 프로그래밍 언어
    • 정적 분석(static analysis): 코드 실행 없이, 코드의 구문과 구조를 검사하는 방법
  • 기능:

    • 보안 취약점 탐지 (Detectors)
      • 99가지 유형 탐지 가능
      • 유형별 설명은 여기 참고
    • 코드 이해를 위한 시각적인 요약 제공 (Printers)
    • 사용자 정의 분석 프로토타입 지원 (Tools)

사용 방법 (보안 취약점 탐지)

1. Slither 설치

python3 -m pip install slither-analyzer

2. 분석할 Solidity 파일 준비

내 목적은 캡스톤 프로젝트에 적용하기 전에 Slither를 간단히 사용해보는 거라서 아무 코드나 돌려보려고 한다.

SmartBugs Wild Dataset에 보안 취약점을 포함하고 있는, 이미 작성된 스마트 컨트랙트 코드가 있다. 여기서 이 데이터 한 개를 이용했다.

3. Slither 실행

3-1) command로 실행

스마트 컨트랙트 프로젝트에 종속성이 있으면 아래 명령어를 쓰라고 한다. (출처: 공식 문서)

slither .

하지만 나는 지금 스마트 컨트랙트 코드 한 개를 돌리려고 하는 거니까 아래 명령어를 사용했다.

slither example.sol

i) Solidity Compiler(solc) 버전 오류 해결

그냥 실행했더니 에러가 떴다.

내가 2에서 다운받은 Solidity 코드의 버전은 0.4.23이고, 코드 내에서 0.4.x 버전의 Solidity 컴파일러를 사용하여 컴파일되어야 함을 명시하고 있다.

pragma solidity ^0.4.0;

하지만 에러 메시지를 보면 나한테 설치된 Solidity 컴파일러 버전은 0.8.30이기 때문에 컴파일에 실패한다.

해결하려면

  • 여기에서 알맞은 버전의 Solidity 컴파일러를 다운받고
  • command에 --solc 옵션으로 컴파일러 경로를 지정해주면 된다.
slither --solc C:/solidity-windows/solc_0.4.25/solc.exe example.sol

컴파일러 경로는 로컬에서 어디에 다운받는지에 따라 사람마다 다르다. 나중에 팀 프로젝트에서 사용할 때에는 코드에서 solc 경로를 매번 바꿔줘야 해서 불편했다. 이 문제는 solc 경로를 .env 파일에 환경변수로 추가하고, gitignore에 .env를 추가해서 해결했다.

ii) 분석 결과 출력

  • Compilation Warning/Error (컴파일 워닝/에러)
INFO:Detectors:
Claimable.transferOwnership(address) (0x0000000000027f6d87be8ade118d9ee56767d993.sol#67-69) should emit an event for:
        - pendingOwner = newOwner (0x0000000000027f6d87be8ade118d9ee56767d993.sol#68)
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#missing-events-access-control
  • Detectors (보안 취약점)
    • 아래가 취약점 1개 내용이다. 이런 게 여러 개 있었다.
INFO:Detectors:
Claimable.transferOwnership(address) (0x0000000000027f6d87be8ade118d9ee56767d993.sol#67-69) should emit an event for:
        - pendingOwner = newOwner (0x0000000000027f6d87be8ade118d9ee56767d993.sol#68)
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#missing-events-access-control
  • 요약
    • (3 contracts → 스마트 컨트랙트를 3개를
      • 2에서 다운받은 파일 까봤을 때 contract 구조체가 3개 있었음. 스마트 컨트랙트 코드 3개가 한 파일에 있었나봄.
    • with 100 detectors) → 100가지 유형의 취약점 검사기(detector)로 분석했고
      • 깃허브에 Detectors 99가지만 나와있는데 왜 100개인지 모르겠음. 다른 팀원들도 100개로 나오던데.
    • 7 result(s) found → 7개의 결과를 발견했다.
      • 위에서 Compilation Warning/Error 1개, Detectors 5개 출력했는데 왜 7개라는 건지는 모르겠음.
INFO:Slither:0x0000000000027f6d87be8ade118d9ee56767d993.sol analyzed (3 contracts with 100 detectors), 7 result(s) found

iii) --json 옵션

분석 결과를 json 파일로 저장

json 파일의 전체 구조는 아래와 같다.

{
"success",   // 예시: true
"error",     // 예시: null
"results" {
	   "detectors": [ {}, {}, ... ]    // 발견한 취약점들 정보
}

위에서 detectors의 원소는 아래와 같다.

{
		"elements" : [ {}, {}, ... ],
		
		"description", // 발견한 취약점의 설명
		..., 
		"id",     // 발견한 취약점의 id
		"check",  // detector (취약점 유형)
		"impact", // 해당 유형의 impact
		"confidence" // 해당 유형의 confidence
}

위에서 elements의 원소는 아래와 같다.

{
		"type",
		"name",
		"source_mapping": {...},
		"type_specific_fields": {
								"parent": {...},  // {} 안은 "element"와 동일
								"signature"       // 없을 수도 있음.
														}
}

iv) --print 옵션

분석한 코드의 다양한 정보를 출력

slither --print contract-summary example.sol
slither --print data-dependency example.sol

나는 해보지는 않았다.

3-2) Python API로 실행

from slither import Slither

# solidity file로 Slither Object 생성
slither = Slither('example.sol')

0개의 댓글