-
Notifications
You must be signed in to change notification settings - Fork 0
/
UFWShellCommand.java
210 lines (188 loc) · 7.46 KB
/
UFWShellCommand.java
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import java.io.*;
import java.util.*;
import javax.swing.*;
public class UFWShellCommand {
String rootPassword;
public UFWShellCommand(String password) {
this.rootPassword = password;
}
public String getPassword() {
return rootPassword;
}
public void enableFireWall() {
command("sudo ufw enable");
}
public void disabelFireWall() {
command("sudo ufw disable");
}
public String getStatus() {
try {
String[] cmd = { "/bin/bash", "-c", "echo " + this.rootPassword + "| sudo -S ufw status" };
Process pb = Runtime.getRuntime().exec(cmd);
BufferedReader input = new BufferedReader(new InputStreamReader(pb.getInputStream()));
String line = input.readLine();
String status = line.split(": ")[1];
return status;
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Something went wrong!", "Warning", JOptionPane.WARNING_MESSAGE);
e.printStackTrace();
return "";
}
}
public String[] getDefaultRules() {
String[] defaultRules = new String[2];
try {
String[] cmd = { "/bin/bash", "-c", "echo " + this.rootPassword + "| sudo -S ufw status verbose" };
Process pb = Runtime.getRuntime().exec(cmd);
BufferedReader input = new BufferedReader(new InputStreamReader(pb.getInputStream()));
String line;
int i = 0;
while ((line = input.readLine()) != null && i < 2) {
i++;
}
String[] words = line.split("[: (),]");
defaultRules[0] = words[2];
defaultRules[1] = words[7];
return defaultRules;
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Something went wrong!", "Warning", JOptionPane.WARNING_MESSAGE);
String[] empty = {};
return empty;
}
}
public void setDefaultRule(String direction, String policy) {
command("sudo ufw default " + policy + " " + direction);
}
public String[][] getRules() {
ArrayList<String[]> rules = new ArrayList<String[]>();
try {
Process pb = Runtime.getRuntime().exec("sudo ufw status numbered");
BufferedReader input = new BufferedReader(new InputStreamReader(pb.getInputStream()));
String line;
int i = 0;
while ((line = input.readLine()) != null) {
if (i > 3) {
System.out.println("line" + i + ": " + line);
while (line.indexOf(" ") != -1) {
line = line.replaceAll(" ", " ");
}
String[] rule = line.trim().split(" ");
if (rule.length >= 3) {
String[] temp = new String[3];
for(int j=0; j<3; j++) {
temp[j] = rule[j];
}
rules.add(temp);
}
}
i++;
}
if (rules.size() == 0) {
String[][] empty = {};
return empty;
}
// for (String[] rule : rules) {
// for (String element : rule)
// System.out.println(element);
// }
String[][] convertedRules = new String[rules.size()][3];
for (int j = 0; j < rules.size(); j++) {
for (int l = 0; l < 3; l++) {
convertedRules[j][l] = rules.get(j)[l];
}
}
return convertedRules;
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Something went wrong!", "Warning", JOptionPane.WARNING_MESSAGE);
e.printStackTrace();
String[][] empty = {};
return empty;
}
}
public void deleteRule(int ruleIndex) {
command("sudo ufw --force delete " + ruleIndex);
}
public void deleteAllRules() {
command("sudo ufw --force reset");
enableFireWall();
}
public void addSimpleRule(String policy, String direction, String protocol, String port) {
String protocolString = "";
if (!protocol.equals("Both")) {
protocolString += " proto " + protocol.toLowerCase();
}
if (direction.equals("Both")) {
command("sudo ufw " + policy.toLowerCase() + " in to any port " + port + protocolString);
command("sudo ufw " + policy.toLowerCase() + " out to any port " + port + protocolString);
} else {
command("sudo ufw " + policy.toLowerCase() + " " + direction.toLowerCase() + " to any port " + port
+ protocolString);
}
}
public void addAdvancedRule(int numberInsert, String policy, String direction, String protocol, String itf,
String ipFrom, String portFrom, String ipTo, String portTo) {
String numberInsertString = "";
if (numberInsert != 0) {
numberInsertString += " insert " + numberInsert;
}
String protocolString = "";
if (!protocol.equals("Both")) {
protocolString += " proto " + protocol.toLowerCase();
}
String interfaceString = "";
if (!itf.equals("All interfaces")) {
interfaceString += " on " + itf;
}
String fromString = "";
if (!ipFrom.equals("") || !portFrom.equals("")) {
fromString += " from ";
if (ipFrom.equals("")) {
fromString += "any";
} else {
fromString += ipFrom;
}
if (!portFrom.equals("")) {
fromString += " port " + portFrom;
}
}
String toString = "";
if (!ipTo.equals("") || !portTo.equals("")) {
toString += " to ";
if (ipTo.equals("")) {
toString += "any";
} else {
toString += ipTo;
}
if (!portTo.equals("")) {
toString += " port " + portTo;
}
}
if (direction.equals("Both")) {
command("sudo ufw" + numberInsertString + " " + policy.toLowerCase() + " in" + interfaceString + fromString +
toString + protocolString);
command("sudo ufw" + numberInsertString + " " + policy.toLowerCase() + " out" + interfaceString + fromString +
toString + protocolString);
} else {
command("sudo ufw" + numberInsertString + " " + policy.toLowerCase() + " " + direction.toLowerCase() + interfaceString +
fromString + toString + protocolString);
}
}
public void command(String command) {
try {
Process pb = Runtime.getRuntime().exec(command);
String line;
BufferedReader input = new BufferedReader(new InputStreamReader(pb.getInputStream()));
if (input.readLine() == null) {
JOptionPane.showMessageDialog(null, "Command ERROR!", "Warning", JOptionPane.WARNING_MESSAGE);
}
while ((line = input.readLine()) != null) {
System.out.println(line);
}
pb.destroy();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Something went wrong!", "Warning", JOptionPane.WARNING_MESSAGE);
e.printStackTrace();
}
}
}