【資訊安全】SonarQube 靜態程式碼掃描安裝 CentOS 7 + Windows

SonarQube 靜態程式碼掃描安裝 (俗稱白箱測試) CentOS 7
基礎會使用到的資料庫(主程式需使用 ) 以及Git(下載目標掃描Source Code ) 的安裝就不多做說明

要使用SonarQube 掃描原始碼,主要需要安裝兩個模組:SonarQube + SonarScanner。
其中SonarQube 負責用來查看、管理源碼掃描的結果;SonarScanner 則負責執行掃描動作。
筆者將前者安裝在CentOS 上;後者因為專案使用C# .NET Framework,故安裝在Windows 上。

SonarQube 安裝

筆者先準備環境 https://ywnz.com/linuxyffq/8768.html

// 安裝相依性的套件OpenJDK 11
yum update 							
yum install unzip

yum install java-11-openjdk-devel	// 安裝 OpenJDK
java -version						// 測試 java
which java

// 下載程式壓縮檔
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.9.7.52159.zip

// 解壓縮等檔案處裡
mv sonarqube-8.9.7.52159.zip /opt/sonarqube-8.9.7.52159.zip	
cd /opt
upzip sonarqube-8.9.7.52159.zip		// 解壓縮
mv sonarqube-8.9.7.52159 sonarqube	// 修改資料夾名稱
chown -R sonar:sonar /opt/sonarqube	// 修改資料夾權限給sonar 使用者

// 依照教學修改設定檔

// 開啟防火牆port 9000 (SonarQube 預設使用的Port )
firewall-cmd --zone=public --add-port=9000/tcp --permanent	// add
firewall-cmd --reload										// reload
firewall-cmd --list-all										// list all

// 啟動SonarQube
systemctl start sonarqube

輸入指令之後發現服務沒有啟動,前往/logs 資料夾中檢查,發現有以下錯誤:

"max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]"

這是因為 預設的虛擬記憶體用量為65530 需要加大

vi /etc/sysctl.conf 			

// 追加以下内容
vm.max_map_count=655360

sysctl -p
>> vm.max_map_count = 655360 	// 最後一行看到這個就對了

// 啟動SonarQube
systemctl start sonarqube

// 參考
// https://www.elastic.co/guide/en/elasticsearch/reference/5.5/vm-max-map-count.html
// https://medium.com/@d101201007/elk%E6%95%99%E5%AD%B8-%E8%A7%A3%E6%B1%BA-max-virtual-memory-areas-vm-max-map-count-1b48fc85da48

至此,服務成功啟動。到瀏覽器輸入 http:<ip>:9000 可以看到登入畫面


SonarScanner 安裝

因為專案是用C# .NET Framework 開發,掃描前需要用MSBuild 編譯。

// 範例使用:SonarScanner 5.5.3
// https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/5.5.3.43281/sonar-scanner-msbuild-5.5.3.43281-net46.zip
// 參考:https://www.itread01.com/content/1570708863.html

// 下載並解壓縮至自訂目錄,例如我放在:C:\sonar-scanner-msbuild-5.5.3.43281-net46,再將此安裝路徑新增到環境變數的PATH 中。
// 打開 <安裝路徑>/SonarQube.Analysis.xml,取消註解SonarQubeAnalysisProperties 區域並設定指向SonarQube 的資料。
// 範例設定內容:
<SonarQubeAnalysisProperties>
  <Property Name="sonar.host.url">http://192.168.0.xxx:9000</Property>
  <Property Name="sonar.login">admin</Property>
  <Property Name="sonar.password">password</Property>
</SonarQubeAnalysisProperties>

// 將MSBuild 路徑也新增到環境變數的PATH 中
// 路徑可能是在 C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin
// 依照個人實際安裝位置為準

// 接下來就可以依序使用下面的指令進行掃描

// 0.開啟Cmd,並移動到要掃描的專案目錄最上層(有.sln 檔)
// 1. SonarScanner 掃描預先程序
SonarScanner.MSBuild.exe begin /k:"TestPorject2" /d:sonar.host.url="http://192.168.0.xxx:9000" /d:sonar.login="admin" /d:sonar.password="password"
// 2. 用MSBuild 指定sln 檔並進行專案重建
MSBuild TestPorject2.sln /t:Rebuild
// 3. 進行掃描
SonarScanner.MSBuild.exe end /d:sonar.login="admin" /d:sonar.password="password"

如果沒意外,成功執行之後,就可以回到Web 上查看掃描結果

如果需要產出報表,可以在Martketpalce 裡面找到CNSE 報表plugin

References:
// 安裝主程式
https://ywnz.com/linuxyffq/8768.html
https://www.elastic.co/guide/en/elasticsearch/reference/5.5/vm-max-map-count.html
https://medium.com/@d101201007/elk%E6%95%99%E5%AD%B8-%E8%A7%A3%E6%B1%BA-max-virtual-memory-areas-vm-max-map-count-1b48fc85da48

// 掃描工具 & 掃描
https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-msbuild/
https://community.sonarsource.com/t/error-when-trying-to-run-sonarscanner-for-my-netcore-project/36365/2
https://dotblogs.com.tw/supershowwei/2016/10/30/164450
https://www.itread01.com/content/1570708863.html