비트(bit)주세요

[codeQL] U Boot Challenge - VS Code 본문

SUA/오픈소스 보안

[codeQL] U Boot Challenge - VS Code

yglee730 2021. 12. 11. 17:04
728x90

도전할 문제 

https://securitylab.github.com/ctf/uboot/

 

GitHub Security Lab CTF 2: U-Boot Challenge

Securing the world’s software, together

securitylab.github.com

 

1. 환경 세팅

위 링크에 접속해서 하단 부분에 준비해야 할 파일들이 존재한다.

 

1.1 VSCode 실행

 

1.2 확장팩 칸에서 codeQL 설치

설치가 완료되면 좌측 카테고리 칸에 [QL]이라는게 생긴다.

 

1.3 codeQL 실행 폴더 Clone

이 저장소를 clone 하면 된다.

https://github.com/github/vscode-codeql-starter

 

GitHub - github/vscode-codeql-starter: Starter workspace to use with the CodeQL extension for Visual Studio Code.

Starter workspace to use with the CodeQL extension for Visual Studio Code. - GitHub - github/vscode-codeql-starter: Starter workspace to use with the CodeQL extension for Visual Studio Code.

github.com

 

1.4 clone 폴더 open

 

1.5 데이터베이스 다운 후 압축 풀기

this snapshot 부분을 클릭해서 다운받으면 된다.

 

1.6 데이터베이스 선택

 

1.7 codQL CLI 다운 후 압축 풀기

Releases · github/codeql-action · GitHub

 

Releases · github/codeql-action

Actions for running CodeQL analysis. Contribute to github/codeql-action development by creating an account on GitHub.

github.com

 

1.8 CLI 선택

압축풀기 한 폴더에 들어가면 codeql.exe라는 파일이 있는데 그 경로를 복사해서 붙여넣으면 된다.

 

 

2. 문제 풀기

여기서 풀면 된다.

 

example.ql 파일에 작성한다.

 

import cpp

from Function f
where f.getName() = "strlen"
select f

 

이런식으로 해주면 된다.

 

import cpp

from Function f
where f.getName() = "memcpy"
select f

결과를 클릭하면, 이렇게 추적이 가능하다.

 

 

import cpp
from Macros m
where m.getName() = "ntoh(l|ll|s)"
select m

 

import cpp

from FunctionCall fc
where fc.getTarget().hasQualifiedName("memcpy")
select fc

 

import cpp

from MacroInvocation m
where m.getOutermostMacroAccess().getMacroName().regexpMatch("ntoh(l|ll|s)")
select m

 

import cpp

from MacroInvocation m
where m.getOutermostMacroAccess().getMacroName().regexpMatch("ntoh(l|ll|s)")
select m.getExpr()

 

/**
* @kind path-problem
*/

import cpp
import semmle.code.cpp.dataflow.TaintTracking
import DataFlow::PathGraph
 
class MyClass extends Expr {
    MyClass(){
        this = any(MacroInvocation m | m.getOutermostMacroAccess().getMacroName().regexpMatch("ntoh(l|ll|s)")).getExpr()
    }
}
 
class Config extends TaintTracking::Configuration {
    Config() { this = "NetworkToMemFuncLength" }
 
    override predicate isSource(DataFlow::Node source) {
        source.asExpr() instanceof MyClass
    }
    override predicate isSink(DataFlow::Node sink) {
        exists (FunctionCall fc |
            fc.getTarget().getName().regexpMatch("memcpy") and
            fc.getArgument(2) = sink.asExpr()
        )
    } 
}
 
from Config cfg, DataFlow::PathNode source, DataFlow::PathNode sink
where cfg.hasFlowPath(source, sink)
select sink, source, sink, "ntoh flows to memcpy"

 

최종 결과