perl 自动化登录QQ空间发说说

上一篇 / 下一篇  2013-08-29 21:46:11 / 个人分类:Perl

package Tencent::Encode;
use Digest::MD5 qw/md5_hex/;
@ISA=qw/decodeQQNum encodeQQpwd dec2hex hex2str Umd5/;

sub hex2str{
my ( $str ) = @_;
$str =~ tr/a-f/A-F/;
$str =~ s/[^0-9A-F]//g;
$str =~ s/(\w{2})/\\x\1/g;
$str = '$str="' . $str . '"';
eval $str;
$str;
}

sub dec2hex{
sprintf('%X',$_[0])
}

sub Umd5{
uc(md5_hex(@_[0]))
}

sub gettoken{
## QQ空间g_tk计算
my $str = $_[0];
my $hash = 5381;
foreach (0..length($str)-1){
  $hash += ($hash << 5) + ord(substr($str,$_,1));
}
$hash & 2147483647 
}

sub decodeQQNum{
my ($str) = @_;
$str =~ tr/a-z/A-Z/;
$str =~ s/\\X//g;
my @ch = split '',$str;
my $times = 0;
my $num=0;
my %dec = (
'0' => 0,
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
'6' => 6,
'7' => 7,
'8' => 8,
'9' => 9,
'A' => 10,
'B' => 11,
'C' => 12,
'D' => 13,
'E' => 14,
'F' => 15
);
my $c;
while ($c =pop @ch) {
$num = $num + 16 ** $times * $dec{$c};
$times++;
};
$num;
}

sub encodeQQpwd{
my ( $uid,$pwd,$verifycode ) = @_;
Umd5(Umd5(hex2str(Umd5($pwd)) . hex2str(substr('0000000000000000' . sprintf("%X",$uid),-16,16))) . $verifycode)
}



#################################################################################################

package Tencent::Qzone;
#use Tencent::Encode;

use HTTP::Request;
use HTTP::Cookies;
use LWP::UserAgent;
use Data::Dumper;

# 用来存 cookie
my $COOKIE_JAR  = HTTP::Cookies->new(
        file        => "./acookies.lwp",
        autosave    => 1,
hide_cookie2   => 1,
ignore_discard => 1
        );
$COOKIE_JAR>clear;

my ( $UA, $RES, $QQ ,$PWD ,$G_TK ) = ();
# 给处理 cookie 的对象放到 LWP::UserAgent 中来处理 cookie
$UA      = LWP::UserAgent->new( parse_head => 0 );
$UA->cookie_jar($COOKIE_JAR);
$UA->agent('Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)');

sub Login{
my ( $user,$password ) = @_;
$COOKIE_JAR->set_cookie( "0", '_qz_referrer', 'i.qq.com', '/', '.qq.com');
$RES = $UA->get('http://ui.ptlogin2.qq.com/cgi-bin/login?hide_title_bar=1&low_login=0&qlogin_auto_login=1&no_verifyimg=1&link_target=blank&appid=549000912&style=12&target=self&s_url=http%3A//qzs.qq.com/qzone/v5/loginsucc.html?para=izone&pt_qr_app=%CA%D6%BB%FAQQ%BF%D5%BC%E4&pt_qr_link=http%3A//z.qzone.com/download.html&self_regurl=http%3A//qzs.qq.com/qzone/v6/reg/index.html&pt_qr_help_link=http%3A//z.qzone.com/download.html');
( $sig ) = $RES->content() =~ /login_sig:"(.+?)"/;
$RES = $UA->get('http://check.ptlogin2.qq.com/check?regmaster=&uin=' . $user . '&appid=549000912&js_ver=10041&js_type=1&login_sig=' . $sig . '&u1=http%3A%2F%2Fqzs.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&r=0.21876225197437726');
my ($rec,$vcd,$uid) = ($RES->content() =~ /\'(.+)\',\'(.*?)\',\'(.+)\'/);

my $verifycode;    #验证码
print "\$rec ,\$vcd ,\$uid : $rec , $vcd , $uid \n";
if (defined($rec) && $rec==0){
$verifycode = $vcd;
## print "不需要输入验证码!\n";
}elsif($rec==1){
$verifycode = getVerifyCode($user ,$vcd);
print "验证码为:$verifycode \n";
}elsif($rec==2){
die "请输入正确的账户号码,支持QQ号、邮箱、手机号登录!" . $RES->content();
}else{
die "获取验证码错误!" . $RES->content();
};

$QQ = Tencent::Encode::decodeQQNum($uid);
$PWD = Tencent::Encode::encodeQQpwd($QQ,$password,$verifycode);
print "QQ:$QQ \nPassWord:$PWD \n";

($uikey) = $RES->headers_as_string =~ /uikey=(.+?);/;
($ptvfsession) = $RES->headers_as_string =~ /ptvfsession=(.+?);/;
($confirmuin) = $RES->headers_as_string =~ /confirmuin=(.+?);/;
$COOKIE_JAR->set_cookie( "0", 'pgv_pvid', '3050220276', '/', '.qq.com');
$COOKIE_JAR->set_cookie( "0", 'pgv_info', 'ssid=s8013483190', '/', '.qq.com');
    $COOKIE_JAR->set_cookie( "0", 'ptui_loginuin', $QQ, '/', '.qq.com');

my $url = 'http://ptlogin2.qq.com/login?u=' . $QQ . '&p=' . $PWD . '&verifycode=' . $verifycode . '&aid=549000912&u1=http%3A%2F%2Fqzs.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&h=1&ptredirect=0&ptlang=2052&from_ui=1&dumy=&low_login_enable=0&regmaster=&fp=loginerroralert&action=2-32-1377429503804&mibao_css=&t=1&g=1&js_ver=10041&js_type=1&login_sig=' . $sig;
my $req = HTTP::Request->new('GET',$url);
$req->header('Cookie' => "uikey=$uikey; ptvfsession=$ptvfsession; confirmuin=$confirmuin");
$RES = $UA->request($req);

if ($RES->content() =~ /\('0'/) {
print "登录成功!\n";
}else{
die "登录失败!\n" . $RES->content();
}

my ($skey) = $RES->headers_as_string =~ /skey=(.+?);/;
$G_TK =  Tencent::Encode::gettoken($skey);
print "skey:$skey , g_tk:$G_TK \n";

}


sub getVerifyCode(){
my ( $user, $vcd ) = @_;
my $v;
while(1){
$UA->mirror('http://captcha.qq.com/getimage?uin=' . $user . '&aid=549000912&cap_cd=' . $vcd . '&' . rand ,"verifycode.jpg");
system('explorer.exe "verifycode.jpg"');
print "请输入验证码:";
$v =  <STDIN>;
chomp $v;
last if length($v) eq 4;
}
$v;
}

sub SendMessage(){
my ( $msg ,$pic ) = @_;
$RES = $UA->post('http://taotao.qq.com/cgi-bin/emotion_cgi_publish_v6?g_tk=' . $G_TK,
        [
'code_version' => '1',
'con' => $msg,
'feedversion' => '1',
'format' => 'fs',
'hostuin' => $QQ ,
'qzreferrer' => 'http://user.qzone.qq.com/' . $QQ . '/infocenter',
'richtype' => '',
'richval' => '',
'special_url' => '',
'subrichtype' => '',
'syn_tweet_verson' => '1',
'to_sign' => '0',
'to_tweet' => '0',
'ugc_right' => '1',
'ver' => '1',
'who' => '1'
        ]
    );
print "发送QQ说说:$msg \n";
if ($RES->content() =~ /"code":0,/) {
print "发布成功!\n";
}else{
print "发布失败!\n";
}
}


&Login('QQ','Passwd');
&SendMessage("NEWTESTaa");
<STDIN>;


TAG: 外挂 Perl perl QQ登录 自动化

51bearer的个人空间 引用 删除 51bearer   /   2014-10-27 18:25:29
学习,不过好像现在不能登录了?
 

评分:0

我来说两句

Open Toolbar