如何使用GoKart对Go代码进行静态安全分析

时间:2021-12-15 03:37:00

如何使用GoKart对Go代码进行静态安全分析

关于GoKart

GoKart是一款针对Go代码安全的静态分析工具,该工具能够从Go源代码中查找使用了SSA(单一静态分配)形式的代码漏洞。GoKart能够跟踪变量和函数参数的来源,以确定输入源是否安全,与其他Go安全扫描程序相比,GoKart能够有效减少误报的数量。例如,与变量连接的SQL查询传统上可能标记为SQL注入,然而,GoKart可以确定变量实际上是常量还是类似常量的参数,在这种情况下GoKart不会报出漏洞。

工具安装

我们可以使用下列方式之一来安装GoKart。

(1) go install安装

广大研究人员可以使用下列命令安装GoKart:

  1. $goinstallgithub.com/praetorian-inc/gokart@latest

(2) Release安装

首先,我们需要访问该项目的【Releases页面】,并下载GoKart源码至本地系统。

接下来,我们可以下载checksums.txt文件来验证下载包的完整性:

  1. #Checkthechecksumofthedownloadedarchive
  2.  
  3. $shasum-a256gokart_${VERSION}_${ARCH}.tar.gz
  4.  
  5. b05c4d7895be260aa16336f29249c50b84897dab90e1221c9e96af9233751f22gokart_${VERSION}_${ARCH}.tar.gz
  6.  
  7. $catgokart_${VERSION}_${ARCH}_checksums.txt|grepgokart_${VERSION}_${ARCH}.tar.gz
  8.  
  9. b05c4d7895be260aa16336f29249c50b84897dab90e1221c9e96af9233751f22gokart_${VERSION}_${ARCH}.tar.gz

然后,提取已下载好的文档:

  1. $tar-xvfgokart_${VERSION}_${ARCH}.tar.gz

最后,将GoKart代码移动至我们的运行路径中:

  1. $mv./gokart/usr/local/bin/

(3) 源码安装

首先,我们需要使用下列命令将该项目源码克隆至本地:

  1. $gitclonehttps://github.com/praetorian-inc/gokart.git

然后切换至项目根目录,并构建源码:

  1. $cdgokart
  2.  
  3. $gobuild

最后,将GoKart代码移动至我们的运行路径中:

  1. $mv./gokart/usr/local/bin

Docker支持

构建Docker镜像:

  1. dockerbuild-tgokart.

运行容器,并执行本地扫描(本地扫描目录需要加载至容器镜像中):

  1. dockerrun-v/path/to/scan-dir:/scan-dirgokartscan/scan-dir

运行容器,并执行远程扫描(指定私钥以作认证,并加载至容器中):

  1. dockerrungokartscan-rhttps://github.com/praetorian-inc/gokart
  2.  
  3. #specifyingaprivatekeyforprivaterepositorysshauthentication
  4.  
  5. dockerrun-v/path/to/key-dir/:/key-dirgokartscan-rgit@github.com:praetorian-inc/gokart.git-k/key-dir/ssh_key

工具使用

针对当前目录中的Go模块运行GoKart:

  1. #runningwithoutadirectoryspecifieddefaultsto'.'
  2.  
  3. gokartscan<flags>

扫描不同目录中的Go模块:

  1. gokartscan<directory><flags>

查看帮助信息:

  1. gokarthelp

项目地址

GoKart:【GitHub传送门

原文地址:https://www.freebuf.com/articles/security-management/290657.html