Wiki source code of Engine_XalanJ

Last modified by Nicolas Gregoire on 2012/01/31 17:35

Hide last authors
Nicolas Gregoire 69.1 1 {{toc/}}
2
3 = Introduction =
4
Nicolas Gregoire 1.1 5 [[Xalan-J>>http://xml.apache.org/xalan-j/||rel="__blank" title="Xalan-J Home Page"]] is a Java based XSLT engine by the Apache Project.
6
Nicolas Gregoire 69.1 7 = Supported version =
Nicolas Gregoire 1.1 8
Nicolas Gregoire 19.1 9 1.0
Nicolas Gregoire 1.1 10
Nicolas Gregoire 69.1 11 = Command line =
Nicolas Gregoire 17.1 12
Nicolas Gregoire 18.1 13 $> java org.apache.xalan.xslt.Process -in foo.xml -xsl foo.xsl
Nicolas Gregoire 17.1 14
Nicolas Gregoire 20.1 15 __Note__ : xml-apis.jar, xercesImpl.jar and xalan*.jar must be in the $CLASSPATH
16
Nicolas Gregoire 69.1 17 = Identification strings =
Nicolas Gregoire 1.1 18
Nicolas Gregoire 22.1 19 |=xsl:vendor-url|http:~/~/xml.apache.org/xalan-j
20 |=xsl:vendor|Apache Software Foundation
21 |=xsl:version|1.0
Nicolas Gregoire 1.1 22
Nicolas Gregoire 69.1 23 = Special features =
Nicolas Gregoire 1.1 24
Nicolas Gregoire 38.1 25 * Java properties disclosure
26 * Java environment disclosure
27 * Java code execution
Nicolas Gregoire 43.1 28 * OS command execution
Nicolas Gregoire 1.1 29 * File creation
30 * JDBC connectivity
Nicolas Gregoire 3.1 31
Nicolas Gregoire 69.1 32 = Java properties disclosure =
Nicolas Gregoire 35.1 33
Nicolas Gregoire 37.1 34 The xsl:system-property() standard function can be called with non standard arguments, mapped to Java properties. In this example, the name of the Java properties is stored in a separate XML file ([[properties.xml>>attach:properties.xml]]). The XSLT code will, for each property, display its name and its value.
Nicolas Gregoire 35.1 35
Nicolas Gregoire 37.1 36 |=Namespace|=Function|=PoC|=Sample output
37 |http:~/~/www.w3.org/1999/XSL/Transform|system-property()|[[xalanj-java-properties.xsl>>attach:xalanj-java-properties.xsl]]|[[xalanj-java-properties-output.txt>>attach:xalanj-java-properties-output.txt]]
Nicolas Gregoire 35.1 38
Nicolas Gregoire 69.1 39 = Java environment disclosure =
Nicolas Gregoire 9.1 40
Nicolas Gregoire 39.1 41 The checkEnvironment() extension function (documented [[here>>http://xml.apache.org/xalan-j/faq.html#faq-N10064||rel="__blank"]]) will display some information about the execution context (including available packages, paths, versions, ...).
Nicolas Gregoire 9.1 42
Nicolas Gregoire 32.1 43 |=Namespace|=Extension function|=PoC|=Sample output
Nicolas Gregoire 33.1 44 |http:~/~/xml.apache.org/xalan|checkEnvironment()|[[xalanj-checkenv.xsl>>attach:xalanj-checkenv.xsl]]|[[xalanj-checkenv-output.txt>>attach:xalanj-checkenv-output.txt]]
Nicolas Gregoire 31.1 45
Nicolas Gregoire 69.1 46 = Java code execution =
Nicolas Gregoire 3.1 47
Nicolas Gregoire 76.1 48 == Basic Java calls ==
49
Nicolas Gregoire 33.1 50 The attached code will display the current date using a newly created "java.util.Date" object. This should be enough to demonstrate Java code execution.
Nicolas Gregoire 3.1 51
Nicolas Gregoire 33.1 52 |=Namespace|=Extension function|=PoC|=Sample output
Nicolas Gregoire 34.1 53 |http:~/~/xml.apache.org/xalan/java/java.util.Date|new()|[[xalanj-java-date.xsl>>attach:xalanj-java-date.xsl]]|Current date:
54 Wed Jan 11 22:45:07 CET 2012
Nicolas Gregoire 33.1 55
Nicolas Gregoire 76.1 56 == Executing arbitrary classes ==
57
58 -- It is afaik not possible to get a pure Java reverse-shell, as we can't create threads :-( --
59
60 {{warning}}
61 TODO : javapayload => loading arbitrary byte code (aka classes) via reflection
62 $> java javapayload.builder.Builder Template XalanJ.xsl bind-jsh-4444.xsl BindTCP 127.0.0.1 4444 - - JSh
63 List supported payloads !
64 Check supported versions of Xalan !
65 {{/warning}}
66
Nicolas Gregoire 69.1 67 = OS command execution =
Nicolas Gregoire 6.1 68
Nicolas Gregoire 64.1 69 Once Java code execution is possible, it is trivial to execute arbitrary OS commands using the java.lang.Runtime class.
Nicolas Gregoire 46.1 70
Nicolas Gregoire 69.1 71 == Command without output ==
Nicolas Gregoire 64.1 72
73 The attached PoC will not read the output of the executed command (because loops are hard in XSLT). But this is not a problem if a reverse-shell have already been started, isn't it ;-)
74
Nicolas Gregoire 52.1 75 |=Namespace|=Extension functions|=PoC
Nicolas Gregoire 53.1 76 |http:~/~/xml.apache.org/xalan/java|split(), getRuntime(), exec() and toString()|[[xalanj-reverse-bash.xsl>>attach:xalanj-reverse-bash.xsl]]
Nicolas Gregoire 49.1 77
Nicolas Gregoire 53.1 78 __Note__ : as arrays are not a native type in XSLT, we create one in Java via split() before passing it as an argument to [[exec(String[] cmdarray)>>http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#exec(java.lang.String[])||rel="__blank"]].
Nicolas Gregoire 54.1 79
Nicolas Gregoire 69.1 80 == Reading stdout ==
Nicolas Gregoire 64.1 81
Nicolas Gregoire 68.1 82 As the output have an unknown number of lines, we must use a __loop__ construct like "while" ... which is not available in XSLT. This limitation is due to the functional programming paradigm but can be circumvented using templates and recursion. This way, we can also __update__ some variables, but the syntax is awful and error prone.
Nicolas Gregoire 64.1 83
Nicolas Gregoire 68.1 84 It's far more efficient to 1) write loops using non-standard elements like <loop:while> and <loop:update> 2) convert them in stylesheets using only templates and recursion. This conversion can be done with a tool like the [[XSLT Loop Compiler>>http://www2.informatik.hu-berlin.de/~~obecker/XSLT/loop-compiler/||rel="__blank"]] (which is itself in XSLT).
Nicolas Gregoire 64.1 85
Nicolas Gregoire 68.1 86 The following PoC will fetch some commands from a XML file, execute them (with bash or cmd.exe depending on the detected OS), read the standard output and display it. The file with a "lxsl" extension uses the non-standard <loop:*> elements and is far more readable than the "xsl" one.
87
Nicolas Gregoire 66.1 88 |=Using non standards elements|=Using recursion and templates|=Commands to execute|=Output
Nicolas Gregoire 68.1 89 |[[xalanj-reading-stdout.lxsl>>attach:xalanj-reading-stdout.lxsl]]|[[xalanj-reading-stdout.xsl>>attach:xalanj-reading-stdout.xsl]]|[[unix_commands.xml>>attach:unix_commands.xml]]|[[xalanj-reading-stdout.txt>>attach:xalanj-reading-stdout.txt]]
Nicolas Gregoire 66.1 90
Nicolas Gregoire 68.1 91 It is of course possible to include commands for multiples OS in one file and to execute only the relevant ones.
Nicolas Gregoire 66.1 92
Nicolas Gregoire 69.1 93 = File creation =
Nicolas Gregoire 54.1 94
Nicolas Gregoire 57.2 95 The "write" extension element allows to create files on the engine side. The content written to the file must be valid UTF-8 (so plain ASCII works too). Existing files can be overwritten.
Nicolas Gregoire 54.1 96
Nicolas Gregoire 55.1 97 |=Namespace|=Extension element|=Parameter|=PoC
Nicolas Gregoire 57.1 98 |http:~/~/xml.apache.org/xalan/redirect|write|file|[[xalanj-write.xsl>>attach:xalanj-write.xsl]]
Nicolas Gregoire 55.1 99
Nicolas Gregoire 69.1 100 = JDBC connectivity =
Nicolas Gregoire 54.1 101
Nicolas Gregoire 63.1 102 It is possible to use XSLT to connect to any database having a corresponding installed JDBC driver.
Nicolas Gregoire 59.1 103
Nicolas Gregoire 69.1 104 == Simple connection ==
Nicolas Gregoire 63.1 105
106 The [[xalanj-jdbc-query.xsl>>attach:xalanj-jdbc-query.xsl]] PoC simply connects to a local MySQL database using some hard-coded credentials, executes a query and displays the result.
107
Nicolas Gregoire 59.1 108 |=Namespace|=Extension function|=PoC
109 |org.apache.xalan.lib.sql.XConnection|new(), query() and close()|[[xalanj-jdbc-query.xsl>>attach:xalanj-jdbc-query.xsl]]
110
Nicolas Gregoire 69.1 111 == Credentials brute-forcing ==
Nicolas Gregoire 60.1 112
113 The [[xalanj-jdbc-bruteforce.xsl>>attach:xalanj-jdbc-bruteforce.xsl]] file will read some tuples (JDBC driver, database URL, username, passsword) from a XML file ([[xalanj-jdbc-bruteforce.xml>>attach:xalanj-jdbc-bruteforce.xml]]) and try to login with each one, effectively brute-forcing credentials from the engine side (usually on the backend ;-).
114
Nicolas Gregoire 61.1 115
Nicolas Gregoire 60.1 116 Here's the output when launched from the CLI :
Nicolas Gregoire 61.1 117
118 ##$> java org.apache.xalan.xslt.Process -in xalanj-jdbc-bruteforce.xml -xsl xalanj-jdbc-bruteforce.xsl 2> /dev/null
Nicolas Gregoire 60.1 119 Username : [root] / Password : [] :
120 Username : [root] / Password : [uberpasswd] :
121 Username : [root] / Password : [cnam] : OK !!
Nicolas Gregoire 61.1 122 Username : [pma] / Password : [pma] : ##
Nicolas Gregoire 77.1 123
124 = Anti XEE =
125
126 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
Nicolas Gregoire 78.1 127 builderFactory.setExpandEntityReferences(false); <<<<==[Here]==<<<<
Nicolas Gregoire 77.1 128 DocumentBuilder builder = builderFactory.newDocumentBuilder();
Nicolas Gregoire 78.1 129 DOMSource xmlSource = new DOMSource(builder.parse(new ByteArrayInputStream(myXmlString.getBytes(~)~)~));
Nicolas Gregoire 79.1 130
Nicolas Gregoire 80.1 131
Nicolas Gregoire 79.1 132 By default (cf. Xalan-j [[documentation>>http://xml.apache.org/xalan-j/apidocs/javax/xml/parsers/DocumentBuilderFactory.html#setExpandEntityReferences(boolean)]]), this value is set to True.