軟體工程師的小事:到底要不要 Close

LDAP Injection 可參考解決方式

在 .NET Framework 查詢 Active Directory 環境物件資訊通常使用 System.DirectoryServices 這個命名空間,查詢時就會需要使用 LDAP 協定並指定查詢字串(有點類似查詢資料庫的感覺)

HP Fority 這個工具也會偵測程式碼中 LDAP 查詢字串是否有被注入攻擊的機會,若有就會產生 「LDAP Injection」在報表中。下面連結 LDAP Injection Prevention Cheat Sheet - OWASP 有此弱點的詳細說明。

程式碼說明

GitHub - txstudio/FortifyLDAPInjection

您可以取得此程式碼修改指定變數後確認 Fortify 工具是否會偵測出 LDAP Injection

範例程式碼為從指定網域路徑中取得指定帳號物件的主控台應用程式,取得程式碼之後請修改網域路徑參數。

directoryEntry root Path filter

LDAP 查詢語法由使用者輸入的參數組合,並設定到 DirectorySearcher 物件的 Filter 屬性。

set filter string to directorysearcher.filter property

Fortify 掃描後會偵測出「LDAP Injection」問題。

ldap injection detected by fority sca
解決方式

使用 Nuget 套件 AntiXSS 提供的 Encoder.LdapFilterEncode 方法將查詢字串進行編碼。

LdapFilterEncoder 位於 Microsoft.Security.Application 命名空間
use nuget manager download AntiXSS

使用 Encoder.LdapFilterEncode 方法對 _filter 變數進行編碼。

LdapFilterEncode filter variable

再次掃描之後「LDAP Injection」已經消失。

ldap injection disappear

下方參考資訊有 OWASP 關於 LDAP Injection 的相關資訊:弱點情境與在 JAVA 中如何防止此問題。

參考資料

留言