地址來源於烏雲知識庫,作者z_zz_zzz
0x01 任意文件下載
web.xml的配置:
<servlet>
<description></description>
<display-name>DownloadAction</display-name>
<servlet-name>DownloadAction</servlet-name>
<servlet-class>com.oboi.DownloadAction.DownloadAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownloadAction</servlet-name>
<url-pattern>/DownloadAction</url-pattern>
</servlet-mapping>
其中的servlet類要換下。類的代碼如下:
public class DownloadAction extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String rootPath = this.getServletContext().getRealPath("/");
String filename = request.getParameter("filename");
if (filename == null)
filename = "";
filename = filename.trim();
InputStream inStream = null;
byte[] b = new byte[1024];
int len = 0;
try {
if (filename == null) {
return;
}
// 讀到流中
// 本行代碼未對文件名參數進行過濾,存在任意文件下載漏洞
//如果有指定后綴名好像也不能截斷
inStream = new FileInputStream(rootPath + "/" + filename);
// 設置輸出的格式
response.reset();
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment; filename=\""
+ filename + "\"");
// 循環取出流中的數據
while ((len = inStream.read(b)) > 0) {
response.getOutputStream().write(b, 0, len);
}
response.getOutputStream().close();
inStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
JDK1.5-1.7存在0x00導致的文件名截斷問題,與操作系統無關。冒號在Windows環境會導致文件名截斷問題,與JAVA無關。
如果要修復這種漏洞的話,可以用文章中講的。
在生成File對象后,使用getCanonicalPath獲取當前文件的真實路徑,判斷文件是否在允許下載的目錄中,若發現文件不在允許下載的目錄中,則拒絕下載。
if (!pathname.getCanonicalPath().startsWith(rootPath)){
System.out.println("禁止目錄穿越下載");
}
這里記錄一下,如果是JAVA寫的網站遇到任意文件讀取,怎么擴大戰果?下載配置文件,有框架的話下載框架配置文件。比如:WEB-INF/web.xml
通過下載的配置文件去讀class文件,然后通過jad反編譯出代碼來。

讀到servlet-class 以后通過WEB-INF/classes/com/oboi/DownloadAction/DownloadAction.class 就能讀到類了。
0x02 惡意文件上傳
未完待續。。。
