發表文章

目前顯示的是有「Coverity」標籤的文章

經得起原始碼資安弱點掃描的程式設計習慣培養(十九)_Schema allows arbitrary nodes (SIGMA.element_any_core_xsd)

Schema allows arbitrary nodes (SIGMA.element_any_core_xsd) Sigma main event: Arbitrary nodes are allowed in a valid document which makes it  easier for attackers to perform attacks like XML injection. Ref: <xsd:any> Element https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256043(v=vs.100) XXE的基本概念和利用方式 https://www.freebuf.com/articles/web/265324.html Structure of a content XSD document https://documentation.opencms.org/opencms-documentation/template-development/content-type-definition-with-xml-schema/structure-of-a-content-xsd-document/ 为什么要使用 XML Schema? https://www.w3school.com.cn/schema/schema_why.asp https://www.w3school.com.cn/schema/schema_complex_any.asp Allowing Any Content docstore.mik.ua/orelly/xml/xmlnut/ch16_08.htm https://docstore.mik.ua/orelly/xml/xmlnut/ch16_08.htm xsd:any:幕后故事 https://searchcloudcomputing.techtarget.com.cn/5-17839/ XML学习笔记5——XSD复杂数据类型 https://www.cnblogs.com/linjisong/p/3305228.html

經得起原始碼資安弱點掃描的程式設計習慣培養(十七)_OPEN_REDIRECT

pen redirect (OPEN_REDIRECT) sink: Calling Redirect. This call passes the tainted data, ....... (The virtual call resolves to System.Web.Mvc.Controller.Redirect(System.String).) 開放式重定向(釣魚攻擊),頁面跳轉過程並未驗證重定向目的網址是否安全。 Before 1 2 3 4 5 6 public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { [perform auth logic] return this .Redirect(returnUrl); } After 1 2 3 4 5 6 7 8 9 10 11 12 13 public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { [perform auth logic] if (Url.IsLocalUrl(returnUrl)) { return this .Redirect(returnUrl); } else { return RedirectToAction ( "Index" , "Home" ); } }一 一般來說除了驗證是否為本機路徑(非絕對路徑),以確保攻擊者無法將使用者重新導向到惡意外部網域,也能用以下確認是相對路徑(不是絕對路徑)方式來加強驗證 After2. 1 2 3 4 5 6 7 8 9 10 Uri targetUri = null ; if (Uri.TryCreate(Request.QueryString[ "ReturnUrl" ], UriKind.Relative, out targetUri)) { Response.Redir...

經得起原始碼資安弱點掃描的程式設計習慣培養(十六)_RISKY_CRYPTO_Risky cryptographic function(WEAK RSA KEY LENGTH)

 Using a key size less than 2048 bits with an asymmetric algorithm. An attacker with sufficient hardware can break this encryption by exhaustive key search CWE-326: Inadequate Encryption Strength https://cwe.mitre.org/data/definitions/326.html 由於密碼分析攻擊和雲端運算能力的進步,美國國家標準技術研究院 (NIST) 於 2011 年 1 月 1 日棄用了 1024 位元 RSA 金鑰。 憑證授權單位瀏覽器論壇以及所有瀏覽器的最新版本目前規定所有 RSA 金鑰的最小金鑰大小為 2048 位元。 (不安全程式碼)以下範例顯示RSACryptoServiceProvider 默認初始化若建構子捨麼都不指定就是 1024 位元長度的金鑰對。 RSACryptoServiceProvider alg = new RSACryptoServiceProvider(); same as RSACryptoServiceProvider alg = new RSACryptoServiceProvider(1024); (安全代碼)使用至少 2048 位元金鑰對配置RSACryptoServiceProvider 。 RSACryptoServiceProvider alg = new RSACryptoServiceProvider(2048); SEC0125 - WEAK RSA KEY LENGTH https://pumascan.com/rules/

經得起原始碼資安弱點掃描的程式設計習慣培養(十五)_HARDCODED_CREDENTIALS(Use of hard-coded password)

在程式碼中直接使用hard code密碼。 存在以下風險: 安全風險:硬編碼的密碼容易被攻擊者發現和濫用。如果攻擊者能夠獲取程式碼,他們就可以輕易地找到密碼,並使用它來進行未授權的存取。 維護風險:當密碼需要更改時,必須修改程式碼並重新編譯應用程式。這樣的做法不僅不安全,還會增加維護成本和風險。 修正方法建議: 1.使用安全的密碼儲存方式:避免在程式碼中直接使用硬編碼的密碼。相反,您可以使用安全的密碼儲存方式,例如使用加密的配置文件、密碼管理工具或密碼保險箱。 2.使用配置文件或環境變數:將密碼存儲在配置文件或環境變數中,而不是直接在程式碼中硬編碼。這樣可以使密碼與程式碼分離,並且可以輕鬆地更改密碼而不需要修改程式碼。 3.使用身份驗證和授權機制:使用適當的身份驗證和授權機制,例如使用 OAuth、JWT 或其他安全標準來保護應用程式的存取權限。 4.密碼管理最佳實踐:遵循密碼管理的最佳實踐,例如使用強密碼、定期更換密碼、限制密碼存取權限等。 在此建議用第2種方式配置到web.config或app.config 來重構程式

經得起原始碼資安弱點掃描的程式設計習慣培養(十四)_MISSING_THROW

https://www.reddit.com/r/ProgrammerHumor/comments/9msvuu/catchexception_e_silently_fail/ 最近在修正asp.net mvc系統的中風險弱點 剛好遇到之前沒看過的新弱點 有些名稱不同但原理修正方式跟之前在工研院用的checkmarx差不多 這邊記錄一下Coverity的弱點修補 Exception not thrown 在程式碼中創建了一個例外 (Exception) 物件,但沒有將其拋出 (throw)。 可能導致以下問題: 錯誤處理不完整:如果沒有將例外拋出,程式碼將無法正確處理錯誤情況。這可能導致程式繼續執行,而不是進行適當的錯誤處理。 錯誤訊息無法捕獲:如果例外沒有被拋出,其他程式碼也無法捕獲和處理該例外。這可能導致錯誤訊息無法被正確地記錄或顯示給使用者。 只要把程式段落中有new Exception("....") 多補上throw 在前面即可通關

Coverity Security Library (.NET Version)

  https://github.com/coverity/coverity-security-library-.net

Coverity程式碼弱點掃描修正_常使用指令集cov-build跟cov-analyze

圖片
Coverity 2022.6.0 Command and Ant Task Reference cov-build Intercept all calls to the compiler invoked by the build system and capture source code from the file system. cov-build    (--dir <intermediate_directory> | --da-broker <broker_servername:port>)    [--capture-ignore <program.extension>]    [--fs-capture-list <file>]    [--fs-capture-search <directory>]    [--test-capture]    [ OPTIONS ]    BUILD_COMMAND | --no-command The cov-build command is the primary tool to capture and emit source code. It performs build capture, where source code is emitted by intercepting all calls to the compiler invoked by the build system. It also performs filesystem capture, where source code is emitted directly from the file system. (For more information about the build capture processes, see the section "Coverity Analyses" in the overview to the Coverity Analysis 2022.6.0 User and Administrator Guide....

Coverity程式碼弱點掃描修正_jQuery Dom XSS修正

圖片
  jquery一些DOM操作容易引發DOM 的XSS風險 xss_sink: Calling echo with the tainted value in any argument. The untrusted data reaches a sink that may allow an attacker to control part of the response 幾項備用方案 解法1. https://github.com/chrisisbeef/jquery-encoder/tree/master/site 解法2. https://github.com/cure53/DOMPurify 解法3. https://github.com/leizongmin/js-xss 目前測試採用第一種方式就可以問題排除 JavaScript-Escaping一些library將其引用近來做相應api包覆使用即可 參考Link: https://www.edgescan.com/wp-content/uploads/2018/08/04.-XSS-and-Encoding-edgescan.pdf https://research.securitum.com/mutation-xss-via-mathml-mutation-dompurify-2-0-17-bypass/ https://blog.techbridge.cc/2021/05/15/prevent-xss-is-not-that-easy/ https://jsxss.com/zh/starter/quickstart.html

Coverity程式碼弱點掃描修正_PHP_XSS

圖片
https://www.hitachi-solutions.co.jp/coverity/  Coverity是一套用於靜態程式碼白箱弱點掃描的軟體 https://scan.coverity.com/ PHP當中若遇到類似要將url給render出來的一些function 通常只需要在外包覆 urlencode(htmlspecialchars( 回傳URL的程式段落可能是函數 )) 若是涉及url連結開啟的href,src或者js/jquery dom 操作一律用 urlencode(htmlspecialchars(URL回傳內容))來包覆 htmlspecialchars 將 HTML 符號變成不可執行的符號 大於(>)小於(<)符號、單引號(‘)或雙引號(“”)及&字元 urlencode 針對URL相關資料或參數進行編碼 例如: <?php echo urlencode(htmlspecialchars(會回傳URL函數())); ?> 反之其他輸出情況 若不允許html有效可直接用來防止非預期的輸出或惡意腳本渲染 <?php echo htmlspecialchars(某PHP函數(),ENT_QUOTES,’UTF-8’);?> (備註:預設htmlspecialchars($string)只轉化雙引號,不對單引號做轉義。