事件
今天报一个文件包含,
正常url:http://xx.xx.xx.com/view=f01
payload : http://xx.xx.xx.com/view=../../../../../../../../etc/passwd%00
看了一下,问题代码如下。
private String getFile(String tagId) {
String path = getClass().getClassLoader().getResource("txt").getPath();
logger.info(String.format("获取文件的路径:%s", path));
String filePath = path + File.separatorChar + tagId + ".txt";
File file = new File(filePath);
String result = null;
try {
result = FileUtils.readFileToString(file);
cache.put(tagId, result);
} catch (IOException e) {
logger.error(String.format("读取文件异常,tagid=%s", tagId), e);
}
return result;
}
原因
开发者使用用户输入”tagId”来拼接路径,来读取一个txt文件。本来后缀已经限定好,但是攻击者用%00来截断,造成任意文件读取。
修改方案
1.使用白名单,不给用户来拼接路径。
2.在前端来实现加载txt文件。
3.数据库实现
审计
关注FileInputStream,readFileToString,readFileToByteArray