PHP + Socket 发送http请求进而实现网站灌水

本质上实现组装http信息的请求行,头信息,主题信息,参考it自学网

cookie信息和http请求头有很大关系,注意把http请求头信息传递到函数里面

01-msg.php

<?php

require('./http.class.php');

$http = new Http('http://home.verycd.com/cp.php?ac=pm&op=send&touid=0&pmid=0');

$http->setHeader('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
$http->setHeader('Accept-Encoding: gzip, deflate');
$http->setHeader('Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3');
$http->setHeader('Connection: keep-alive');

$http->setHeader('Cookie: Hm_lvt_c7849bb40e146a37d411700cb7696e46=1371132935,1371551596,1371552570; CNZZDATA1479=cnzz_eid%3D2070887527-1371133011-http%253A%252F%252Fhome.verycd.com%26ntime%3D1371559611%26cnzz_a%3D27%26retime%3D1371559611556%26sin%3Dhttp%253A%252F%252Fwww.verycd.com%252Fi%252F17907141%252F%26ltime%3D1371559611556%26rtime%3D1; __utma=248211998.1671623420.1371133015.1371557486.1371559612.4; __utmz=248211998.1371552579.2.2.utmcsr=verycd.com|utmccn=(referral)|utmcmd=referral|utmcct=/; Hm_lpvt_c7849bb40e146a37d411700cb7696e46=1371554752; post_action=repost; BAIDU_CLB_REFER=http%3A%2F%2Fcwebmail.mail.163.com%2Fjs5%2Fread%2Freadhtml.jsp%3Fssid%3DI7zQECYCxLDKHPlnXYxQm9sNe5EPh1drYPvN26nZekk%253d%26mid%3D45%3A1tbiLRFAhFEFoLvtCAAAsS%26color%3D003399%26preventSetRead%3Don%26font%3D15; uchome_loginuser=http%E5%8D%8F%E8%AE%AE; uchome__refer=%2Fspace.php%3Fdo%3Dpm%26filter%3Dnewpm; __utmc=248211998; sid=9a48b201fb4d176a7188ef2a3560789651eb4766; member_id=17822047; member_name=http%E5%8D%8F%E8%AE%AE; mgroupId=93; pass_hash=e75f942862a5e927a2faffd2d2d582c9; rememberme=false; uchome_auth=4128oUJWyonkCXZvxM4sDRjcugSmt3L0r197Pq%2BPdf8fcLJsUiNZKBXjG0hYuPZSRK3XEs2FuRn1pH2cEFBNc1H0Im7x5A; uchome_sendmail=1; uchome_checkpm=1; __utmb=248211998.1.10.1371559612; dcm=1');

$http->setHeader('Referer: http://home.verycd.com/cp.php?ac=pm');
$http->setHeader('User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0');

$msg = array(
'formhash'=>'4f23e777',
'message'=>'我不是在灌水,请放行',
'pmsubmit'=>'true',
'pmsubmit_btn'=>'发送',
'refer'=>'http://home.verycd.com/space.php?do=pm&filter=privatepm',
'username'=>'http接收'
);

file_put_contents('./res.html',$http->post($msg));
echo 'ok';

http.class.php

<?php
/*
PHP+socket编程 发送HTTP请求

要求能 模拟下载,注册,登陆,批量发帖
*/

// http请求类的接口
interface Proto {
    // 连接url
    function conn($url);

    //发送get查询
    function get();

    // 发送post查询
    function post();

    // 关闭连接
    function close();
}

class Http implements Proto {

    const CRLF  = "\r\n";

    protected $errno = -1;
    protected $errstr = '';
    protected $response = '';

    protected $url = null;
    protected $version = 'HTTP/1.1';
    protected $fh = null;

    protected $line = array();
    protected $header = array();
    protected $body = array();

    public function __construct($url) {
        $this->conn($url);
        $this->setHeader('Host: ' . $this->url['host']);
    }

    // 此方法负责写请求行
    protected function setLine($method) {
        $this->line[0] = $method . ' ' . $this->url['path'] . '?' .$this->url['query'] . ' '. $this->version;
    }

    // 此方法负责写头信息
    public function setHeader($headerline) {
        $this->header[] = $headerline;
    }

    // 此方法负责写主体信息
    protected function setBody($body) {
         $this->body[] = http_build_query($body);
    }

    // 连接url
    public function conn($url) {
        $this->url = parse_url($url);
        // 判断端口
        if(!isset($this->url['port'])) {
            $this->url['port'] = 80;
        }

        // 判断query
        if(!isset($this->url['query'])) {
            $this->url['query'] = '';
        }

        $this->fh = fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3);
    }

    //构造get请求的数据
    public function get() {
        $this->setLine('GET');
        $this->request();
        return $this->response;
    }

    // 构造post查询的数据
    public function post($body = array()) {
        $this->setLine('POST');

        // 设计content-type
        $this->setHeader('Content-type: application/x-www-form-urlencoded');

        // 设计主体信息,比GET不一样的地方
        $this->setBody($body);

        // 计算content-length
        $this->setHeader('Content-length: ' . strlen($this->body[0]));

        $this->request();

        return $this->response;
    }

    // 真正请求
    public function request() {
        // 把请求行,头信息,实体信息 放在一个数组里,便于拼接
        $req = array_merge($this->line,$this->header,array(''),$this->body,array(''));
        //print_r($req);

        $req = implode(self::CRLF,$req);
        //echo $req; exit;

        fwrite($this->fh,$req);

        while(!feof($this->fh)) {
            $this->response .= fread($this->fh,1024);
        }

        $this->close(); // 关闭连接
    }

    // 关闭连接
    public function close() {
        fclose($this->fh);
    }

}

/*
$url = 'http://news.163.com/13/0613/09/9187CJ4C00014JB6.html';

$http = new Http($url);
echo $http->get();
*/
/*
set_time_limit(0);

$url = 'http://liangyue.net.cn/0523/?';

for($i=1;$i<100;$i++) {
    $str = str_shuffle('abcdefghijklmnopqrst0776656');
    $tit = substr($str,0,5);
    $con = substr($str,6,8);

    $http = new Http($url);
    $http->post(array('tit'=>$tit,'con'=>$con,'submit'=>'留言'));

    echo $tit,'-----------',$con,'<br />';

    usleep(2000);
}

*/
时间: 2024-07-31 11:49:20

PHP + Socket 发送http请求进而实现网站灌水的相关文章

socket发送http请求

转自:思齐-socket发送http请求 socket方式: $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); //socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array("sec"=>20, "usec"=>0)); socket_connect($socket, 'www.baidu.com', 80); //里面的换行代表 \r

使用socket发送http请求(GET/POST)

使用socket发送http请求(GET/POST) 下载LOFTER客户端 1.socket的原理 在相关文章中已经提及,不再赘述. 2.http头的格式 (1)请求行请求行由请求方法字段.URL字段和HTTP协议版本字段3个字段组成,它们用空格分隔.例如,GET /index.html HTTP/1.1.HTTP协议的请求方法有GET.POST.HEAD.PUT.DELETE.OPTIONS.TRACE.CONNECT.这里介绍最常用的GET方法和POST方法.GET:当客户端要从服务器中读

c/c++ socket发送http请求访问网站

这几天课比较少,校园网上网要认证才能上网,每次必须输入学号密码,为了方便,写了一个自动登录以及如果在线,登录自服务系统强制下线的小工具. 强制下线思路:获取sessionID---------->获取验证码图片------------>AspriseOCRLib识别验证码--------------->MD5加密.url Encode---------------->登录自服务系统---------------->强制下线--------------------->退出

如果使用socket发送http请求(并且编译成可以被lr调用的压力测试脚本)

#include  <unistd.h> #include  <sys/types.h>       /* basic system data types */ #include  <sys/socket.h>      /* basic socket definitions */ #include  <netinet/in.h>      /* sockaddr_in{} and other Internet defns */ #include  <

php socket 发送http请求

<?php /** * Created by PhpStorm. * User: Mch * Date: 7/8/18 * Time: 21:39 @func: parse_url http://www.tfjyzx.com/model-school.jsp?area=%E5%BC%80%E5%B0%81&school=%E5%BC%80%E5%B0%81%E5%B8%82%E7%AC%AC%E4%BA%94%E4%B8%AD%E5%AD%A6#%E5%AD%A6%E6%A0%A1%E7%A

perl6 Socket: 发送HTTP请求

sub MAIN(Str $host,Str $path, Int $port) { my $send = "GET $path HTTP/1.1\r\nHost: $host\r\n\r\n"; my $c = IO::Socket::INET.new(:host($host), :port($port)); $c.print: $send; while (my $buff = $c.recv(1024)) { say $buff.print; } $c.close; }

php 利用socket发送GET,POST请求

作为php程序员一定会接触http协议,也只有深入了解http协议,编程水平才会更进一步.最近我一直在学习php的关于http的编程,许多东西恍然大悟,受益匪浅.希望分享给大家.本文需要有一定http基础的开发者阅读. 今天给大家带来的是如何利用socket发送GET,POST请求.我借用燕十八老师封装好的一个Http类给进行说明. 在日常编程中相信很多人和我一样大部分时间是利用浏览器向服务器提出GET,POST请求,那么可否利用其它方式提出GET,POST请求呢?答案必然是肯定的.了解过HTT

java 常见几种发送http请求案例

1 import java.io.FileOutputStream; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.io.InputStreamReader; 5 import java.io.OutputStreamWriter; 6 import java.io.UnsupportedEncodingException; 7 import java.net.HttpURLConnection

Nodejs发送Post请求时出现socket hang up错误的解决办法

参考nodejs官网发送http post请求的方法,实现了一个模拟post提交的功能.实际使用时报socket hang up错误. 后来发现是请求头设置的问题,发送选项中需要加上headers字段信息(这个估计也和对方的服务器有关,对于不完成的post请求头,可能被丢弃了). 完整的代码如下(遇到类型问题的同学可以做个参考): var querystring = require('querystring') , http = require('http'); var data = query