Skip to content

Commit

Permalink
FFS, CloudFlare!
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaned1as committed Jun 15, 2016
1 parent b7934e2 commit 62ec4c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
8 changes: 4 additions & 4 deletions build.gradle
Expand Up @@ -4,7 +4,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.android.tools.build:gradle:2.1.2'
}
}

Expand Down Expand Up @@ -37,13 +37,13 @@ dependencies {

android {
compileSdkVersion 23
buildToolsVersion '24.0.0-rc2'
buildToolsVersion '23.0.1'

defaultConfig {
minSdkVersion 14
targetSdkVersion 23
versionCode 257
versionName "2.5.7"
versionCode 259
versionName "2.5.9"
}

try {
Expand Down
44 changes: 22 additions & 22 deletions src/main/java/adonai/diary_browser/DiaryHttpClient.java
Expand Up @@ -45,11 +45,11 @@
public class DiaryHttpClient {
public final static String CLOUDFLARE_ANCHOR = "a = document.getElementById('jschl-answer');";
public final static String FIXED_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36";

private final static Pattern OPERATION_PATTERN = Pattern.compile("setTimeout\\(function\\(\\)\\{\\s+(var t,r,a,f.+?\\r?\\n[\\s\\S]+?a\\.value =.+?)\\r?\\n");

private final static Pattern PASS_PATTERN = Pattern.compile("name=\"pass\" value=\"(.+?)\"");
private final static Pattern CHALLENGE_PATTERN = Pattern.compile("name=\"jschl_vc\" value=\"(\\w+)\"");

private final static Pattern OPERATION_PATTERN = Pattern.compile("setTimeout\\(function\\(\\)\\{\\s+(var s,t,o,p,b,r,e,a,k,i,n,g,f.+?\\r?\\n[\\s\\S]+?a\\.value =.+?)\\r?\\n");

private URI currentUrl = URI.create("http://www.diary.ru");

OkHttpClient httpClient = new OkHttpClient();
Expand Down Expand Up @@ -91,7 +91,7 @@ public void run() {
public CookieStore getCookieStore() {
return cookieManager.getCookieStore();
}

public boolean hasCookie(@NonNull String name) {
for(HttpCookie cookie : cookieManager.getCookieStore().getCookies()) {
if(cookie.getName().equals(name))
Expand All @@ -103,18 +103,18 @@ public boolean hasCookie(@NonNull String name) {
public String postPageToString(@NonNull String url, @NonNull RequestBody data) {
return postPageToString(url, data, null);
}

public String postPageToString(@NonNull String url, @NonNull RequestBody data, @Nullable Headers headers) {
URI current = resolve(url);

Request.Builder httpPost = new Request.Builder()
.url(HttpUrl.get(current))
.post(data);

if(headers != null) {
httpPost.headers(headers);
}

Call call = httpClient.newCall(httpPost.build());
runningRequests.add(call);
try {
Expand All @@ -125,7 +125,7 @@ public String postPageToString(@NonNull String url, @NonNull RequestBody data, @
return null;
}
}

public String postPageToString(@NonNull String url, @NonNull List<Pair<String, String>> nameValuePairs) {
URI current = resolve(url);
FormEncodingBuilder rb = new FormEncodingBuilder();
Expand All @@ -135,7 +135,7 @@ public String postPageToString(@NonNull String url, @NonNull List<Pair<String, S
} catch (UnsupportedEncodingException ignored) {
}
}

Request httpPost = new Request.Builder()
.url(HttpUrl.get(current))
.post(rb.build())
Expand Down Expand Up @@ -207,7 +207,7 @@ public Response getPage(@NonNull URI url) throws IOException {
try {
Call call = httpClient.newCall(new Request.Builder().url(HttpUrl.get(url)).get().build());
runningRequests.add(call);

Response result = call.execute();
syncCookiesWithWebViews();
return result;
Expand Down Expand Up @@ -246,30 +246,30 @@ public boolean cloudFlareSolve(@NonNull String responseString) {
try {
String domain = "www.diary.ru";
getPage(URI.create("http://" + domain));

// CF should wait
Thread.sleep(5000);

// extract the arithmetic operation
Matcher operationSearch = OPERATION_PATTERN.matcher(responseString);
Matcher challengeSearch = CHALLENGE_PATTERN.matcher(responseString);
Matcher passSearch = PASS_PATTERN.matcher(responseString);
if(!operationSearch.find() || !passSearch.find() || !challengeSearch.find())
return false;

String rawOperation = operationSearch.group(1);
String challengePass = passSearch.group(1);
String challenge = challengeSearch.group(1);

String operation = rawOperation
.replaceAll("a\\.value =(.+?) \\+ .+?;", "$1")
.replaceAll("a\\.value = (parseInt\\(.+?\\)).+", "$1")
.replaceAll("\\s{3,}[a-z](?: = |\\.).+", "");
String js = operation.replace("\n", "");

rhino.setOptimizationLevel(-1);
Scriptable scope = rhino.initStandardObjects();
int result = ((Double) rhino.evaluateString(scope, js, "CloudFlare JS Challenge", 1, null)).intValue();

String answer = String.valueOf(result + domain.length());

Headers headers = new Headers.Builder()
Expand All @@ -284,7 +284,7 @@ public boolean cloudFlareSolve(@NonNull String responseString) {
.addEncodedQueryParameter("pass", URLEncoder.encode(challengePass, "windows-1251"))
.addEncodedQueryParameter("jschl_answer", URLEncoder.encode(answer, "windows-1251"))
.build().toString();

Response response = getPage(URI.create(url), headers);
if(response.isSuccessful()) {
response.request();
Expand Down Expand Up @@ -345,11 +345,11 @@ public void writeTo(BufferedSink sink) throws IOException {
}
}
}

private void syncCookiesWithWebViews() {
List<HttpCookie> cookies = getCookieStore().getCookies();
CookieManager cookieManager = CookieManager.getInstance();

// to webviews
for (HttpCookie cookie : cookies) {
String cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();
Expand Down Expand Up @@ -381,7 +381,7 @@ public void syncCookiesWithClient() {
public void setCurrentUrl(URI url) {
currentUrl = url;
}

public URI resolve(String url) {
return currentUrl.resolve(url);
}
Expand Down

0 comments on commit 62ec4c3

Please sign in to comment.