??現(xiàn)在越來越多的企業(yè)要做SDL方案赎败,安全編碼是其中的一環(huán)荞彼,而安全編碼,很多時候是借助掃描工具來發(fā)現(xiàn)漏洞,企業(yè)常用的代碼掃描工具有Fortify丽柿、Checkmarx等恢准,我經(jīng)歷的公司大多都是用Fortify;也經(jīng)常需要審計Fortify的掃描結(jié)果甫题,并幫助開發(fā)修復(fù)馁筐,為此經(jīng)常給開發(fā)培訓(xùn),并整理常見的漏洞修復(fù)列表坠非。
??這里會涉及到一個很重要的問題敏沉,就是如何判斷漏洞是否誤報?
其實是看輸入是不是外部輸入或者用戶的輸入,當(dāng)掃描出來漏洞時炎码,如果不是外部輸入或者用戶輸入盟迟,那么很多時候是沒有問題的,但大多數(shù)掃描出來的漏洞都是很容易修復(fù)的,所以根據(jù)縱深防御原則潦闲,有必要都修復(fù)攒菠,修復(fù)總的原則是,外部輸入不可信歉闰,盡量減少外部輸入辖众,服務(wù)器端要校驗。
下面我分享一下Fortify掃常見的漏洞及修復(fù)方式:
1. SQL注入(SQL Injection)
修復(fù)方法:
(1)如果是使用mybaits的框架和敬,使用#符號替代,應(yīng)為$符號是直接拼接數(shù)據(jù)庫語句昼弟;
(2)如果沒有使用框架啤它,直接查詢的話,可以使用Java預(yù)編譯的方法PreparedStatement修復(fù);
// This should REALLY be validated too
String custname = request.getParameter("customerName");
// Perform input validation to detect attacks
String query = "SELECT account_balance FROM user_data WHERE user_name = ? ";
PreparedStatement pstmt = connection.prepareStatement( query );
pstmt.setString( 1, custname);
ResultSet results = pstmt.executeQuery( );
2. XXE(XML External Entity Injection\XML Injection
)
修復(fù)方法:
(1)上傳XML文件和office文檔的地方舱痘,解析器禁用外部實體蚕键,
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
// This may not be strictly required as DTDs shouldn't be allowed at all, per previous line.
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
3. Spring-boot-actuator 配置安全( Spring Boot Misconfiguration: Shutdown Actuator Endpoint Enabled\System Information Leak: Spring Boot Actuators Enabled)
修復(fù)方法:
(1)關(guān)閉開啟的斷點
endpoints.enabled = false
// 如果要開啟一個端點,可以先關(guān)閉所有再開啟
endpoints.metrics.enabled = true
(2)開啟安全認證衰粹,引入spring-boot-starter-security依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
在application.properties中指定actuator的端口以及開啟security功能锣光,配置訪問權(quán)限驗證,這時再訪問actuator功能時就會彈出登錄窗口铝耻,需要輸入賬號密碼驗證后才允許訪問誊爹。
management.port=8099
management.security.enabled=true
security.user.name=admin
security.user.password=admin
4. XSS漏洞(Cross-Site Scripting: Reflected\ Cross-Site Scripting: Persistent)
修復(fù)方法:
(1)通過OWASP的ESAPI防XSS組件,輸出編碼瓢捉,先通過Maven引入JAR包频丘;
<dependency>
<groupId>org.owasp.encoder</groupId>
<artifactId>encoder</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.owasp.encoder</groupId>
<artifactId>encoder-jsp</artifactId>
<version>1.2.2</version>
</dependency>
然后使用時,輸出編碼
PrintWriter out = ....;
out.println("<textarea>"+Encode.forHtml(userData)+"</textarea>");
(2) 根據(jù)業(yè)務(wù)場景泡态,校驗輸入的數(shù)據(jù)類型和字符長度;
5. 未授權(quán)訪問的MongoDB(Unauthenticated Service: MongoDB)
修復(fù)方法:
(1)不要把MongoDB服務(wù)器部署在互聯(lián)網(wǎng)上或者DMZ搂漠,開啟MongoDB的授權(quán)訪問;
編輯 /etc/mongo.conf 文件某弦,找到 #auth=true , 去掉注釋.
創(chuàng)建用戶管理員
另外再連接MongoDB的時候
public class MongoDBJDBC {
public static void main(String[] args){
try {
//連接到MongoDB服務(wù) 如果是遠程連接可以替換“l(fā)ocalhost”為服務(wù)器所在IP地址
//ServerAddress()兩個參數(shù)分別為 服務(wù)器地址 和 端口
ServerAddress serverAddress = new ServerAddress("localhost",27017);
List<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
//MongoCredential.createScramSha1Credential()三個參數(shù)分別為 用戶名 數(shù)據(jù)庫名稱 密碼
MongoCredential credential = MongoCredential.createScramSha1Credential("username", "databaseName", "password".toCharArray());
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
//通過連接認證獲取MongoDB連接
MongoClient mongoClient = new MongoClient(addrs,credentials);
//連接到數(shù)據(jù)庫
MongoDatabase mongoDatabase = mongoClient.getDatabase("databaseName");
System.out.println("Connect to database successfully");
} catch (Exception e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
6. 硬編碼(Password Management: Hardcoded Password)
修復(fù)方法:
(1) 不允許把用戶的密碼硬編碼在代碼中桐汤,可以加密放在配置文件中而克,最好的方法是存入密碼托管服務(wù)
7. 密鑰長度不夠(Weak Encryption: Inadequate RSA Padding)
修復(fù)方法:
(1) 目前RSA的密鑰長度要求至少2048位,隨著算力的增長怔毛,將來可能要求更長;
8. AES的ECB模式不安全员萍,不能隱藏加密模式( Weak Encryption: Insecure Mode of Operation)
修復(fù)方法:
(1)禁用ECB,使用CBC拣度,或者最新的加密算法GCM碎绎;
9. 不安全的密碼算法(Weak Encryption)
修復(fù)方法:
(1)使用AES-CBC模式等,禁用DES,3DES抗果;
10. 目錄遍歷漏洞(Path Manipulation: Absolute Path Traversal)
修復(fù)方法:
(1)禁止把絕對路徑傳入到前端筋帖,使用文件id的方法
(2)過濾../及其編碼
參考
:
(1) https://xz.aliyun.com/t/2233
(2) https://vulncat.fortify.com/en/weakness?q=spring%20boot
(3) https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html