chrome内核的浏览器和火狐浏览器均可通过pdf内置javaScript脚本进行xss攻击,这是由于pdf本身支持携带js脚本,且浏览器解析时自动解析js脚本所致,
处理方法:
方法1:还可以加上在线预览改为强制下载。
方法2:禁止上传带有js脚本的pdf文件(本文采用)
导入jar包:
1 2 3 4 5 |
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.26</version> </dependency> |
检测代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
/** * 校验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脚本代码"); } } |
执行结果:
from:https://www.cnblogs.com/wulm/p/16723177.html