class Railroader::CheckExecute

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 #{params}”) exec(params) `unlink #{params[:something}`

Constants

SAFE_VALUES
SHELLWORDS
SHELL_ESCAPES

Public Instance Methods

check_for_backticks(tracker) click to toggle source

Looks for calls using backticks such as

`rm -rf #{params}`

# File lib/railroader/checks/check_execute.rb, line 106
def check_for_backticks tracker
  tracker.find_call(:target => nil, :method => :`).each do |result|
    process_backticks result
  end
end
check_open_calls() click to toggle source
# File lib/railroader/checks/check_execute.rb, line 79
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 => :high
    end
  end
end
dangerous?(exp) click to toggle source

This method expects a :dstr or :evstr node

# File lib/railroader/checks/check_execute.rb, line 136
def dangerous? exp
  exp.each_sexp do |e|
    if call? e and e.method == :to_s
      e = e.target
    end

    next if node_type? e, :lit, :str
    next if SAFE_VALUES.include? e
    next if shell_escape? e

    if node_type? e, :if
      # If we're in a conditional, evaluate the `then` and `else` clauses to
      # see if they're dangerous.
      if res = dangerous?(e.values[1..-1])
        return res
      end
    elsif node_type? e, :or, :evstr, :dstr
      if res = dangerous?(e)
        return res
      end
    else
      return e
    end
  end

  false
end
dangerous_interp?(exp) click to toggle source
# File lib/railroader/checks/check_execute.rb, line 164
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) click to toggle source
# File lib/railroader/checks/check_execute.rb, line 92
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) click to toggle source

Processes backticks.

# File lib/railroader/checks/check_execute.rb, line 113
def process_backticks result
  return unless original? result

  exp = result[:call]

  if input = include_user_input?(exp)
    confidence = :high
  elsif input = dangerous?(exp)
    confidence = :medium
  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) click to toggle source

Processes results from Tracker#find_call.

# File lib/railroader/checks/check_execute.rb, line 45
def process_result result
  call = result[:call]
  args = call.arglist
  first_arg = call.first_arg

  case call.method
  when :popen
    unless array? first_arg
      failure = include_user_input?(args) || dangerous_interp?(args)
    end
  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 original? result

    if failure.type == :interp # Not from user input
      confidence = :medium
    else
      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_check() click to toggle source

Check models, controllers, and views for command injection.

# File lib/railroader/checks/check_execute.rb, line 26
def run_check
  Railroader.debug "Finding system calls using ``"
  check_for_backticks tracker

  check_open_calls

  Railroader.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], :nested => true

  Railroader.debug "Processing system calls"
  calls.each do |result|
    process_result result
  end
end
shell_escape?(exp) click to toggle source
# File lib/railroader/checks/check_execute.rb, line 178
def shell_escape? exp
  return false unless call? exp

  if exp.target == SHELLWORDS and SHELL_ESCAPES.include? exp.method
    true
  elsif exp.method == :shelljoin
    true
  else
    false
  end
end