CodeQL for VSCode搭建流程

0收藏

0点赞

浏览量:711

2022-02-14

举报

 

Visual Studio Code安装

都是免费的哈哈哈
官网下载

 

CodeQL安装

这里给出两种安装方式

1. 使用Chocolatey安装CodeQL cli

使用choco安装可透过指令执行且环境变数也会自动设定好
管理员权限打开cmd
运行choco install codeql

安装成功

如果想要自定义安装目录的话,需要更改一下环境变量
ChocolateyInstall就是默认的安装路径

2. gihub下载

先下载codeql-cli
选择对应操作系统下载即可

https://github.com/github/codeql-cli-binaries
下载好解压至自己选定的文件夹,再添加系统变量
这里我将codeql文件夹名称改为codeql-cli

在命令行输入codeql,如下图即配置成功
若提示找不到该命令,重启后再执行命令

下载codeql规则库(使用starter workspace可跳过此步骤)
https://github.com/github/codeql
在codeql文件夹下打开cmd
执行git clone https://github.com/github/codeql codeql-repo
下载完后如图

 

下载之后安装codeql插件

有三种方法

  1. 在VS插件中搜索codeql点击安装
  2. 浏览器打开商城下载
  3. github中下载VSIX文件
    在more actions中选择从VSIX文件安装

 

配置codeql-cli

终端无法访问扩展管理的cli
可以自己下载codeql,并在插件中配置路径
这样终端可以访问
这个插件是使用codeql-cli编译并运行规则的,要确保下载的版本支持该插件即可

Executable Path输入codeql.exe所在路径

 

配置规则

两种方法建立codeql workspace
第一种就是把要审计的代码放入codeql中
第二种是把codeql加入要审计的代码的workspace中

1. 使用‘starter workspace’也就是git仓库

  1. 下载starter
    git clone --recursive https://github.com/github/vscode-codeql-starter/
    或者
    git clone https://github.com/github/vscode-codeql-starter/
    项目下载完成后,进入项目目录
    git submodule update --init
    git submodule update --remote
    确保包含需要的子模块
    截图使用的是第一种方法

    子模块需要定期更新
  2. 在VS Code中打开starter workspace

注意:
starter子模块中包括C/C++, C#, Java, JavaScript, Python, Ruby以及GO的规则,在vscode-codeql-starter\ql下
CodeQL暂时无法扫描php代码

2. 将CodeQL规则库加入现有的工作站

此种方法未实现,仅按文档进行流程说明

将下载好的本地CodeQL库加入你的workspace
CodeQL库下载链接

  1. 选择File > Add Folder to Workspace,加入下载好的codeql库
  2. 每种语言都要创建一个新的文件夹
    通过New Folder 或者 Add Folder to Workspace选项
    放置你的自定义规则和规则库
  3. 在每个语言目录下新建一个qlpack.yml文件
    用来告诉CodeQL,这个目录下的语言类型和依赖包
    例如:
    在C++代码目录下自定义一个CodeQL文件夹名为’my-custom-cpp-pack’
    在C++代码目录下创建一个qlpack.yml文件,内容如下
name: my-custom-cpp-pack
version: 0.0.0
libraryPathDependencies: codeql/cpp-all

注意:
GO语言的规则库并不在 github/codeql 中
扫描GO代码,需要下载https://github.com/github/codeql-go
然后将规则像上面说的这样加入规则库
(starter中都是包含的)

 

运行CodeQL

选择数据库

扫描分析一个project,我们需要建立一个CodeQL数据库
在侧边栏打开CodeQL数据库
如图有四种添加数据库的方法

当添加数据库之后,会有数据库视图
可以右击列表中的项进行数据库交互
可以利用Ctrl/Cmd+click选择多个数据库

创建本地数据库

如果没有存在的数据库导入,也可以先创建一个本地数据库
有三种方法

通过coedql-cli创建一个数据库

执行命令:
`codeql database create <database> --language=<language-identifier>`

参数说明:
<database>:创建数据库的路径,目录会在执行命令的时候被创建
--language: 指定数据库语言,输入标识符。当和—db-cluster一起使用时,可以指定多个,用’,’分隔,也可以进行多次指定。
--db-cluster:为多种语言创建数据库
--command:创建一个或多个编译语言数据库的时候使用。python和JavaScript/TypeScript不需要该参数,如果编译语言不带该参数,codeql会自动检测并编译
--no-run-unnecessary-builds:为多语言创建数据库,且包括编译和非编译语言时,可以利用 --no-run-unnecessary-builds来帮助非编译语言跳过command选项
更多参数说明
CodeQL支持以下语言

语言标识符
C/C++cpp
C#csharp
GOgo
Javajava
JavaScript/TypeScriptjavascript
Pythonpython
Rubyruby

找了codeql-repo中的python案列测试创建数据库成功

官方给出的command案例

Specifying build commands

The following examples are designed to give you an idea of some of the build commands that you can specify for compiled languages.

Important

The --command option accepts a single argument—if you need to use more than one command, specify --command multiple times.

If you need to pass subcommands and options, the whole argument needs to be quoted to be interpreted correctly.

  • C/C++ project built using make:
    codeql database create cpp-database --language=cpp --command=make
    
  • C# project built using dotnet build (.NET Core 3.0 or later):
    codeql database create csharp-database --language=csharp --command='dotnet build /t:rebuild'
    

    On Linux and macOS (but not Windows), you need to disable shared compilation when building C# projects with .NET Core 2 or earlier, so expand the command to:

    codeql database create csharp-database --language=csharp --command='dotnet build /p:UseSharedCompilation=false /t:rebuild'
    
  • Go project built using the COEQL_EXTRACTOR_GO_BUILD_TRACING=on environment variable:
    CODEQL_EXTRACTOR_GO_BUILD_TRACING=on codeql database create go-database --language=go
    
  • Go project built using a custom build script:
    codeql database create go-database --language=go --command='./scripts/build.sh'
    
  • Java project built using Gradle:
    codeql database create java-database --language=java --command='gradle clean test'
    
  • Java project built using Maven:
    codeql database create java-database --language=java --command='mvn clean install'
    
  • Java project built using Ant:
    codeql database create java-database --language=java --command='ant -f build.xml'
    
  • Project built using Bazel:
    # Navigate to the Bazel workspace.
    
    # Before building, remove cached objects
    # and stop all running Bazel server processes.
    bazel clean --expunge
    
    # Build using the following Bazel flags, to help CodeQL detect the build:
    # `--spawn_strategy=local`: build locally, instead of using a distributed build
    # `--nouse_action_cache`: turn off build caching, which might prevent recompilation of source code
    # `--noremote_accept_cached`, `--noremote_upload_local_results`: avoid using a remote cache
    codeql database create new-database --language=<language> \
      --command='bazel build --spawn_strategy=local --nouse_action_cache --noremote_accept_cached --noremote_upload_local_results //path/to/package:target'
    
    # After building, stop all running Bazel server processes.
    # This ensures future build commands start in a clean Bazel server process
    # without CodeQL attached.
    bazel shutdown
    
  • Project built using a custom build script:
    codeql database create new-database --language=<language> --command='./scripts/build.sh'
    

    This command runs a custom script that contains all of the commands required to build the project.

剩下两种方法未测试,仅给出说明链接

编写运行规则,建议通过官方给出的练习题进行练习

 

传送门

CodeQL官方文档
练习题目



(来源:安全客)

(原文链接:https://www.anquanke.com/post/id/266823

发表评论

点击排行

钓鱼邮件-如何快速成为钓鱼达人

一、前言在大型企业边界安全做的越来越好的情况下,不管是 APT 攻击还是红蓝对抗演练,钓鱼邮件攻击使用的...

【渗透实战系列】| 1 -一次对跨境赌博类APP的渗透实战(getshell并获得全部数据)

本次渗透实战主要知识点:1.app抓包,寻找后台地址2.上传绕过,上传shell3.回shell地址的分析4.中国蚁剑工...

HTTPS - 如何抓包并破解 HTTPS 加密数据?

HTTPS 在握手过程中,密钥规格变更协议发送之后所有的数据都已经加密了,有些细节也就看不到了,如果常规的...

无线电安全攻防之GPS定位劫持

一、需要硬件设备HackRFHackRF 连接数据线外部时钟模块(TCXO 时钟模块)天线(淘宝套餐中的 700MHz-2700MH...

记一次Fastadmin后台getshell的渗透记录

1.信息搜集先来看看目标站点的各种信息后端PHP,前端使用layui,路由URL规则看起来像ThinkPHP,那自然想到...

华为防火墙实战配置教程,太全了

防火墙是位于内部网和外部网之间的屏障,它按照系统管理员预先定义好的规则来控制数据包的进出。防火墙是系...

ADCS系列之ESC1、ESC8复现

对原理感兴趣的可以去https://www.specterops.io/assets/resources/Certified_Pre-Owned.pdf看原文,这里只...

【干货分享】利用MSF上线断网主机的思路分享

潇湘信安&nbsp;Author 3had0w潇湘信安一个不会编程、挖SRC、代码审计的安全爱好者,主要分享一些安全经验、...

扫描二维码下载APP