-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathExecute.java
More file actions
142 lines (121 loc) · 6.06 KB
/
Execute.java
File metadata and controls
142 lines (121 loc) · 6.06 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// The MIT License (MIT)
//
// Copyright (c) 2025 Robert C. Seacord
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package exec;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Objects;
import static java.lang.System.lineSeparator;
public class Execute {
private static void getOutput(Process proc) throws IOException, InterruptedException {
int result = proc.waitFor();
if (result != 0) {
System.out.println("process error: " + result);
}
InputStream in = (result == 0) ? proc.getInputStream() : proc.getErrorStream();
int c;
while ((c = in.read()) != -1) {
System.out.print((char) c);
}
} // end get Output
public static void main(String[] args) throws Exception {
Runtime rt = Runtime.getRuntime();
String nl = lineSeparator(); // retrieve line separator dependent on OS.
// Examine arguments
System.out.println("[rt.exec] PrintArgs Path with Spaces");
String PrintArgs = "C:\\Users\\RobertSeacord(Wovenb\\source\\repos\\PrintArgs\\x64\\Debug\\PrintArgs.exe ";
// String PrintArgs = "wsl /home/rcs/code/PrintArgs.exe ";
System.out.println("PrintArgs.exe argument1 \"argument 2\" \"\\some\\path with\\spaces\"");
Process proc = rt.exec(PrintArgs + "argument1 \"argument 2\" \"\\some\\path with\\spaces\"");
getOutput(proc);
System.out.println(nl + "+-----------------------------------------------------+" );
System.out.println(nl + "[rt.exec] PrintArgs Nested Quotes");
System.out.println("PrintArgs argument1 \"she said, \"you had me at hello\"\" \"\\some\\path with\\spaces\"");
proc = rt.exec(PrintArgs + "argument1 \"she said, \"you had me at hello\"\" \"\\some\\path with\\spaces\"");
getOutput(proc);
System.out.println(nl + "+-----------------------------------------------------+" );
System.out.println(nl + "[rt.exec] PrintArgs Unbalanced Quotes");
System.out.println("PrintArgs argument1 \"argument\"2\" argument3 argument4");
proc = rt.exec(PrintArgs + "argument1 \"argument\"2\" argument3 argument4");
getOutput(proc);
System.out.println(nl + "+-----------------------------------------------------+" );
System.out.println(nl + "[rt.exec] PrintArgs trailing backslash");
System.out.println("PrintArgs \"\\some\\directory with\\spaces\\\" argument2");
proc = rt.exec(PrintArgs + "\"\\some\\directory with\\spaces\\\" argument2");
getOutput(proc);
System.out.println(nl + "+-----------------------------------------------------+" );
// Use ProcessBuilder and separate commands and each argument
System.out.println(nl + "[ProcessBuilder] PrintArgs Unbalanced Quotes");
String[] PrintArgsCommand = {PrintArgs, "argument1 \"argument\"2\" argument3 argument4"};
System.out.println("PrintArgs argument1 \"argument\"2\" argument3 argument4");
ProcessBuilder pb = new ProcessBuilder(PrintArgsCommand);
proc = pb.start();
getOutput(proc);
System.out.println(nl + "+-----------------------------------------------------+" );
System.out.println(nl + "[rt.exec] command shell with &calc argument");
System.out.println("cmd.exe /C dir &calc");
String dir = "&calc"; // substituted for System.getProperty("dir");
System.out.println("cmd.exe /C dir " + dir);
proc = rt.exec("cmd.exe /C dir " + dir);
int result = proc.waitFor();
if (result != 0) {
System.out.println("process error: " + result);
}
InputStream in = (result == 0) ? proc.getInputStream() : proc.getErrorStream();
int c;
while ((c = in.read()) != -1) {
System.out.print((char) c);
}
System.out.println(nl + "+-----------------------------------------------------+" );
// Using ProcessBuilder incorrectly (without separating arguments)
System.out.println(nl + "[ProcessBuilder] used incorrectly (without separating arguments)");
System.out.println("C:\\Windows\\System32\\cmd.exe /C dir " + dir);
pb = new ProcessBuilder("C:\\Windows\\System32\\cmd.exe /C dir " + dir);
try {
proc = pb.start();
getOutput(proc);
} catch (IOException ioe) {
ioe.printStackTrace();
}
System.out.println(nl + "+-----------------------------------------------------+" );
// Use ProcessBuilder and separate commands and each argument
System.out.println(nl + "[ProcessBuilder] separating commands and each argument");
String[] command = {"C:\\Windows\\System32\\cmd.exe", "/C", "dir", dir};
System.out.println(Arrays.toString(command) + dir);
pb = new ProcessBuilder(command);
proc = pb.start();
getOutput(proc);
System.out.println(nl + "+-----------------------------------------------------+" );
// Secure method avoiding Runtime.exec()
System.out.println(nl + "[Library function] dir &calc");
// File pathname = new File(System.getProperty("dir"));
File pathname = new File(dir);
if (pathname.isDirectory()) {
for (String file : Objects.requireNonNull(pathname.list())) {
System.out.println(file);
}
} else {
System.out.println("Not a directory");
}
}
}