java 目录遍历漏洞_HttpClient使用之下载远程服务器中的文件(注意目录遍历漏洞)...

本文介绍了Java中的目录遍历漏洞,并提供了一个`DownloadServlet`的示例,展示了如何从远程服务器下载文件。同时,文章强调了目录遍历攻击的原理和危害,如利用'../'序列访问非Web目录的文件。为了防止此类漏洞,建议过滤请求中的特殊字符并限制可访问文件范围。最后,给出了`ClientA`客户端示例,用于演示使用Apache HttpClient执行HTTP GET请求。
摘要由CSDN通过智能技术生成

参考文献:

1.下载地址

Apache-》Projects-》HttpComponents

4f7adffd5fce2b0554e926fce05214ef.png

2.DownloadServlet

1 packagecom.servlet;2

3 importjava.io.BufferedInputStream;4 importjava.io.BufferedOutputStream;5 importjava.io.File;6 importjava.io.FileInputStream;7 importjava.io.IOException;8 importjava.io.InputStream;9 importjava.io.OutputStream;10 importjava.net.URLDecoder;11 importjava.net.URLEncoder;12

13 importjavax.servlet.ServletException;14 importjavax.servlet.http.HttpServlet;15 importjavax.servlet.http.HttpServletRequest;16 importjavax.servlet.http.HttpServletResponse;17

18

19

20 public class DownloadServlet extendsHttpServlet {21

22 private static final long serialVersionUID = 1L;23

24 public voiddoGet(HttpServletRequest request, HttpServletResponse response)25 throwsServletException, IOException {26 String filename = request.getParameter("id");27 String fileUrl = request.getServletContext().getRealPath("").replace("\\", "/");28 fileUrl = fileUrl + "/files/document/" +filename;29 System.out.println("fileUrl:"+fileUrl);30 String rname = new String(filename.getBytes("utf-8"));31 System.out.println("begin:"+rname);32 rname =URLEncoder.encode(rname);33 System.out.println("end:"+rname);34 response.addHeader("Content-Disposition", "attachment;filename="+rname);35 response.setContentType("application/octet-stream");36

37 File file = newFile(fileUrl);38 InputStream is = new BufferedInputStream(newFileInputStream(file));39 byte[] buffer = new byte[is.available()];40 is.read(buffer);41 is.close();42

43 OutputStream os = newBufferedOutputStream(response.getOutputStream());44 os.write(buffer);45 os.flush();46 os.close();47 }48

49

50 public voiddoPost(HttpServletRequest request, HttpServletResponse response)51 throwsServletException, IOException {52

53

54 }55

56

57 }58

59

3.ClientA.java

packagecom.tool;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importorg.apache.http.HttpResponse;importorg.apache.http.client.ClientProtocolException;importorg.apache.http.client.HttpClient;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.impl.client.DefaultHttpClient;public classClientA {/***

*@paramargs*/

public static voidmain(String[] args) {//TODO 自动生成的方法存根

ClientA client = newClientA();

client.service();

}public voidservice() {//TODO 自动生成的方法存根

String url= "http://此处填写ip或网址/download.do";

HttpClient client= newDefaultHttpClient();

HttpGet get= newHttpGet(url);try{

HttpResponse response=client.execute(get);}catch(ClientProtocolException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

4.注意服务器的编码方式和客户端的区别

统一为utf-8

5.注意目录遍历漏洞

目录遍历是通过操作URL强行访问web目录以外的文件,目录和命令,攻击者可以在目标机器的任何位置访问文件,执行命令。

最基本的目录遍历攻击技术是在URL中使用"../"序列,改变访问资源的路径,访问到web目录以外的文件。

例如:

http://example.com/../../../../some/file

http://example.com/..%255c..%255c/some/file

正常请求为:

http://example.com/test.cgi?look=intex.html

如果存在目录遍历漏洞,攻击者可以访问

http://example.com/test.cgi?look=test.cgi

解决办法:

过滤请求数据中"../"字符序列及其各种变形。

验证用户请求中提交的需要访问的文件是否在限定的范围内。

java web使用fliter过滤url即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值