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+)\""); abstract public HttpResponse getPage(URI url, HashMap<String, String> headers) throws IOException; abstract public CookieStore getCookieStore(); public boolean cloudFlareSolve(String responseString) { // Rhino Context rhino = Context.enter(); try { String domain = "www.example.com"; // CF Thread.sleep(5000); // 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("\\s{3,}[az](?: = |\\.).+", ""); String js = operation.replace("\n", ""); rhino.setOptimizationLevel(-1); // rhino Android Scriptable scope = rhino.initStandardObjects(); // // either do or die trying int result = ((Double) rhino.evaluateString(scope, js, "CloudFlare JS Challenge", 1, null)).intValue(); String answer = String.valueOf(result + domain.length()); // javascript challenge final List<NameValuePair> params = new ArrayList<>(3); params.add(new BasicNameValuePair("jschl_vc", challenge)); params.add(new BasicNameValuePair("pass", challengePass)); params.add(new BasicNameValuePair("jschl_answer", answer)); HashMap<String, String> headers = new HashMap<>(1); headers.put("Referer", "http://" + domain + "/"); // url , String url = "http://" + domain + "/cdn-cgi/l/chk_jschl?" + URLEncodedUtils.format(params, "windows-1251"); HttpResponse response = getPage(URI.create(url), headers); if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // , Referer response.getEntity().consumeContent(); // return true; } } catch (Exception e) { return false; } finally { Context.exit(); // Rhino } return false; } private void syncCookiesWithWebViews() { List<Cookie> cookies = getCookieStore().getCookies(); CookieManager cookieManager = CookieManager.getInstance(); // CookieManager cookies WebView for (Cookie cookie : cookies) { String cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain(); cookieManager.setCookie("diary.ru", cookieString); } }
Source: https://habr.com/ru/post/258101/
All Articles