| 1 |
/* SOPBypass.java |
| 2 |
Copyright (C) 2015 Red Hat, Inc. |
| 3 |
|
| 4 |
This file is part of IcedTea. |
| 5 |
|
| 6 |
IcedTea is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, version 2. |
| 9 |
|
| 10 |
IcedTea is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with IcedTea; see the file COPYING. If not, write to |
| 17 |
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 |
02110-1301 USA. |
| 19 |
|
| 20 |
Linking this library statically or dynamically with other modules is |
| 21 |
making a combined work based on this library. Thus, the terms and |
| 22 |
conditions of the GNU General Public License cover the whole |
| 23 |
combination. |
| 24 |
|
| 25 |
As a special exception, the copyright holders of this library give you |
| 26 |
permission to link this library with independent modules to produce an |
| 27 |
executable, regardless of the license terms of these independent |
| 28 |
modules, and to copy and distribute the resulting executable under |
| 29 |
terms of your choice, provided that you also meet, for each linked |
| 30 |
independent module, the terms and conditions of the license of that |
| 31 |
module. An independent module is a module which is not derived from |
| 32 |
or based on this library. If you modify this library, you may extend |
| 33 |
this exception to your version of the library, but you are not |
| 34 |
obligated to do so. If you do not wish to do so, delete this |
| 35 |
exception statement from your version. |
| 36 |
*/ |
| 37 |
|
| 38 |
import java.applet.Applet; |
| 39 |
import java.io.BufferedReader; |
| 40 |
import java.io.InputStream; |
| 41 |
import java.io.InputStreamReader; |
| 42 |
import java.io.PrintWriter; |
| 43 |
import java.net.InetSocketAddress; |
| 44 |
import java.net.MalformedURLException; |
| 45 |
import java.net.Socket; |
| 46 |
import java.net.URL; |
| 47 |
import java.net.URLConnection; |
| 48 |
import java.security.AccessController; |
| 49 |
import java.security.AccessControlException; |
| 50 |
import java.security.PrivilegedAction; |
| 51 |
|
| 52 |
public class SOPBypassSigned extends Applet { |
| 53 |
private String unrelatedUrl; |
| 54 |
private String reachableResource; |
| 55 |
private String resourcesUrl; |
| 56 |
|
| 57 |
|
| 58 |
@Override |
| 59 |
public void init(){ |
| 60 |
setUnrelatedUrl(this.getParameter("unrelatedUrl")); |
| 61 |
setReachableResource(this.getParameter("reachableResource")); |
| 62 |
setResourcesUrl(this.getParameter("resourceUrl")); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void start() { |
| 67 |
System.out.println("Applet Started"); |
| 68 |
|
| 69 |
System.out.println("Codebase URL: " + getCodeBase()); |
| 70 |
System.out.println("DocumentBase URL: " + getDocumentBase()); |
| 71 |
System.out.println("unrelatedUrl: " + unrelatedUrl); |
| 72 |
|
| 73 |
AccessController.doPrivileged(new PrivilegedAction<Boolean>() { |
| 74 |
@Override |
| 75 |
public Boolean run() { |
| 76 |
checkPrivilege(); |
| 77 |
attemptSocketConnectionToCodebase(); |
| 78 |
attemptSocketConnectionToDocumentBase(); |
| 79 |
attemptSocketConnectionToUnrelated(); |
| 80 |
attemptSocketConnectionToResourcesLoc(); |
| 81 |
attemptUrlConnectionToCodebase(); |
| 82 |
attemptUrlConnectionToDocumentBase(); |
| 83 |
attemptUrlConnectionToUnrelated(); |
| 84 |
attemptUrlConnectionToResourcesLoc(); |
| 85 |
return true; |
| 86 |
} |
| 87 |
}); |
| 88 |
|
| 89 |
System.out.println("*** APPLET FINISHED ***"); |
| 90 |
} |
| 91 |
|
| 92 |
void checkPrivilege() { |
| 93 |
boolean canRead = false; |
| 94 |
try { |
| 95 |
System.getProperty("user.home"); |
| 96 |
canRead = true; |
| 97 |
} catch (AccessControlException ace) { |
| 98 |
ace.printStackTrace(); |
| 99 |
canRead = false; |
| 100 |
} |
| 101 |
System.out.println("Elevated privileges: " + canRead); |
| 102 |
} |
| 103 |
|
| 104 |
void attemptSocketConnectionToCodebase() { |
| 105 |
String host = getCodeBase().getHost(); |
| 106 |
int port = getCodeBase().getPort(); |
| 107 |
attemptSocketConnection(host, port, reachableResource, "codeBase", true); |
| 108 |
} |
| 109 |
|
| 110 |
void attemptSocketConnectionToDocumentBase() { |
| 111 |
String host = getDocumentBase().getHost(); |
| 112 |
int port = getDocumentBase().getPort(); |
| 113 |
attemptSocketConnection(host, port, reachableResource, "documentBase", true); |
| 114 |
} |
| 115 |
|
| 116 |
void attemptSocketConnectionToUnrelated() { |
| 117 |
String host = "example.com"; |
| 118 |
int port = 80; |
| 119 |
if (unrelatedUrl != null){ |
| 120 |
host=extractHost(unrelatedUrl); |
| 121 |
port=extractPort(unrelatedUrl); |
| 122 |
} |
| 123 |
attemptSocketConnection(host, port, reachableResource, "unrelated", true); |
| 124 |
} |
| 125 |
|
| 126 |
void attemptSocketConnectionToResourcesLoc() { |
| 127 |
String host = getCodeBase().getHost(); |
| 128 |
int port = getCodeBase().getPort(); |
| 129 |
//if resources url was null, then it was probably from codebase |
| 130 |
if (resourcesUrl != null){ |
| 131 |
host=extractHost(resourcesUrl); |
| 132 |
port=extractPort(resourcesUrl); |
| 133 |
} |
| 134 |
attemptSocketConnection(host, port, reachableResource, "resource's", true); |
| 135 |
} |
| 136 |
|
| 137 |
void attemptSocketConnection(String host, int port, String resource, String id, boolean sendData) { |
| 138 |
boolean connected = true; |
| 139 |
try { |
| 140 |
final Socket local = new Socket(); |
| 141 |
local.bind(null); |
| 142 |
local.connect(new InetSocketAddress(host, port)); |
| 143 |
final BufferedReader br = new BufferedReader(new InputStreamReader(local.getInputStream())); |
| 144 |
final PrintWriter writer = new PrintWriter(local.getOutputStream(), true); |
| 145 |
if (sendData) { |
| 146 |
writer.println("GET /" + reachableResource + " HTTP/1.1"); |
| 147 |
writer.println("Host: " + "itwTest"); |
| 148 |
writer.println("Accept: */*"); |
| 149 |
writer.println("User-Agent: Java"); //used to it to much |
| 150 |
writer.println(""); // Important, else the server will expect that there's more into the request. |
| 151 |
writer.flush(); |
| 152 |
String s = getText(br); |
| 153 |
System.out.println("" + s); |
| 154 |
if (s == null || s.trim().isEmpty()) { |
| 155 |
connected = false; |
| 156 |
} |
| 157 |
local.close(); |
| 158 |
} |
| 159 |
} catch (Exception e) { |
| 160 |
connected = false; |
| 161 |
e.printStackTrace(); |
| 162 |
} |
| 163 |
System.out.println("SocketConnection:" + id + " " + connected); |
| 164 |
} |
| 165 |
|
| 166 |
void attemptUrlConnectionToCodebase() { |
| 167 |
attemptUrlConnection(getCodeBase(), reachableResource, "codeBase"); |
| 168 |
} |
| 169 |
|
| 170 |
void attemptUrlConnectionToDocumentBase() { |
| 171 |
attemptUrlConnection(getDocumentBase(), reachableResource, "documentBase"); |
| 172 |
} |
| 173 |
|
| 174 |
void attemptUrlConnectionToUnrelated() { |
| 175 |
try { |
| 176 |
if (unrelatedUrl == null) { |
| 177 |
attemptUrlConnection(new URL("http://example.com:80"), reachableResource, "unrelated"); |
| 178 |
} else { |
| 179 |
attemptUrlConnection(new URL(unrelatedUrl), reachableResource, "unrelated"); |
| 180 |
} |
| 181 |
} catch (MalformedURLException e) { |
| 182 |
e.printStackTrace(); |
| 183 |
System.out.println("Unrelated URL test failed due to MalformedURLException"); |
| 184 |
System.out.println("URLConnection:unrelated false"); |
| 185 |
} |
| 186 |
} |
| 187 |
void attemptUrlConnectionToResourcesLoc() { |
| 188 |
try { |
| 189 |
if (resourcesUrl == null) { |
| 190 |
//if resources url was null, then it was probably from codebase |
| 191 |
attemptUrlConnection(getCodeBase(), reachableResource, "resource's"); |
| 192 |
} else { |
| 193 |
attemptUrlConnection(new URL(resourcesUrl), reachableResource, "resource's"); |
| 194 |
} |
| 195 |
} catch (MalformedURLException e) { |
| 196 |
e.printStackTrace(); |
| 197 |
System.out.println("resource's URL test failed due to MalformedURLException"); |
| 198 |
System.out.println("URLConnection:resource's false"); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
void attemptUrlConnection(URL url, String resource, String id) { |
| 203 |
boolean connected = true; |
| 204 |
try { |
| 205 |
URLConnection conn = url.openConnection(); |
| 206 |
conn.connect(); |
| 207 |
conn.getContentEncoding(); |
| 208 |
conn.getContentLength(); |
| 209 |
conn.getContentType(); |
| 210 |
if (resource != null) { |
| 211 |
URLConnection connn = concateUrlAndResource(url, resource).openConnection(); |
| 212 |
connn.connect(); |
| 213 |
connn.getContentEncoding(); |
| 214 |
connn.getContentLength(); |
| 215 |
connn.getContentType(); |
| 216 |
String s = getText(connn.getInputStream()); |
| 217 |
System.out.println("" + s); |
| 218 |
if (s == null || s.trim().isEmpty()) { |
| 219 |
connected = false; |
| 220 |
} |
| 221 |
} |
| 222 |
} catch (Exception e) { |
| 223 |
connected = false; |
| 224 |
e.printStackTrace(); |
| 225 |
} |
| 226 |
System.out.println("URLConnection:" + id + " " + connected); |
| 227 |
} |
| 228 |
|
| 229 |
public static void main(String[] args) { |
| 230 |
//args = new String[]{"SOPBypass-filtered.html", "http://localhost:44321"}; |
| 231 |
SOPBypassSigned sop = new SOPBypassSigned(); |
| 232 |
if (args.length > 0) { |
| 233 |
sop.setReachableResource(args[0]); |
| 234 |
} |
| 235 |
if (args.length > 1) { |
| 236 |
sop.setUnrelatedUrl(args[1]); |
| 237 |
} |
| 238 |
if (args.length > 2) { |
| 239 |
sop.setResourcesUrl(args[2]); |
| 240 |
} |
| 241 |
sop.start(); |
| 242 |
} |
| 243 |
|
| 244 |
private void setUnrelatedUrl(String s) { |
| 245 |
unrelatedUrl = s; |
| 246 |
} |
| 247 |
|
| 248 |
private void setReachableResource(String s) { |
| 249 |
reachableResource = s; |
| 250 |
} |
| 251 |
|
| 252 |
public void setResourcesUrl(String resourcesUrl) { |
| 253 |
this.resourcesUrl = resourcesUrl; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
static private String extractHost(String unrelatedUrl) { |
| 259 |
String s[] = unrelatedUrl.split(":"); |
| 260 |
while (s[1].startsWith("/")) { |
| 261 |
s[1] = s[1].substring(1); |
| 262 |
} |
| 263 |
return s[1]; |
| 264 |
} |
| 265 |
|
| 266 |
static private int extractPort(String unrelatedUrl) { |
| 267 |
String s[] = unrelatedUrl.split(":"); |
| 268 |
if (s.length < 3) { |
| 269 |
return 80; |
| 270 |
} |
| 271 |
//protocol:host:port or |
| 272 |
//protocol:i:p:...:v:6:port |
| 273 |
int i = s.length - 1; |
| 274 |
try { |
| 275 |
return new Integer(s[i]); |
| 276 |
} catch (NumberFormatException ex) { |
| 277 |
//nope |
| 278 |
} |
| 279 |
return 80; |
| 280 |
} |
| 281 |
|
| 282 |
private URL concateUrlAndResource(URL url, String resource) { |
| 283 |
String s = url.toExternalForm(); |
| 284 |
String badResource1 = resource.replace("-filtered", ""); |
| 285 |
String badResource2 = badResource1.replace("Signed", ""); |
| 286 |
//see testcases for usages |
| 287 |
s = s.replace("/codebase/", "/"); |
| 288 |
s = s.replace("/"+badResource1, "/"); |
| 289 |
s = s.replace("/"+badResource2, "/"); |
| 290 |
try { |
| 291 |
//docbase may have it |
| 292 |
if (s.endsWith(resource)) { |
| 293 |
return new URL(s); |
| 294 |
} |
| 295 |
if (s.endsWith("/")) { |
| 296 |
s = s + resource; |
| 297 |
} else { |
| 298 |
s = s + "/" + resource; |
| 299 |
} |
| 300 |
return new URL(s); |
| 301 |
} catch (MalformedURLException ex) { |
| 302 |
//jsut adding resource to vlaisd url. |
| 303 |
//should not happen, if so, die later |
| 304 |
return null; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
public static String getText(InputStream is) throws Exception { |
| 309 |
BufferedReader in = new BufferedReader(new InputStreamReader(is)); |
| 310 |
try { |
| 311 |
return getText(in); |
| 312 |
} finally { |
| 313 |
in.close(); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
public static String getText(BufferedReader in) throws Exception { |
| 318 |
|
| 319 |
StringBuilder response = new StringBuilder(); |
| 320 |
String inputLine; |
| 321 |
while ((inputLine = in.readLine()) != null) { |
| 322 |
response.append(inputLine); |
| 323 |
} |
| 324 |
return response.toString(); |
| 325 |
} |
| 326 |
|
| 327 |
} |