chrome内核的浏览器和火狐浏览器均可通过pdf内置javaScript脚本进行xss攻击,这是由于pdf本身支持携带js脚本,且浏览器解析时自动解析js脚本所致,

处理方法:

方法1:还可以加上在线预览改为强制下载。

方法2:禁止上传带有js脚本的pdf文件(本文采用)

 

导入jar包:

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.26</version>
        </dependency>

 

检测代码:


/** * 校验pdf文件是否包含js脚本 **/ public static boolean containsJavaScript(File file) throws IOException { PDDocument document = PDDocument.load(file); return containsJavaScript(document); } /** * 校验pdf文件是否包含js脚本 **/ public static boolean containsJavaScript(InputStream input) throws IOException { PDDocument document = PDDocument.load(input); return containsJavaScript(document); } /** * 校验pdf文件是否包含js脚本,这里使用分页解析,因为对于大pdf文件(文字较多),会出现加载问题。 **/
public static boolean containsJavaScript(PDDocument document) {
//Getting the PDDocumentInformation object

PDPageTree pages = document.getPages();
return IntStream.range(0, pages.getCount()).anyMatch(i -> {
//return str.contains("JavaScript") || str.contains("COSName{JS}");
return pages.get(i).getCOSObject().toString().contains("COSName{JS}");
});
}
 
public static void main(String args[]) throws IOException {
//Loading an existing document
String s1 = "xss注入测试.pdf";
File file = new File("C:\\Users\\Administrator\\Desktop\\" + s1);
boolean b = containsJavaScript(file);
if (b) {
System.out.println("pdf文件包含,js脚本代码");
}
}


 

执行结果:

 

posted on 2022-09-23 16:24  花开浪漫拾  阅读(5577)  评论(0编辑  收藏  举报