Java backend linux soft link file download test

Source: Internet
Author: User

Recent project testing Buddy has raised a problem with a Linux system soft link attack, and when you export a file on a server in a project, you can get information about other files through a soft-connect vulnerability.

The specific process has written a download of the demo simulated a bit:

Download the servlet and HTML as follows, download/opt/temp/a.txt, international practice, content is Hello World, file size 12kb.

  
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "UTF-8"><title>Download File</title><Scripttype= "Text/javascript">    functionDownload () {window.open ("download.do"); }</Script></Head><Body>    <inputtype= "button"value= "Download"onclick= "Download ()" /></Body></HTML>
View Code   
 PackageCom.dj.servlet;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.net.URLEncoder;Importjavax.servlet.ServletException;ImportJavax.servlet.ServletOutputStream;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importorg.apache.tomcat.util.http.fileupload.IOUtils;/** * @authorDU **/@WebServlet ("/download.do") Public classDownloaddemoservletextendshttpservlet{Private Static Final LongSerialversionuid = 1L;  PublicDownloaddemoservlet () {Super(); }  protected voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {String fileName= "A.txt"; Response.setheader ("Content-disposition", "attachment;filename=" + urlencoder.encode (filename, "UTF-8")); String FilePath= "/opt/temp/" +FileName; FileInputStream is=NewFileInputStream (NewFile (FilePath)); Servletoutputstream OS=Response.getoutputstream ();    Ioutils.copy (is, OS);    Os.close ();  Is.close (); }  protected voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response); }}
View Code

This download down the file without any problems, the next involves the problem of soft links, on the server/home directory under a Npp.7.3.3.installer.x64.exe file, of course, this is a notepad++ installation files, I put in. Remove the/opt/temp/a.txt first, then execute the command with Xshell ln-s/home/npp.7.3.3.installer.x64.exe/opt/temp/a.txt Create the. exe soft link file for a.txt, about the soft link file here is not fine table, can self-Baidu, can be understood as a shortcut similar to Windows, after the successful creation is such, note that this time the file size is 2.84M.

  

Then click on Download again, this time download down is this:

  

Yes, download the soft link will download the soft link to the source file, if it is sensitive file, it is very bad. Of course, the author is still in the rookie stage, it is not clear how malicious attackers can achieve by means of creating this soft link.

The next step is to avoid the problem:

The first way is to compare the absolute path of the file and the normalized path, if it is the source file, the two paths should be the same, or the link file.

  
    // Compare absolute paths with normalized paths    String Absolutepath = file.getabsolutepath ();     = File.getcanonicalpath ();     if (! absolutepath.equals (canonicalpath))    {      response.getwriter (). Append ("Download failed!" );       return ;    }
View Code

The second way can be judged directly by the interface provided by the Java.nio.file.Files class that jdk1.7 begins to appear.

  

  
    // directly judging by the interface provided in the Java.nio.file package    Path Path = Paths.get (filePath);     boolean issymboliclink = files.issymboliclink (path);     if (Issymboliclink)    {      response.getwriter (). Append ("Download failed!" );       return ;    }
View Code

, verify success!

Java backend linux soft link file download test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.