Author Topic: [Java] RedirectScanner - show real URL  (Read 5967 times)

0 Members and 1 Guest are viewing this topic.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
[Java] RedirectScanner - show real URL
« on: October 24, 2012, 04:43:15 pm »
Hello EZ,

this code snippet checks a given URL for redirects. I.e. if there is someone providing a shortened link with services like TinyURL can check the real URL here without visiting the site.

Code: (Java) [Select]
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

public class RedirectLocationScanner {

    public static void main(final String[] args) throws IOException {
       
        String urlStr = null;

        if (!(args.length == 1)) {
            Scanner scan = new Scanner(System.in);
            System.out.println("URL?");
            urlStr = scan.nextLine();
            scan.close();
        } else {
            urlStr = args[0];
        }
       
        if(!urlStr.startsWith("http://")){
            urlStr = "http://" + urlStr;
        }
       
        URL url = new URL(urlStr);
        HttpURLConnection.setFollowRedirects(false);
        String redirectLoc = url.openConnection().getHeaderField("Location");
       
        if(redirectLoc == null){
            System.out.println("No redirect.");
        } else {
            System.out.println("real URL: " + redirectLoc);
        }
    }
}

__________________________________________________________________


This code is an updated version, that also scans for target links in ad link services like adfly, adfoc and linkbucks.

Code: (Java) [Select]
import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.Scanner;
 
 public class LinkScanner {
 
     private static final String[] adServiceStrings = { "adf.ly", "adfoc.us",
             "any.gs", "tinylinks,co", "linkbucks.com", "yyv.co", "miniurls.co",
             "qqc.co", "whackyvidz.com", "ultrafiles.net", "dyo.gs",
             "megaline.co", "uberpicz.com", "linkgalleries.net", "qvvo.com",
             "urlbeat.net", "seriousfiles.com", "zxxo.net", "ugalleries.net",
             "picturesetc.net" };
 
     private static final String USAGE = "java -jar scanner.jar <URL>";
 
     public static void main(String[] args) {
         try {
             if (args.length == 1) {
                 System.out.println("Target URL: ");
                 scanAndPrint(args[0]);
             } else if (args.length == 0) {
                 Scanner scan = new Scanner(System.in);
                 System.out.println("URL?");
                 String url = scan.nextLine();
                 scanAndPrint(url);
             } else {
                 System.out.println(USAGE);
             }
         } catch (IOException e) {
             System.err.println(e.getMessage());
         }
     }
 
     private static void scanAndPrint(String string) throws IOException {
         System.out.println("Scanning ...");
         String target = new LinkScanner().scan(string);
         if (target != null) {
             System.out.println("Target URL: ");
             System.out.println(target);
         } else {
             System.err.println("No target URL found");
         }
     }
 
     public String scan(String urlStr) throws IOException {
         if (!urlStr.startsWith("http://")) {
             urlStr = "http://" + urlStr;
         }
         URL url = new URL(urlStr);
         HttpURLConnection.setFollowRedirects(false);
 
         if (isAdServiceLink(urlStr)) {
             return handleAdLink(url);
         } else {
             return handleRedirect(url);
         }
 
     }
 
     private boolean isAdServiceLink(String urlStr) {
         for (String str : adServiceStrings) {
             if (urlStr.contains(str)) {
                 return true;
             }
         }
         return false;
     }
 
     private String handleRedirect(URL url) throws IOException {
         return url.openConnection().getHeaderField("Location");
     }
 
     private String handleAdLink(URL url) throws IOException {
         URLConnection urlc = url.openConnection();
         urlc.addRequestProperty("user-agent", "Firefox");
         BufferedReader in = null;
         try {
             in = new BufferedReader(
                     new InputStreamReader(urlc.getInputStream()));
             String line;
             while ((line = in.readLine()) != null) {
                 if (line.contains("var zzz =")) {
                     return extractADFlyURL(line);
                 }
                 if (line.contains("var click_url =")
                         && !line.contains("//var click_url =")) {
                     return extractADFocURL(line);
                 }
                 if (line.contains("Lbjs.TargetUrl =")) {
                     return extractLinkBucksURL(line);
                 }
             }
             throw new IOException("Unable to find target URL in link");
         } finally {
             if (in != null) {
                 in.close();
             }
         }
     }
 
     private String extractLinkBucksURL(String line) {
         String go = line.split("'")[1];
         return go;
     }
 
     private String extractADFocURL(String line) throws IOException {
         String go = line.split("\"")[1];
         return go;
     }
 
     private String extractADFlyURL(String line) throws IOException {
         String go = line.split("'")[1];
         String redirect = handleRedirect(new URL("http://adf.ly" + go));
         if (redirect == null) {
             return go;
         }
         return redirect;
     }
 }
« Last Edit: February 13, 2013, 09:20:12 am by Deque »

Offline Ragehottie

  • Knight
  • **
  • Posts: 313
  • Cookies: -9
  • Hack to learn, not learn to hack.
    • View Profile
Re: [Java] RedirectScanner - show real URL
« Reply #1 on: October 24, 2012, 11:18:04 pm »
Cool, thanks. Been wanting one for the goo.gle(or goog.le or whatever) links.
Blog: rexmckinnon.tumblr.com

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: [Java] RedirectScanner - show real URL
« Reply #2 on: October 24, 2012, 11:57:48 pm »
Just tried and it works fine with goo.gl links.

Good job.
Satan911
Evilzone Network Administrator

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [Java] RedirectScanner - show real URL
« Reply #3 on: February 13, 2013, 09:20:59 am »
I updated the code.
Changelog:
* adfly changed its source, I made it working again
* added support for linkbucks and adfoc