Class: Brakeman::CheckExecute

Inherits:
BaseCheck show all
Defined in:
lib/brakeman/checks/check_execute.rb

Overview

Checks for string interpolation and parameters in calls to Kernel#system, Kernel#exec, Kernel#syscall, and inside backticks.

Examples of command injection vulnerabilities:

system(“rf -rf #:file”) exec(params) ‘unlink #params[:something`

Constant Summary collapse

SAFE_VALUES =
[s(:const, :RAILS_ROOT),
s(:call, s(:const, :Rails), :root),
s(:call, s(:const, :Rails), :env)]

Constants inherited from BaseCheck

BaseCheck::CONFIDENCE

Constants included from Util

Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_ENV, Util::REQUEST_PARAMETERS, Util::REQUEST_PARAMS, Util::SESSION, Util::SESSION_SEXP

Constants inherited from SexpProcessor

SexpProcessor::VERSION

Instance Attribute Summary

Attributes inherited from BaseCheck

#tracker, #warnings

Attributes inherited from SexpProcessor

#context, #env, #expected

Instance Method Summary collapse

Methods inherited from BaseCheck

#add_result, inherited, #initialize, #process_call, #process_cookies, #process_default, #process_dstr, #process_if, #process_params

Methods included from Util

#array?, #block?, #call?, #camelize, #class_name, #contains_class?, #context_for, #cookies?, #false?, #file_by_name, #file_for, #github_url, #hash?, #hash_access, #hash_insert, #hash_iterate, #integer?, #make_call, #node_type?, #number?, #params?, #pluralize, #rails_version, #regexp?, #relative_path, #request_env?, #request_value?, #result?, #set_env_defaults, #sexp?, #string?, #string_interp?, #symbol?, #table_to_csv, #template_path_to_name, #true?, #truncate_table, #underscore

Methods included from ProcessorHelper

#process_all, #process_all!, #process_call_args, #process_call_defn?, #process_class, #process_module

Methods inherited from SexpProcessor

#in_context, #initialize, #process, processors, #scope

Constructor Details

This class inherits a constructor from Brakeman::BaseCheck

Instance Method Details

#check_for_backticks(tracker) ⇒ Object

Looks for calls using backticks such as

‘rm -rf #:file`



98
99
100
101
102
# File 'lib/brakeman/checks/check_execute.rb', line 98

def check_for_backticks tracker
  tracker.find_call(:target => nil, :method => :`).each do |result|
    process_backticks result
  end
end

#check_open_callsObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/brakeman/checks/check_execute.rb', line 71

def check_open_calls
  tracker.find_call(:targets => [nil, :Kernel], :method => :open).each do |result|
    if match = dangerous_open_arg?(result[:call].first_arg)
      warn :result => result,
        :warning_type => "Command Injection",
        :warning_code => :command_injection,
        :message => "Possible command injection in open()",
        :user_input => match,
        :confidence => CONFIDENCE[:high]
    end
  end
end

#dangerous?(exp) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/brakeman/checks/check_execute.rb', line 129

def dangerous? exp
  exp.each_sexp do |e|
    next if node_type? e, :lit, :str
    next if SAFE_VALUES.include? e

    if call? e and e.method == :to_s
      e = e.target
    end

    if node_type? e, :or, :evstr, :dstr
      if res = dangerous?(e)
        return res
      end
    else
      return e
    end
  end

  false
end

#dangerous_interp?(exp) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/brakeman/checks/check_execute.rb', line 150

def dangerous_interp? exp
  match = include_interp? exp
  return unless match
  interp = match.match

  interp.each_sexp do |e|
    if res = dangerous?(e)
      return Match.new(:interp, res)
    end
  end

  false
end

#dangerous_open_arg?(exp) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
# File 'lib/brakeman/checks/check_execute.rb', line 84

def dangerous_open_arg? exp
  if string_interp? exp
    # Check for input at start of string
    exp[1] == "" and
      node_type? exp[2], :evstr and
      has_immediate_user_input? exp[2]
  else
    has_immediate_user_input? exp
  end
end

#process_backticks(result) ⇒ Object

Processes backticks.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/brakeman/checks/check_execute.rb', line 105

def process_backticks result
  return if duplicate? result

  add_result result

  exp = result[:call]

  if input = include_user_input?(exp)
    confidence = CONFIDENCE[:high]
  elsif input = dangerous?(exp)
    confidence = CONFIDENCE[:med]
  else
    return
  end

  warn :result => result,
    :warning_type => "Command Injection",
    :warning_code => :command_injection,
    :message => "Possible command injection",
    :code => exp,
    :user_input => input,
    :confidence => confidence
end

#process_result(result) ⇒ Object

Processes results from Tracker#find_call.



40
41
42
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
# File 'lib/brakeman/checks/check_execute.rb', line 40

def process_result result
  call = result[:call]
  args = call.arglist
  first_arg = call.first_arg

  case call.method
  when :system, :exec
    failure = include_user_input?(first_arg) || dangerous_interp?(first_arg)
  else
    failure = include_user_input?(args) || dangerous_interp?(args)
  end

  if failure and not duplicate? result
    add_result result

    if failure.type == :interp #Not from user input
      confidence = CONFIDENCE[:med]
    else
      confidence = CONFIDENCE[:high]
    end

    warn :result => result,
      :warning_type => "Command Injection",
      :warning_code => :command_injection,
      :message => "Possible command injection",
      :code => call,
      :user_input => failure,
      :confidence => confidence
  end
end

#run_checkObject

Check models, controllers, and views for command injection.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/brakeman/checks/check_execute.rb', line 21

def run_check
  Brakeman.debug "Finding system calls using ``"
  check_for_backticks tracker

  check_open_calls

  Brakeman.debug "Finding other system calls"
  calls = tracker.find_call :targets => [:IO, :Open3, :Kernel, :'POSIX::Spawn', :Process, nil],
    :methods => [:capture2, :capture2e, :capture3, :exec, :pipeline, :pipeline_r,
      :pipeline_rw, :pipeline_start, :pipeline_w, :popen, :popen2, :popen2e,
      :popen3, :spawn, :syscall, :system]

  Brakeman.debug "Processing system calls"
  calls.each do |result|
    process_result result
  end
end