LoginSignup
0

More than 1 year has passed since last update.

posted at

updated at

(WIP)脆弱性検出ツールとしての、SonarQube#1

これは何

SonarCloud(SonarQubeのデモサイト)を使用して、C言語のコードを解析した1
C言語の解析には癖があるため、手順を示す。
構築のとっかかりになれば幸いです

SonarQubeは簡単に言えば、静的なコード解析ツールで、27言語&1000のコードチェックルールを搭載したツールです。
一般的にコード品質に用いられると思いますが、セキュリティの品質を測ることもできるそうです。
後者の側面を深堀できれば良いかなと思ってます。

環境

サーバ側の準備

https://sonarcloud.io/
にアクセスし、gihubアカウントでソーシャルログイン

▼ fig.1
image.png
[Analyze New project]を選択

C言語のコードが含まれているリポジトリを選択し、"Set UP"
▼ fig.2
image.png

▼ fig.3
image.png
[Manually]を選択し、C言語>Linuxを選択し、待機

▼ fig.4
image.png

▼ fig.5
image.png

※あとで、sonnar-scannerを実行した時に”Not authorized. ”が出ないよう「execute analysis」の権限にチェックを入れておく
▼fig.6
image.png

▼fig.7
image.png

サーバ側の準備は以上

クライアント側の準備

構築のステップ:

1.EC2でLinux用のAMI(amzn2-ami-hvm-2.0.20200917.0-x86_64-gp2)を立ち上げて、接続

2.前述のfig.4画面を参照し、「Build Wrapper」と「SonarScanner」をダウンロード。インスタンスにアップロードする。

 #ローカルで
 sudo scp -i key.pem sonar-scanner-cli-4.4.0.2170-linux.zip   ec2-user@1.1.1.1:/home/ec2-user/  
 #ec2で
 unzip sonar-scanner-cli-4.4.0.2170-linux.zip  
 #ローカルで
 sudo scp -i key.pem build-wrapper-linux-x86.zip    ec2-user@1.1.1.1:/home/ec2-user/ 
 #ec2で
 unzip build-wrapper-linux-x86.zip        

3.その他、ツールのインストール

$ sudo yum install git-all
$ sudo yum -y install gcc-c++

4.解析用のリポジトリをごっそりクローン

git clone https://github.com/SonarSource/sonar-scanning-examples.git
#C言語のsrcが入った対象フォルダに移動
cd sonar-scanning-examples/sonarqube-scanner-build-wrapper-linux/

実行

build-wrapperとsonar-scannerのパスを通しておく

$ export PATH=$PATH:/home/ec2-user/build-wrapper-linux-x86/
$ export PATH=$PATH:/home/ec2-user/sonar-scanner-4.4.0.2170-linux/

ビルドする

#sonar-scanning-examples/sonarqube-scanner-build-wrapper-linux/配下
build-wrapper-linux-x86-64 --out-dir bw-output ./build.sh

sonnar-scanの実行(fig.5からコピペ)

sonar-scanner \
  -Dsonar.organization=xx \
  -Dsonar.projectKey=xx \
  -Dsonar.sources=. \
  -Dsonar.cfamily.build-wrapper-output=bw-output \
  -Dsonar.host.url=https://sonarcloud.io

成功すると以下のようにログが出力される
一行目に表示されるURLにアクセスすると後述のダッシュボードが表示される

INFO: ANALYSIS SUCCESSFUL, you can find the results at: https://sonarcloud.io/dashboard?id=terub10_sonar
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at https://sonarcloud.io/api/ce/task?id=AXWnuSLZoI_e1FQSmOT6
INFO: Analysis total time: 16.406 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 20.554s
INFO: Final Memory: 11M/105M
INFO: ------------------------------------------------------------------------

▼fig.8
image.png

脆弱性がある場合は、
脆弱性の種類/対処策、CERTのリンクなどを教えてくれる。
▼fig.9
image.png

参考リンク

あとがき

今回のコードでは、Security Hotspotsは検出されなかったが、
脆弱性のある任意のコードに対して、build.shを書いて、
SonarCloudに挙げられれば良いのではないのかと、現在試行中。。


  1. 仕事でもcode smellを炙り出し、リファクタリングの基になる材料として活用しているが、セキュリティの機能があるのが少し気になっていたので、良い機会で調査した 

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
0