From afd1c6fc796e9ab853a24b2adc26832b6bf11136 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 22 Oct 2018 19:57:09 +0300 Subject: [PATCH] Deobfuscate Cloudflare-obfuscated emails --- app/build.gradle | 2 +- .../gr/thmmy/mthmmy/base/BaseApplication.java | 11 ++++- .../thmmy/mthmmy/utils/EmailDeobfuscator.java | 44 +++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/utils/EmailDeobfuscator.java diff --git a/app/build.gradle b/app/build.gradle index d13093bf..cfb58070 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -64,7 +64,7 @@ dependencies { implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'com.google.android.material:material:1.0.0' implementation 'com.google.firebase:firebase-core:16.0.4' - implementation 'com.google.firebase:firebase-messaging:17.3.3' + implementation 'com.google.firebase:firebase-messaging:17.3.4' implementation 'com.crashlytics.sdk.android:crashlytics:2.9.5' implementation 'com.squareup.okhttp3:okhttp:3.11.0' implementation 'com.squareup.picasso:picasso:2.5.2' diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java index d181301d..e218c9f4 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java @@ -38,8 +38,11 @@ import io.fabric.sdk.android.Fabric; import okhttp3.HttpUrl; import okhttp3.OkHttpClient; import okhttp3.Request; +import okhttp3.Response; import timber.log.Timber; +import static gr.thmmy.mthmmy.utils.EmailDeobfuscator.deobfuscate; + public class BaseApplication extends Application { private static BaseApplication baseApplication; //BaseApplication singleton @@ -100,8 +103,14 @@ public class BaseApplication extends Application { request = request.newBuilder().url(newUrl).build(); } } - return chain.proceed(request); + Response response = chain.proceed(request); + try { + response = deobfuscate(response); + } catch (Exception e) { + Timber.e(e, "Email deobfuscation error."); + } + return response; }) .connectTimeout(30, TimeUnit.SECONDS) .writeTimeout(30, TimeUnit.SECONDS) diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/EmailDeobfuscator.java b/app/src/main/java/gr/thmmy/mthmmy/utils/EmailDeobfuscator.java new file mode 100644 index 00000000..56ee76ad --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/EmailDeobfuscator.java @@ -0,0 +1,44 @@ +package gr.thmmy.mthmmy.utils; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.nodes.TextNode; +import org.jsoup.select.Elements; + +import java.io.IOException; + +import okhttp3.MediaType; +import okhttp3.Response; +import okhttp3.ResponseBody; + +//Deobfuscates Cloudflare-obfuscated emails +public class EmailDeobfuscator { + public static Response deobfuscate(Response response) throws IOException { + String responseBody = response.body().string(); + Document document = Jsoup.parse(responseBody); + Elements obfuscatedEmails = document.select("span.__cf_email__"); + for (Element obfuscatedEmail : obfuscatedEmails) { + String email = deobfuscateEmail(obfuscatedEmail.attr("data-cfemail")); + Element parent = obfuscatedEmail.parent(); + if (parent.is("a")&&parent.attr("href").contains("email-protection")) + parent.attr("href", "mailto:"+email); + obfuscatedEmail.replaceWith(new TextNode(email, "")); + } + + MediaType contentType = response.body().contentType(); + ResponseBody body = ResponseBody.create(contentType, document.toString()); + return response.newBuilder().body(body).build(); + } + + + private static String deobfuscateEmail(final String encodedString) { + final StringBuilder email = new StringBuilder(); + final int r = Integer.parseInt(encodedString.substring(0, 2), 16); + for (int n = 2; n < encodedString.length(); n += 2) { + final int i = Integer.parseInt(encodedString.substring(n, n+2), 16) ^ r; + email.append(Character.toString ((char) i)); + } + return email.toString(); + } +}