今天用笔记里写好msfvenom例子开监听端口,竟然直接上线了个meterpreter,分析发现,竟然是大半年前某个没攻击成功的洞突然回光返照了,有时候幸福来的就是这么突然。
    为了不漏掉任何一个meterpreter,于是决定搞个webhook时时提醒幸福的来临,照着网上的案例改了个plugin。
    1、将插件dingtalk.rb拷贝到msf的plugins目录。
    dingtalk.rb

    1. module Msf
    2. class Plugin::SessionNotifier < Msf::Plugin
    3. include Msf::SessionEvent
    4. class Exception < ::RuntimeError ; end
    5. class SessionNotifierCommandDispatcher
    6. include Msf::Ui::Console::CommandDispatcher
    7. attr_reader :dingtalk_api
    8. def name
    9. 'Dingtalk'
    10. end
    11. def commands
    12. {
    13. 'set_session_dingtalk_api' => 'Set set_session_dingtalk_api',
    14. 'save_session_dingtalk_settings' => 'Save all the session notifier settings to framework',
    15. 'start_session_dingtalk' => 'Start notifying sessions',
    16. 'stop_session_dingtalk' => 'Stop notifying sessions',
    17. 'restart_session_dingtalk' => 'Restart notifying sessions'
    18. }
    19. end
    20. def initialize(driver)
    21. super(driver)
    22. load_settings_from_config
    23. end
    24. def cmd_set_session_dingtalk_api(*args)
    25. @dingtalk_api = args[0]
    26. end
    27. def cmd_save_session_dingtalk_settings(*args)
    28. save_settings_to_config
    29. print_status("Session Notifier settings saved in config file.")
    30. end
    31. def cmd_start_session_dingtalk(*args)
    32. if is_session_notifier_subscribed?
    33. print_status('You already have an active session notifier.')
    34. return
    35. end
    36. begin
    37. self.framework.events.add_session_subscriber(self)
    38. print_status("dingtalk notification started.")
    39. rescue Msf::Plugin::SessionNotifier::Exception, Rex::Proto::Sms::Exception => e
    40. print_error(e.message)
    41. end
    42. end
    43. def cmd_stop_session_dingtalk(*args)
    44. self.framework.events.remove_session_subscriber(self)
    45. print_status("dingtalk Session notification stopped.")
    46. end
    47. def cmd_restart_session_dingtalk(*args)
    48. cmd_stop_session_dingtalk(args)
    49. cmd_start_session_dingtalk(args)
    50. end
    51. def send_text_to_dingtalk(session,dingtalk_webhook)
    52. # https://ding-doc.dingtalk.com/doc# /serverapi2/qf2nxq/9e91d73c
    53. uri_parser = URI.parse(dingtalk_webhook)
    54. markdown_text = "## You have a new #{session.type} session!\n\n" \
    55. "**platform** : #{session.platform}\n\n" \
    56. "**tunnel** : #{session.tunnel_to_s}\n\n" \
    57. "**arch** : #{session.arch}\n\n" \
    58. "**info** : > #{session.info ? session.info.to_s : nil}"
    59. json_post_data = JSON.pretty_generate({
    60. msgtype: 'markdown',
    61. markdown: { title: 'Session Notifier', text: markdown_text }
    62. })
    63. http = Net::HTTP.new(uri_parser.host, uri_parser.port)
    64. http.use_ssl = true
    65. request = Net::HTTP::Post.new(uri_parser.request_uri)
    66. request.content_type = 'application/json'
    67. request.body = json_post_data
    68. res = http.request(request)
    69. body = JSON.parse(res.body)
    70. print_status((body['errcode'] == 0) ? 'Session notified to DingTalk.' : 'Failed to send notification.')
    71. end
    72. def on_session_open(session)
    73. subject = "You have a new #{session.type} session!"
    74. msg = "#{session.tunnel_peer} (#{session.session_host}) #{session.info ? "\"#{session.info.to_s}\"" : nil}"
    75. send_text_to_dingtalk(session,self.dingtalk_api)
    76. end
    77. private
    78. def save_settings_to_config
    79. config_file = Msf::Config.config_file
    80. ini = Rex::Parser::Ini.new(config_file)
    81. ini.add_group(name) unless ini[name]
    82. ini[name]['dingtalk_api'] = self.dingtalk_api
    83. ini.to_file(config_file)
    84. end
    85. def load_settings_from_config
    86. config_file = Msf::Config.config_file
    87. ini = Rex::Parser::Ini.new(config_file)
    88. group = ini[name]
    89. if group
    90. @dingtalk_api = group['dingtalk_api'] if group['dingtalk_api']
    91. print_status('Session Notifier settings loaded from config file.')
    92. end
    93. end
    94. def is_session_notifier_subscribed?
    95. subscribers = framework.events.instance_variable_get(:@session_event_subscribers).collect { |s| s.class }
    96. subscribers.include?(self.class)
    97. end
    98. def validate_settings!
    99. if self.dingtalk_api.nil?
    100. raise Msf::Plugin::SessionNotifier::Exception, "All Session Notifier's settings must be configured."
    101. end
    102. end
    103. end
    104. def name
    105. 'Dingtalk'
    106. end
    107. def initialize(framework, opts)
    108. super
    109. add_console_dispatcher(SessionNotifierCommandDispatcher)
    110. end
    111. def cleanup
    112. remove_console_dispatcher(name)
    113. end
    114. def name
    115. 'Dingtalk'
    116. end
    117. def desc
    118. 'This plugin notifies you a new session via SMS.'
    119. end
    120. end
    121. end

    目录大概会在这些位置:

    1. /usr/share/metasploit-framework/plugin/ kali
    2. /opt/metasploit-framework/embedded/framework/plugins/ apt安装)

    image.png
    2、设置钉钉机器人
    首先新建群
    image.png
    添加群助手
    image.png
    设置机器人并添加触发关键词session,发送的字符串中带有这个关键词就会触发消息。
    image.png
    3、运行msfconsole后加载插件,设置dingtalk_api

    1. load dingtalk
    2. set_session_dingtalk_api https://oapi.dingtalk.com/robot/send?access_token=42a9ddd318d7b21e3f937bec57432bdb2a************fb547260f88f70
    3. start_session_dingtalk

    image.png
    如果不知道命令的话,可在load dingtalk后执行help查看命令提示
    image.png
    4、正常使用msf,反弹meterpreter回话时便会触发钉钉消息

    1. use exploit/multi/handler
    2. set payload linux/x86/meterpreter/reverse_tcp
    3. set LHOST 192.168.153.128
    4. set LPORT 11223
    5. exploit -z -j

    image.png

    参考:
    https://github.com/rapid7/metasploit-framework/pull/13571
    https://mp.weixin.qq.com/s/4I6FzuuRCTULDgqV-0QSJA