2023年9月26日 星期二

以SonarQube掃描dotNet Core專案源碼(白箱掃描)

SonarQube是一套白箱掃描工具,本例使用開源免費的Community版本,有關安裝方式可參考「在KALI Linux建置黑、白箱掃描系統(Arachni及SonarQube)」

本文以GitHub上的dotNet Core範例「MagicianredLabs/dotnetcore5-example」做示範說明,若想掃描其他 dotNet Core專案,請依該專案環境、結構酌作調整,尤其工具及SDK的版本。

一、必要需求

(一)dotNet SDK

可下載安裝版或ZIP檔,本例選用7.0.201的Amd 64之ZIP版,直接解壓縮到自選目錄:C:\dotNet.core\dotnet-x64-7.0.201,並將此路徑加入 Path 環境變數。

(二)檢查「%USERPROFILE%\.dotnet\tools」目錄裡是否有「dotnet-sonarscanner.exe」檔案及「.store」目錄,如果沒有,請執行下列命令,以安裝 dotNet Core 用的sonarscanner 全域工具:

cd C:\dotNet.core\dotnet-x64-7.0.201
dotnet tool install --global dotnet-sonarscanner --version 5.13.1

備註:可到NUGet查看--version X.X.X可支援的版本

二、執行掃描

(一)啟動SonarQube Scanner Server,假設位於「 http://192.168.232.153:9000」

(二)以瀏覽器連線SonarQube Scanner Server,並建立掃描專案(如重掃描舊專案,請備妥 Project Token)

1. 由Projects頁面選擇「Create Project」->「Manually」

2.社群版只能選擇「Locally」

3.填入「Project display name」(專案名稱)及「Project key」(專案鍵名)

4.產生Project Token(專案身份代號)

5.請記下Project Token



6.選擇要被掃描的專案類型(.NET)

7.選擇.NET的型態(.NET Core)



(三)開啟命令提示字元,將工作目錄切換到待掃描的dotNet Core專案目錄,本例使用MagicianredLabs/dotnetcore5-example裡的it.example.dotnetcore5專案做示範,下載後置於D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5。

(四)如果已完成項一之(二)的要求,就不會理會上圖的第一條命令(dotnet tool install --global dotnet-sonarscanner)。
如已將dotNet SDK的目錄加到PATH環境變數,就逐一複製及執行SonarQube所提供的後三段命令。
若想靈活切換SDK版本,可以在執行掃描命令之前先執行「set PATH=SDKPath;%PATH%」設定路徑(SDKPath請換成dotNet SDK的真正路徑)

(五)執行過程如下所示(橘紅色部分是我所執行的命令)

C:\>set path=C:\dotNet-x64-7.0.201;%path%
C:\>d:
D:\>
cd d:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5
D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5>dotnet sonarscanner begin /k:".netFramework-Example.core" /d:sonar.host.url="http://192.168.232.153:9000"  /d:sonar.login="sqp_c855f5d97c43571f467c8757cd5bcd1e9bb8792a"
SonarScanner for MSBuild 5.13.1
Using the .NET Core version of the Scanner for MSBuild
Pre-processing started.
Preparing working directories...
<<部分訊息省略>>

D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5>dotnet build
MSBuild version 17.5.0-preview-23061-01+040e2a90e for .NET
  正在判斷要還原的專案...
<<部分訊息省略>>
  Sonar: (it.example.dotnetcore5.webapi.tests.integration.csproj) Project processed successfully


建置成功。


C:\dotNet-x64-7.0.201\sdk\7.0.201\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.EolTargetFrameworks.targets(28,5): warning NETSDK1138: 目標 Framework 'net5.0' 已不受支援,未來將不會再收到任何安全性更新。如需支援原則的詳細資訊,請參閱 https://aka.ms/dotnet-core-support。 [D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5\it.example.dotnetcore5.bl\it.example.dotnetcore5.bl.csproj]
<<部分訊息省略>>

D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5\it.example.dotnetcore5.webapi\Program.cs(12,18): warning S1118: Add a 'protected' constructor or the 'static' keyword to the class declaration. [D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5\it.example.dotnetcore5.webapi\it.example.dotnetcore5.webapi.csproj]
    39 個警告
    0 個錯誤

經過時間 00:01:51.00

D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5>dotnet sonarscanner end /d:sonar.login="sqp_c855f5d97c43571f467c8757cd5bcd1e9bb8792a"
SonarScanner for MSBuild 5.13.1
Using the .NET Core version of the Scanner for MSBuild
Post-processing started.
Calling the SonarScanner CLI...
<<部分訊息省略>>

INFO: More about the report processing at http://192.168.232.153:9000/api/ce/task?id=AYrMzgcpHOOPZyW4SWNL
INFO: Analysis total time: 14.247 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 17.470s
INFO: Final Memory: 17M/60M
INFO: ------------------------------------------------------------------------
The SonarScanner CLI has finished
19:34:12.82  Post-processing succeeded.

D:\sonar-scanning-examples-master\dotnetcore5-example-main\it.example.dotnetcore5>

如果前面各步驟無誤,應該可以完成掃描,最後會看到「EXECUTION SUCCESS」。如果過程出現錯誤,請仔細研讀訊息,善用Google或ChatGPT找出解答。

完成掃描後,可在SonarQube Scanner Web UI看到專案結果~~




   關於 dotNet Framework 專案的掃描,請參閱:以SonarQube執行.netFramework專案的源碼掃描(白箱掃描)

2023年9月24日 星期日

以SonarQube執行.netFramework專案的源碼掃描(白箱掃描)

SonarQube是一套白箱掃描工具,本例使用開源免費的Community版本,有關安裝方式可參考「在KALI Linux建置黑、白箱掃描系統(Arachni及SonarQube)」

本文以SonarQube提供的範例「sonar-scanning-examples-master\sonarqube-scanner-msbuild\CSharpProject」做示範說明,若想掃描其他.netFramework專案,請依該專案環境、結構酌作調整,尤其工具及SDK的版本。

一、必要需求

(一)SonarScanner for .NET(本例選用sonar-scanner-msbuild-5.11.0.60783-net46.zip)

(二)MSBuild V14以上(VS Community 2019以上,本例選用VS Community 2022)

(三)符合專案所需的.Net Framework(本例使用VS Community 2022自帶版本)

二、選用需求

將SonarScanner for .NET 執行檔和 MSBuild.EXE 的所在路徑加入 Path 環境變數,若不加入環境變數,執行命令時,必須手動指定完整路徑。

三、各項需求安裝

(一)安裝SonarScanner for .NET:

將所下載的 sonar-scanner-msbuild-5.11.0.60783-net46.zip 解壓縮到自選目錄,例如「C:\sonar-scanner-msbuild-5.11.0.60783-net46」

(二)安裝Visual Studio Community 2022:

1.下載 VisualStudioSetup.exe (VS Community 2022的安裝檔),並執行安裝。

2.安裝過程中,請加選「.NET Framework 4.6.2-4.7.1開發工具」和「.NET Framework 4.8.1開發工具」

四、檢測所需環境

(一)執行「C:\sonar-scanner-msbuild-5.11.0.60783-net46\SonarScanner.MSBuild.exe /?」會看到Sonar-scanner for .net的使用說明。

(二)執行「"C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\msbuild.exe" /version」會看到Msbuild.exe的版本。

五、執行掃描

(一)啟動SonarQube Scanner Server,假設位於「 http://192.168.232.153:9000」

(二)以瀏覽器連線SonarQube Scanner Server,並建立掃描專案(如重掃描舊專案,請備妥 Project Token)

1. 由Projects頁面選擇「Create Project」->「Manually」

2.社群版只能選擇「Locally」

3.填入「Project display name」(專案名稱)及「Project key」(專案鍵名)

4.產生Project Token(專案身份代號)

5.請記下Project Token

6.選擇要被掃描的專案類型(.NET)

7.選擇.NET的型態(.NET Framework)

(三)複製剛剛建立掃描專案時,SonarQube所提供的三段命令,利用文字編輯器酌作調整,並以「Sonar_net-framework-example.bat」檔名儲存在.netFramework方案目錄下(保有.sln方案檔的目錄,本例為C:\sonar-scanning-examples-master\sonarqube-scanner-msbuild\CSharpProject)

(四)以系統管理員身分開啟命令提示字元,將工作目錄切換到.netFramework方案目錄

(五)直接執行「Sonar_net-framework-example.bat」(執行過程如下)

C:\>cd C:\sonar-scanning-examples-master\sonarqube-scanner-msbuild\CSharpProject
C:\sonar-scanning-examples-master\sonarqube-scanner-msbuild\CSharpProject>Sonar_net-framework-example.bat
SonarScanner for MSBuild 5.11
Using the .NET Framework version of the Scanner for MSBuild
Pre-processing started.
Preparing working directories...
08:56:22.776  Updating build integration targets...
<<部分訊息省略>>

SonarWriteProjectData: 
  Sonar: (SomeConsoleApplicationTest.csproj) Project processed successfully
<<部分訊息省略>>

建置成功。 
"C:\sonar-scanning-examples-master\sonarqube-scanner-msbuild\CSharpProject\SomeConsoleApplication.sln" (Rebuild 目標) (1)
 ->
"C:\sonar-scanning-examples-master\sonarqube-scanner-msbuild\CSharpProject\SomeConsoleApplication\SomeConsoleApplication.csproj" (Rebuild 目標) (2) ->
(CoreCompile 目標) ->
  C:\sonar-scanning-examples-master\sonarqube-scanner-msbuild\CSharpProject\SomeConsoleApplication\Program.cs(4,18): warning S1118: Add a 'protected' constructor or the 'static' keyword to the class declaration. (https://rules.sonarsource.com/csharp/RSPEC-1118) [C:\sonar-scanning-examples-master\sonarqube-scanner-msbuild\CSharpProject\SomeConsoleApplication\SomeConsoleApplication.csproj]
  C:\sonar-scanning-examples-master\sonarqube-scanner-msbuild\CSharpProject\SomeConsoleApplication\Program.cs(21,28): w
arning S3400: Remove this method and declare a constant for this value. (https://rules.sonarsource.com/csharp/RSPEC-3400) [C:\sonar-scanning-examples-master\sonarqube-scanner-msbuild\CSharpProject\SomeConsoleApplication\SomeConsoleApplication.csproj]
  C:\sonar-scanning-examples-master\sonarqube-scanner-msbuild\CSharpProject\SomeConsoleApplication\Program.cs(9,17): warning S2583: Change this condition so that it does not always evaluate to 'true'; some subsequent code is never executed. (https://rules.sonarsource.com/csharp/RSPEC-2583) [C:\sonar-scanning-examples-master\sonarqube-scanner-msbuild\CSharpProject\SomeConsoleApplication\SomeConsoleApplication.csproj]



    3 個警告
    0 個錯誤
經過時間 00:00:20.71
SonarScanner for MSBuild 5.11
Using the .NET Framework version of the Scanner for MSBuild
Post-processing started.
Calling the TFS Processor executable...
The TFS Processor has finished
Calling the SonarScanner CLI...
<<部分訊息省略>>

INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 45.588s
INFO: Final Memory: 16M/57M
INFO: ------------------------------------------------------------------------
The SonarScanner CLI has finished
08:57:43.051  Post-processing succeeded.
C:\sonar-scanning-examples-master\sonarqube-scanner-msbuild\CSharpProject>


如果前面各步驟無誤,應該可以完成掃描,最後會看到「EXECUTION SUCCESS」。如果過程出現錯誤,請仔細研讀訊息,善用Google或ChatGPT找出解答。

完成掃描後,可在SonarQube Scanner Web UI看到專案結果~~


    關於 dotNet Core 專案的掃描,請參閱:以SonarQube掃描dotNet Core專案源碼(白箱掃描)

2023年6月25日 星期日

Web Server開啟PUT / DELETE方法,真的危險嗎?

前言

RFC7231是http方法的實作建議文件,在第4節提到:

HTTP was originally designed to be usable as an interface to distributed object systems. The request method was envisioned as applying semantics to a target resource in much the same way as invoking a defined method on an identified object would apply semantics.

(HTTP的原始設計目標是作為分散式物件系統的操作介面,將請求方法當成操作指定物件的方式)。

而RFC7231中共提供八種http方法:

方法 說明
GET 傳送指定的資源(含回應標頭及狀態碼)
HEAD 類似GET,但傳送回應標頭及狀態碼
POST 按請求載荷,處理指定的資源
PUT 以請求載荷取代目標資源的內容(若目標資源不存在,則新增之)
DELETE 移除目標資源的當前內容
CONNECT 就處理的資源,在client和server之間建立連線通道
OPTIONS 指出目標資源可用的通訊選項
TRACE 沿著到達目標資源的路徑,執行訊息環回測試。

對於不會改變資源狀態的方法,被視為「安全」,如GET、HEAD、OPTIONS和TRACE

緣由

收過幾次滲透測試報告,指出:網站開啟OPTIONS、PUT、DELETE等方法(method),會讓駭客上傳後門、木馬,或者刪除重要檔案,屬高風險漏洞,必須關閉。事實真是如此嗎?心中一直存疑。滲透測試人員是否做過驗證程序嗎?還是看到網路上的報導,就人云亦云?會有這樣的見解,或許是因為早期Internet的目標資源是「檔案」形式。但現今,目標資源已衍生出多種形態,原本的不安全論點是否還適用?

從RFC7231的說明:

Unlike distributed objects, the standardized request methods in HTTP are not resource-specific, since uniform interfaces provide for better visibility and reuse in network-based systems [REST].  Once defined, a standardized method ought to have the same semantics when applied to any resource, though each resource determines for itself whether those semantics are implemented or allowed.

(與分散式物件不同,標準的HTTP請求並未限定特定資源,在網路體系(REST)裡,介面的一致性可更好理解和重複使用,儘管各個資源可以自行決定是否實作這些方法的功能或同意執行這些方法,但有了標準化方法,操作任何資源時,就不致於誤解意思。)

上面這段話並「沒有規定PUT/DELETE只用來操作檔案」。

行動

為此,在Git Hub上找到Tomcat源碼,從Tomcat預設DefaultServlet.java發現處理PUT和DELETE方法的邏輯,確實可以上傳及刪除指定的檔案。既然PUT和DELETE的功能可由Tomcat的預設Servlet來定義,表示開發者亦可重新定義這些方法,進而改變PUT和DELETE的處理邏輯,那麼,PUT和DELETE是不是真的不安全?就必要經過實際驗證,因此錄製一段影片,藉由修改PUT和DELETE的程式邏輯,證明PUT和DELETE不像一般滲透測試人員所言那麼地危險!

影片在:https://youtu.be/x85wiKm3JSg