Advertisement
Guest User

Untitled

a guest
Feb 17th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.64 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.net.InetSocketAddress;
  5. import java.net.Socket;
  6.  
  7. public class TurkBot
  8. {
  9.   //Main, that's the entry point of the program.
  10.   public static void main(String[] args)
  11.   {
  12.     int tc;
  13.     try
  14.     {
  15.       if(args.length < 1)
  16.         throw new NumberFormatException();
  17.       tc = Integer.parseInt(args[0]);
  18.     }
  19.     catch(NumberFormatException e)
  20.     {
  21.       System.out.println("java TurkBot Threads");
  22.       return;
  23.     }
  24.     //Start 6 Threads:
  25.     for(int i = 0; i < tc; i++)
  26.       (new Task()).start();
  27.   }
  28.  
  29.   //A thread, started by the main class
  30.   public static class Task extends Thread
  31.   {
  32.     public void run()
  33.     {
  34.       //Just start the spam function.
  35.       spamTask();
  36.     }
  37.   }
  38.  
  39.   private static final String host = "turkcraft.net";
  40.  
  41.   //The spam function... :D
  42.   public static void spamTask()
  43.   {
  44.     try
  45.     {
  46.       Socket s;
  47.       String session = null;
  48.       String captchaID = null;
  49.       String header;
  50.       InputStream in;
  51.       OutputStream out;
  52.       byte[] buffer;
  53.       String reply;
  54.       int cl = 0;
  55.       int got;
  56.       String[] split;
  57.       int t;
  58.       String postdata;
  59.       String captcha;
  60.       String token;
  61.       StringBuilder sb;
  62.      
  63.       //The main loop. As we want to spam it should never end.
  64.       while(true)
  65.       {
  66.         captcha = null;
  67.         //This loop fetches the registration page till it gets the anser to the captcha.
  68.         while(captcha == null)
  69.         {
  70.           //New socket
  71.           s = new Socket();
  72.           //As we want to register a new user: Remove the session hash.
  73.           session = null;
  74.           //Connect to the web server
  75.           s.connect(new InetSocketAddress(host, 80));
  76.           //Get an input and an output stream to talk to the server.
  77.           in = s.getInputStream();
  78.           out = s.getOutputStream();
  79.           //Generate a valid HTTP header:
  80.           /*
  81.            * GET /index.php?register/ HTTP/1.1
  82.            * Host: turkcraft.net
  83.            * Connection: close
  84.            * User-Agent: V10Bot
  85.            */
  86.           header = "GET /index.php?register/ HTTP/1.1\r\nHost: "+host+"\r\nConnection: close\r\nUser-Agent: V10Bot\r\n\r\n";
  87.           //Send it to the host
  88.           out.write(header.getBytes());
  89.           out.flush();
  90.           //Read the HTTP header the server replies. The header ends with a double newline.
  91.           //We read it byte for byte so we can stop after the header.
  92.           reply = new String();
  93.           //Read 8 kb. The header shouldn't be bigger. This may cut off some parts of the body, but that's no big problem as there's no information we need.
  94.           buffer = new byte[8192];
  95.           in.read(buffer);
  96.           reply = new String(buffer);
  97.           //We have the header, let's parse it line by line.
  98.           split = reply.split("\r\n\r\n");
  99.           for(String line: split[0].split("\r\n"))
  100.           {
  101.             //If the line describes the length of the HTTP body (the html page) save the length.
  102.             //We need it later to read the buffer.
  103.             if(line.startsWith("Content-Length: "))
  104.               cl = Integer.parseInt(line.substring(16));
  105.             //Else if the line is used to send us the cookie with the session hash, save that session hash
  106.             //So we can authenticate us later.
  107.             else if(line.startsWith("Set-Cookie: xf_session="))
  108.               session = line.substring(23, line.indexOf(";"));
  109.           }
  110.           //Now read the body. As we have the length let's make a buffer with it and read from the socket till we have all.
  111.           //The buffer may be bigger than it needs to be, I was to lazy to convert to the 8kb header read. :P
  112.           got = cl - split[1].length();
  113.           buffer = new byte[cl];
  114.           while(got > 0)
  115.           {
  116.             t = cl - got;
  117.             got -= in.read(buffer, t, cl - t);
  118.           }
  119.           //Allright, we don't need the connection anymore, let's close it.
  120.           in.close();
  121.           out.close();
  122.           s.close();
  123.           //Let's make a readable string from the byte buffer containing the body
  124.           reply = new String(buffer);
  125.           //Now let's parse the captchaID + the security question (the captcha itself) with a bit split/substring magic
  126.           split = reply.split("\t\t<div class=\"ddText\"><label for=\"");
  127.           if(split.length < 2)
  128.             continue;
  129.           t = split[1].indexOf("\"");
  130.           captchaID = split[1].substring(0, t);
  131.           //Let's get the answer for the question or continue with the loop (see above).
  132.           reply = split[1].substring(t+2, split[1].indexOf("<"));
  133.           if(reply.equals("Minecraft'ın yapımcısı kimdir?") || reply.equals("Minecraft'da \"Patlayan Yaratık\"a ne isim verilir?"))
  134.             captcha = "Notch";
  135.           else if(reply.equals("Hangisi bir isimdir? \"Asdasd\", \"Mehmet\""))
  136.             captcha = "Mehmet";
  137.           else if(reply.equals("Türkiyenin başkenti hangi ilimizdir?"))
  138.             captcha = "Ankara";
  139.           else if(reply.equals("\"Hello\"nun Türkçe karşılığı nedir?"))
  140.             captcha = "Merhaba";
  141.         }
  142.        
  143.         //Now register the user with the session in a cookie + the captcha and the other needed stuff like username, email, password, ... in the POST data.
  144.        
  145.         //First: Connect on a new socket.
  146.         s = new Socket();
  147.         s.connect(new InetSocketAddress(host, 80));
  148.         in = s.getInputStream();
  149.         out = s.getOutputStream();
  150.         //We need a username, let's use a random number generator for it.
  151.         postdata = Long.toHexString(Double.doubleToLongBits(Math.random()));
  152.         //If the username is to long et's short it
  153.         if(postdata.length() > 15)
  154.           postdata = postdata.substring(0, 14);
  155.         //Now let's make the POST data:
  156.         /*
  157.          * username = <username>
  158.          * email = <username>@<username>.com
  159.          * password = <username>
  160.          * password_confirm = <username>
  161.          * dob_month = 1
  162.          * dob_day = 1
  163.          * dob_year = 1990
  164.          * gender = male
  165.          * timezone = Europe/Amsterdam
  166.          * captcha_question_answer = <captcha>
  167.          * captcha_questionHash = <capthcaID>
  168.          */
  169.         postdata = "username="+postdata+"&email="+postdata+"%40"+postdata+".com&password="+postdata+"&password_confirm="+postdata+"&dob_month=1&dob_day=1&dob_year=1990&gender=male&timezone=Europe%2FAmsterdam&captcha_question_answer="+captcha+"&captcha_question_hash="+captchaID+"&agree=1\r\n";
  170.         //Now let's make a valid HTTP header:
  171.         /*
  172.          * POST /index.php?register/register HTTP/1.1
  173.          * Host: turkcraft.net
  174.          * Connection: close
  175.          * User-Agent: V10bot
  176.          * Content-Length <postdata length>
  177.          * Content-Type: application/x-www-form-urlencoded
  178.          */
  179.        
  180.         sb = new StringBuilder("POST /index.php?register/register HTTP/1.1\r\nHost: ").append(host).append("\r\nConnection: close\r\nUser-Agent: V10Bot\r\nContent-Length: ").append(postdata.length()).append("\r\nContent-Type: application/x-www-form-urlencoded\r\n");
  181.         //If we have a session hash (we should!) let's add it as a cookie
  182.         if(session != null)
  183.           sb.append("Cookie: xf_session=").append(session).append("\r\n");
  184.         //And a ending newline to tell the server that the POST data begins. After that the postdata.
  185.         sb.append("\r\n").append(postdata);
  186.         //Let's send that data to the server:
  187.         /*
  188.          * POST /index.php?register/register HTTP/1.1
  189.          * Host: turkcraft.net
  190.          * Connection: close
  191.          * User-Agent: V10bot
  192.          * Content-Length <postdata length>
  193.          * Content-Type: application/x-www-form-urlencoded
  194.          * Cookie: xf_session=<session>
  195.          *
  196.          * username=<username>&email=<username>@<username>.com&password=<username>&password_confirm=<username>&dob_month=1&dob_day=1&dob_year=1990&gender=male&timezone=Europe/Amsterdam&captcha_question_answer=<captcha>&captcha_questionHash=<capthcaID>
  197.          */
  198.         out.write(sb.toString().getBytes());
  199.         out.flush();
  200.         //Read the header
  201.         buffer = new byte[8192];
  202.         in.read(buffer);
  203.         reply = new String(buffer);
  204.         for(String line: reply.split("\r\n\r\n")[0].split("\r\n"))
  205.           if(line.startsWith("Set-Cookie: xf_session="))
  206.             session = line.substring(23, line.indexOf(";"));
  207.         //No need to read the body, xenforo seems to register us while we read the header.
  208.         in.close();
  209.         out.close();
  210.         s.close();
  211.        
  212.         //Now, as we are registred, go to the create thread page...
  213.         s = new Socket();
  214.         s.connect(new InetSocketAddress(host, 80));
  215.         in = s.getInputStream();
  216.         out = s.getOutputStream();
  217.         sb = new StringBuilder("GET /index.php?forums/survival-mod-sunucular%C4%B1.20/create-thread HTTP/1.1\r\nHost: ").append(host).append("\r\nConnection: close\r\nUser-Agent: V10Bot\r\n");
  218.         if(session != null)
  219.           sb.append("Cookie: xf_session=").append(session).append("\r\n");
  220.         sb.append("\r\n").append(postdata);
  221.         out.write(sb.toString().getBytes());
  222.         out.flush();
  223.        
  224.         buffer = new byte[8192];
  225.         in.read(buffer);
  226.         reply = new String(buffer);
  227.         //We have the header, let's parse it line by line.
  228.         split = reply.split("\r\n\r\n");
  229.         for(String line: split[0].split("\r\n"))
  230.         {
  231.           //If the line describes the length of the HTTP body (the html page) save the length.
  232.           //We need it later to read the buffer.
  233.           if(line.startsWith("Content-Length: "))
  234.             cl = Integer.parseInt(line.substring(16));
  235.           //Else if the line is used to send us the cookie with the session hash, save that session hash
  236.           //So we can authenticate us later.
  237.           else if(line.startsWith("Set-Cookie: xf_session="))
  238.             session = line.substring(23, line.indexOf(";"));
  239.         }
  240.         //Now read the body. As we have the length let's make a buffer with it and read from the socket till we have all.
  241.         //The buffer may be bigger than it needs to be, I was to lazy to convert to the 8kb header read. :P
  242.         got = cl - split[1].length();
  243.         buffer = new byte[cl];
  244.         while(got > 0)
  245.         {
  246.           t = cl - got;
  247.           got -= in.read(buffer, t, cl - t);
  248.         }
  249.         //We need the attachment hash and the xfToken from that page or we will cause a security exception from xenforo later
  250.         split = new String(buffer).split("<input type=\"hidden\" name=\"attachment_hash\" value=\"");
  251.         if(split.length < 2)
  252.           continue;
  253.         String hash = split[1];
  254.         split = hash.split("<input type=\"hidden\" name=\"_xfToken\" value=\"");
  255.         if(split.length < 2)
  256.           continue;
  257.         token = split[1];
  258.         hash = hash.substring(0, hash.indexOf("\""));
  259.         token = token.substring(0, token.indexOf("\""));
  260.         //And we close...
  261.         in.close();
  262.         out.close();
  263.         s.close();
  264.        
  265.         s = new Socket();
  266.         s.connect(new InetSocketAddress(host, 80));
  267.         in = s.getInputStream();
  268.         out = s.getOutputStream();
  269.         //We need postdata again, Iwon't get into detail this time, but it contains the thread title, the message, the attachment hash, the xfToken and other things to make a valid thread.
  270.         postdata = "title=I%20told%20you%20to%20NOT%20DELETE%20MY%20POSTS&message=Lynax%20is%20the%20liar%2C%20not%20me.%20He%20is%20unable%20to%20give%20the%20simplest%20information%20like%20paypal%20addresses%20and%20transaction%20numbers%20which%20would%20prove%20his%20payment!%0A%0AWe%20do%20not%20forgive.%0AWe%20do%20not%20forget.%0AExpect%20us.&_xfRelativeResolver=http%3A%2F%2Fturkcraft.net%2Findex.php%3Fforums%2Fsurvival-mod-sunucular%25C4%25B1.20%2Fcreate-thread&attachment_hash="+hash+"&watch_thread=1&watch_thread_email=1&watch_thread_state=1&poll%5Bquestion%5D=&poll%5Bresponses%5D%5B%5D=&poll%5Bresponses%5D%5B%5D=&_xfToken="+token+"&_xfRequestUri=%2Findex.php%3Fforums%2Fsurvival-mod-sunucular%25C4%25B1.20%2Fcreate-thread&_xfNoRedirect=1&_xfResponseType=json\r\n";
  271.         sb = new StringBuilder("POST /index.php?forums/survival-mod-sunucular%C4%B1.20/add-thread HTTP/1.1\r\nHost: ").append(host).append("\r\nConnection: close\r\nUser-Agent: V10Bot\r\nContent-Length: ").append(postdata.length()).append("\r\nContent-Type: application/x-www-form-urlencoded\r\nReferer: /index.php?forums/survival-mod-sunucular%C4%B1.20/create-thread\r\n");
  272.         if(session != null)
  273.           sb.append("Cookie: xf_session="+session+"\r\n");
  274.         sb.append("\r\n").append(postdata);
  275.         out.write(sb.toString().getBytes());
  276.         //No need to read the data here. Xenforo saved the post before it replies any data. Also we don't flush/close for better performance. ^^
  277.        
  278.         //Allright, let's start from the beginning of the loop.
  279.       }
  280.     }
  281.     catch(IOException e)
  282.     {
  283.       e.printStackTrace();
  284.       return;
  285.     }
  286.   }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement