Java background Linux soft link file download test, javalinux
Recently, the test buddy of the project raised a linux system soft link attack. When exporting a file on the server in the project, the soft connection vulnerability can be used to obtain information about other files.
In the specific process, I wrote a download Demo to simulate it:
The downloaded servlet and html are as follows: Download/opt/temp/a.txt, international convention, the content is hello world, the file size is 12kb.
<!DOCTYPE html>View Code
package com.dj.servlet;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.net.URLEncoder;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.tomcat.util.http.fileupload.IOUtils;/** * @author DU * */@WebServlet("/download.do")public class DownloadDemoServlet extends HttpServlet{ private static final long serialVersionUID = 1L; public DownloadDemoServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String fileName = "a.txt"; response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); String filePath = "/opt/temp/" + fileName; FileInputStream is = new FileInputStream(new File(filePath)); ServletOutputStream os = response.getOutputStream(); IOUtils.copy(is, os); os.close(); is.close(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}View Code
In this way, there is no problem with the downloaded file, and the soft link will be involved. There is a npp.7.3.3.installer.x64.exe file under the server/homedirectory. Of course, this is a notepad ++ Installation File, I put it myself. Delete/opt/temp/a.txt first, and run the command ln-s/home/npp.7.3.3.Installer.x64.exe/opt/temp/a.txt to create the. exefile a.txt. The soft link file is not detailed here, baidu can be understood as a shortcut similar to windows. After the creation is successful, note that the file size is already 2.84M.
Click Download again. the download is as follows:
Yes, downloading a soft link will directly download the source file pointed to by the soft link. If it is a sensitive file, it is not good. Of course, I am still in the cainiao stage. It is not clear how attackers can create this soft link.
The next step is how to avoid this problem:
The first method is to compare the absolute path and normalized path of the file. If the source file is used, the two paths should be the same; otherwise, the files will be linked.
// Compare the absolute path with the normalized path String absolutePath = file. getAbsolutePath (); String canonicalPath = file. getCanonicalPath (); if (! AbsolutePath. equals (canonicalPath) {response. getWriter (). append ("Download Failed! "); Return ;}View Code
The second method can be determined directly through the interface provided by the java. nio. file. Files class that appears at the beginning of jdk1.7.
// Directly use java. nio. the interface provided in the file package determines Path path = Paths. get (filePath); boolean isSymbolicLink = Files. isSymbolicLink (path); if (isSymbolicLink) {response. getWriter (). append ("Download Failed! "); Return ;}View Code
, Verification Successful!