Foro de elhacker.net

Programación => PHP => Mensaje iniciado por: lnvisible en 25 Diciembre 2011, 14:45 pm



Título: [Reto][PHP] Bot http
Publicado por: lnvisible en 25 Diciembre 2011, 14:45 pm
Hay un tipo de retos incrementales que son más productivos que los retos habituales.

http://en.wikipedia.org/wiki/Cutting_contest
http://en.wikipedia.org/wiki/Guitar_battle

Esto es un reto a ese estilo, consiste en ir añadiendo algo al bot para hacerlo más parecido a una persona en la navegación (con cookies, con referer, con todo eso), además para que sea más fácil indicarle los parámetros que tiene que rellenar (por ejemplo muchos formularios tienen campos hidden, podría rellenarlos él mismo), para que sea más fácil parsear el html después, para que sea más fácil usar proxies... el límite lo pone vuestra imaginación.

Creo que puede ser un reto interesante, entretenido, productivo y del que todos saquemos el divertirnos, un código interesante y aprender y hacer algo útil. Vosotros diréis.

Aquí tenéis un ejemplo de cómo podría empezarse, veremos si alguien continua o no, en unos días pondré una mejora si nadie se anima, y con eso espero que ya sí se anime alguien.

Código
  1. class PHPBot {
  2.  
  3.    function PHPBot (){
  4.      $this->ch = curl_init();
  5.      $ch = $this->ch;
  6.      curl_setopt($ch, CURLOPT_HEADER, 1); // Include headers in response or not
  7.      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return (don't print) answer of exec
  8.      curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12");
  9.      curl_setopt($ch, CURLOPT_AUTOREFERER, true); // Isn't this great?
  10.      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: */*', 'Accept-Language: en-us,en;q=0.5', 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'));      
  11.    }
  12.  
  13.    private function act($url, $params = false){
  14.      $ch = $this->ch;
  15.      curl_setopt($ch, CURLOPT_URL, $url);
  16.      if ($params != false){
  17.        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->myurlencode($params));
  18.        curl_setopt($ch, CURLOPT_POST, 1);
  19.      }
  20.      else
  21.        curl_setopt($ch, CURLOPT_POST, 0);
  22.      $r = curl_exec($ch);
  23.      return $r;
  24.    }
  25.  
  26.    private function myurlencode($dict){
  27.      $r = "";
  28.      foreach($dict as $key => $value)
  29.        $r = $r.urlencode($key).'='.urlencode($value).'&';
  30.      return $r;
  31.    }
  32.  
  33.    function get ($url){
  34.      return $this->act($url);
  35.    }
  36.  
  37.    function post($url, $params){
  38.      return $this->act($url, $params);
  39.    }
  40.  
  41.  }


Título: Re: [Reto][PHP] Bot http
Publicado por: #!drvy en 27 Diciembre 2011, 02:11 am
Añado una función para parsear un string, recoger todo lo que empiece con http (links) y devolverlo en un array.

Código
  1. class PHPBot {
  2.  
  3.    function PHPBot (){
  4.      $this->ch = curl_init();
  5.      $ch = $this->ch;
  6.      curl_setopt($ch, CURLOPT_HEADER, 1); // Include headers in response or not
  7.      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return (don't print) answer of exec
  8.      curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12");
  9.      curl_setopt($ch, CURLOPT_AUTOREFERER, true); // Isn't this great?
  10.      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: */*', 'Accept-Language: en-us,en;q=0.5', 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'));      
  11.    }
  12.  
  13.    private function act($url, $params = false){
  14.      $ch = $this->ch;
  15.      curl_setopt($ch, CURLOPT_URL, $url);
  16.      if ($params != false){
  17.        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->myurlencode($params));
  18.        curl_setopt($ch, CURLOPT_POST, 1);
  19.      }
  20.      else
  21.        curl_setopt($ch, CURLOPT_POST, 0);
  22.      $r = curl_exec($ch);
  23.      return $r;
  24.    }
  25.  
  26.    private function myurlencode($dict){
  27.      $r = "";
  28.      foreach($dict as $key => $value)
  29.        $r = $r.urlencode($key).'='.urlencode($value).'&';
  30.      return $r;
  31.    }
  32.  
  33.    private function get_urls($context){
  34. $query = '/http?\:\/\/[^\" ]+/i';
  35. preg_match_all($query,$context,$result);
  36. return $result[0];
  37.    }
  38.  
  39.    function get ($url){
  40.      return $this->act($url);
  41.    }
  42.  
  43.    function post($url, $params){
  44.      return $this->act($url, $params);
  45.    }
  46.  
  47.  }
  48.  
  49.  

Saludos


Título: Re: [Reto][PHP] Bot http
Publicado por: lnvisible en 27 Diciembre 2011, 03:28 am
Muy bien, pero en ocasiones los links pueden no ser absolutos sino relativos y aparecer sin http. Añado una función para recoger los links (absolutos y relativos) pero sólo cuando son enlaces a otras páginas, no los que son a imágenes ni otras cosas.

A ver si más gente se anima  :D

Código
  1. class PHPBot {
  2.  
  3.    function PHPBot (){
  4.      $this->ch = curl_init();
  5.      $ch = $this->ch;
  6.      curl_setopt($ch, CURLOPT_HEADER, 1); // Include headers in response or not
  7.      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return (don't print) answer of exec
  8.      curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12");
  9.      curl_setopt($ch, CURLOPT_AUTOREFERER, true); // Isn't this great?
  10.      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: */*', 'Accept-Language: en-us,en;q=0.5', 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'));      
  11.    }
  12.  
  13.    private function act($url, $params = false){
  14.      $ch = $this->ch;
  15.      curl_setopt($ch, CURLOPT_URL, $url);
  16.      if ($params != false){
  17.        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->myurlencode($params));
  18.        curl_setopt($ch, CURLOPT_POST, 1);
  19.      }
  20.      else
  21.        curl_setopt($ch, CURLOPT_POST, 0);
  22.      $r = curl_exec($ch);
  23.      return $r;
  24.    }
  25.  
  26.    private function myurlencode($dict){
  27.      $r = "";
  28.      foreach($dict as $key => $value)
  29.        $r = $r.urlencode($key).'='.urlencode($value).'&';
  30.      return $r;
  31.    }
  32.  
  33.    private function get_urls($context){
  34. $query = '/http?\:\/\/[^\" ]+/i';
  35. preg_match_all($query,$context,$result);
  36. return $result[0];
  37.    }
  38.  
  39.    function getLinks ($url){
  40.      $dom = new DOMDocument();
  41.      $p = $this->act($url);
  42.      $dom->loadHTML(mb_convert_encoding($p, 'utf-8'));
  43.      $links = $dom->getElementsByTagName('a');
  44.      foreach($links as $l)
  45.        $r[] = $l->getAttribute('href');
  46.      return $r;
  47.    }
  48.  
  49.  
  50.    function get ($url){
  51.      return $this->act($url);
  52.    }
  53.  
  54.    function post($url, $params){
  55.      return $this->act($url, $params);
  56.    }
  57.  
  58.  }
  59.  
  60.  


Título: Re: [Reto][PHP] Bot http
Publicado por: Pablo Videla en 23 Enero 2012, 14:50 pm
Podrían comentar el codigo para explicar bien lo que hace, hay gente como yo que sabe la sintaxis de php pero no conoce bien las funciones porque no suele programar en ese lenguaje xD , de antemano, gracias.

Espero no haber estorbado acá.


Título: Re: [Reto][PHP] Bot http
Publicado por: lnvisible en 24 Enero 2012, 20:10 pm
Puedes hacer click en las funciones que no conozcas y te lleva directamente a la documentación. :D