Module: BScan

Includes:
BscanHelper, Mailer
Defined in:
lib/bscan.rb

Constant Summary

Constants included from Mailer

Mailer::ATTACHMENT, Mailer::BODY, Mailer::HEADERS, Mailer::MARKER, Mailer::MESSAGE, Mailer::SIMPLE

Instance Attribute Summary

Attributes included from BscanHelper

#activity, #bscan_config, #modules_only, #run_proxy, #stat, #url_prefs

Instance Method Summary collapse

Methods included from Mailer

#pv, #send_email, #sv, #ver, #zip_encode

Methods included from BscanHelper

#Log, #add_multi, #add_spider_headers, #copy_vars, #do_active_scan, #do_passive_scan, #do_scan, #esc, #excluded?, #exit_suite, #get_action, #get_action_params, #get_bool_prop, #get_par, #get_rr, #get_url_host_port, #hdr_nbr, #include_in_scope, #init_internals, #init_stat, #is_in_scope, #is_module_static, #load_config, #log, #make_request_socket, #msg_end, #open_in_path, #prop, #read_response_socket, #redirect, #redirect?, #replace_params, #run_modules, #save_config, #search_path, #search_path_file, #send_only, #send_req, #send_to_spider, #set_len, #to_map, #verify_response, #write_issue_state

Instance Method Details

#evt_application_closingObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/bscan.rb', line 183

def evt_application_closing
  begin
    Log 2,"BScan.evt_application_closing #{@bscan_config['bscan.smtp.server']} #{@bscan_config['bscan.smtp.to']}"
    if not @load_config_flag 
      @istream.close if @istream
      @stat['end_time'] = Time.now.strftime("%Y-%m-%d %H:%M:%S") 
      send_email if @bscan_config['bscan.smtp.server'] and @bscan_config['bscan.smtp.to']
      @log.close if @log
#        exit_suite false
    end
    @load_config_flag = false
  rescue Exception => e
    Log 0, "BScan.evt_application_closing Exception: #{e.message}"
    Log 0, e.backtrace.join("\n")
 end
end

#evt_commandline_args(args) ⇒ Object



39
40
41
# File 'lib/bscan.rb', line 39

def evt_commandline_args args
    init_internals JSON.parse(args[0])
end

#evt_extender_init(ext) ⇒ Object



34
35
36
37
# File 'lib/bscan.rb', line 34

def evt_extender_init ext
  @burp_extender = ext
  Log 2, "#"*70, "# BScan.evt_extender_init EXT: #{ext.toString}", "#"*70
end

#evt_get_action_nameObject



102
103
104
105
106
107
108
109
# File 'lib/bscan.rb', line 102

def evt_get_action_name
  Log 2, "# BScan.evt_get_action_name"
  ret = ''
  acts = get_action
  Log 2, "# BScan.evt_get_action_name"
  ret = acts.join('+') if acts.size > 0
  ret 
end

#evt_http_message(tool_name, is_request, message_info) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/bscan.rb', line 133

def evt_http_message(tool_name, is_request, message_info)
#    super(tool_name, is_request, message_info)
  begin
    Log 3, "BScan.evt_http_message #{tool_name} #{Java.burp.IBurpExtenderCallbacks.TOOL_SPIDER} #{is_request} #{message_info.to_s}"
    if tool_name == Java.burp.IBurpExtenderCallbacks.TOOL_SCANNER or tool_name == 'scanner'
      if is_request
        Log 2, "#"*70, "# BScan.evt_http_message REQUEST: #{message_info.url.toString}, tool: #{tool_name}", "#"*70
        Log 3, "BScan.evt_http_message #{message_info.getRequest()}", ""
      else
        Log 2, "# BScan.evt_http_message RESPONSE CODE: #{message_info.statusCode}", ""
        Log 3, "BScan.evt_http_message #{message_info.getResponse()}", ""
      end
    end
    if tool_name == Java.burp.IBurpExtenderCallbacks.TOOL_SPIDER or tool_name == 'spider'\
            or tool_name == Java.burp.IBurpExtenderCallbacks.TOOL_PROXY or tool_name == 'proxy'
     if not is_request
        @activity[0] = true
        https = message_info.getProtocol() == "https" ? true : false
        Log 2, "BScan.evt_http_message Passively scanning: #{message_info.url.to_string}"
        do_passive_scan(message_info.getHost(), message_info.getPort(), https, message_info.getRequest(),  message_info.getResponse())
        if (is_in_scope(message_info.url) and not excluded? message_info.url)
          Log 2, "BScan.evt_http_message Actively scanning: #{message_info.url.to_string}"
#          isqi = do_active_scan(message_info.getHost(), message_info.getPort(), https, message_info.getRequest(), [])
          isqi = do_active_scan(message_info.getHost(), message_info.getPort(), https, message_info.getRequest())
          @queue.push(isqi)
          run_modules self, message_info
        end
     end
    end
   rescue Exception => e
    Log 0, "BScan.evt_http_message Exception: #{e.message}"
    Log 0, e.backtrace.join("\n")
   end  
end

#evt_perfrom_action(req) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/bscan.rb', line 111

def evt_perfrom_action  req
  acts = get_action
  pars = get_action_params
  Log 3, "BScan.evt_perform_action #{acts[0]} #{pars[0]}"
  msg_info = @burp_cb.getHelpers().analyzeRequest(req)
  msg_body = req.getRequest()[msg_info.getBodyOffset()..-1]
  i = 0
  acts.each do |a|
    par = pars[i] 
    i++
    case a
    when 'add_header'
      Log 3, "BScan.evt_perform_action #{msg_info.getRequest().to_s} #{a} #{par}"
      hdrs = msg_info.getHeaders()
      hdrs.add(par.to_java(:string))
      msg = @burp_cb.getHelpers().buildHttpMessage(hdrs, msg_body)
      @burp_cb.issueAlert(@burp_cb.getHelpers().bytesToString(msg))
      req.setRequest(msg)
    end
  end
end

#evt_register_callbacks(cb) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/bscan.rb', line 200

def evt_register_callbacks cb
  #    super(cb)
  @burp_cb = cb
  @burp ||= self
  begin
    Log 2, "="*30, "BScan.evt_register_callbacks registring, log = #{@cmd_params['logfile']}  ", "="*30 

        
    @queue ||= []
    @inactivity_to = @bscan_config['bscan.inactivity_to']
    @inactivity_to ||= '30'
    @max_proxy_time ||= '30'
    @inactivity_to = @inactivity_to.to_i
    @max_proxy_time = @max_proxy_time.to_i
    # Will exit if @activity = 0  and @queue is empty two times
    if @monitor == nil
      Log 2, "="*30, "BScan.evt_register_callbacks Starting Monitor", "="*30 
      @monitor = Thread.new(@queue, @activity) {|q,a|
        cnt=0;
        while (true) 
          sleep(@inactivity_to/2)
          @max_proxy_time -= @inactivity_to/2
                  
          q.delete_if {|e| e.getPercentageComplete() == 100}
          if q.length == 0 and not a[0] 
            cnt += 1
          else
            cnt = 0
#            Log 2, "BScan.evt_register_callbacks QUEUE: #{q.length}, Activity: #{a[0]}"
          end
          if cnt > 1 
            Log 2, "BScan.evt_register_callbacks Scanning complete"
            if @run_proxy
              gen_report 
            end
            exit_suite false
            break
          end
          Log 2, "BScan.evt_register_callbacks monitor #{@run_proxy} #{@max_proxy_time}"
          a[0] = false
        end
      }
    end  
   
    Log 2, '='*30, 'BScan.evt_register_callbacks Params', '='*30
    params = save_config
 #     params.each_pair {|k,v| Log 2,"#{k}:#{v}:#{v.class}"} # if k =~ /^(scanner|spider)/}
    params['target.scopeinclude0']='**empty**' # burp can store the previous scope somehow, thus need to clean
    @burp_config.each_pair do |k,v|
      params[k] = v if params[k]
    end 
    add_spider_headers(params) 
    if @cmd_params['burp_config_only']
      load_config params, @bscan_config['bscan.burp_defaults']
      exit_suite false
    else 
      load_config @burp_config
    end
      
    params.each_pair {|k,v| Log 2,"#{k}:#{v}"} # if k =~ /^(scanner|spider)/}
   
#      run_modules
#      exit_suite

    urls = @bscan_config['bscan.url']
    Log 2, "BScan.evt_register_callbacks urls: #{@modules_only} #{urls}"
    
    
    if not urls and not @run_proxy
     Log 0, "BScan.evt_register_callbacks No URL's provided in config. Use bscan.url param. Multiple entries are OK"
     exit_suite false
    end
    
    urls = [urls] if urls and not urls.kind_of?(Array)
    
    if urls
      urls.each do |u|
        Log 2, "BScan.evt_register_callbacks checking url: #{u}"
        if not is_in_scope(u)
          Log 2, "BScan.evt_register_callbacks including url: #{u}"
          include_in_scope(u)
        end
        Log 2, "BScan.evt_register_callbacks Sending to spider: #{u}"
        send_to_spider(u)
      end
    end
end
rescue Exception => e
      Log 0, "BScan.evt_register_callbacks Exception: #{e.message}"
      Log 0,  e.backtrace.join("\n")
      exit_suite
end

#evt_scan_issue(issue) ⇒ Object



169
170
171
172
173
# File 'lib/bscan.rb', line 169

def evt_scan_issue issue
#    super(issue)
#    Buby::HttpRequestResponseHelper.implant(issue.http_messages)
  write_issue_state issue
end

#gen_reportObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bscan.rb', line 75

def gen_report 
  issues = nil
  if not @url_prefs
    issues = @burp_cb.getScanIssues(nil)
    file = "report.xml"
    file = @bscan_config['bscan.report_def_name'] if @bscan_config['bscan.report_def_name']
    file = File.expand_path(File.join(@issues, [file])) if @issues
    gen_report_params(issues,file)
  else
    i=1
    @url_prefs.each do |url_name| 
      u, n = url_name.split('|')
      n = 'pref' + i.to_s if not n
      i += 1
      issues = @burp_cb.getScanIssues(u)
      n = File.expand_path(File.join(@issues, [n])) if @issues
      gen_report_params(issues,n)  
    end
  end
end

#gen_report_params(issues, file) ⇒ Object



95
96
97
98
99
100
# File 'lib/bscan.rb', line 95

def gen_report_params issues, file
   io = Java.java.io.File.new(file.to_java(:string))
   filtered = get_filtered_issues(issues)
   @burp_cb.generateScanReport("XML".to_java(:string), filtered, io)
   Log 2, "#"*70, "# BScan.gen_report: #{file} #{io.to_s} #{filtered.to_s}", "#"*70
end

#get_filtered_issues(issues) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bscan.rb', line 43

def get_filtered_issues issues
  
  info = get_bool_prop('bscan.ignore_info')
  low = get_bool_prop('bscan.ignore_low')
  medium = get_bool_prop('bscan.ignore_medium') 
  high = get_bool_prop('bscan.ignore_high')
  
  return issues if (not (info or low or medium or high))
  
  filtered = []  
  issues.each do |i|
#      puts "=============ISSUE==================="
#      puts i
#      puts i.getSeverity()
#      i.getClass().methods.each do |m|
#        p m.getName()
#      end
#      puts
    case i.getSeverity()
      when 'Information'
        filtered.push(i) if not info
      when 'Low'
        filtered.push(i) if not low
      when 'Medium'
        filtered.push(i) if not medium
      when 'High'
        filtered.push(i) if not high
      end
  end
  return filtered
end

#start_burp(args, arg_strs) ⇒ Object



27
28
29
30
31
32
# File 'lib/bscan.rb', line 27

def start_burp args, arg_strs
  Java.burp.BurpExtender.handler = self
  init_internals JSON.parse(args[0])
  Java.burp.StartBurp.main(arg_strs.to_java(:string))
  
end

#sync_save_state(issue) ⇒ Object



175
176
177
178
179
180
# File 'lib/bscan.rb', line 175

def sync_save_state issue
  @sync_state_mutex ||= Mutex.new
  @sync_state_mutex.synchronize {
    save_state(@sstream) if @sstream and issue.severity =~ /(High)/
  }
end