Class: Brakeman::CheckWeakHash

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

Constant Summary collapse

DIGEST_CALLS =
[:base64digest, :digest, :hexdigest, :new]

Constants inherited from BaseCheck

BaseCheck::CONFIDENCE

Constants included from Util

Util::ALL_COOKIES, Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_COOKIES, 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_cookies, #process_default, #process_dstr, #process_if, #process_params

Methods included from Util

#array?, #block?, #call?, #camelize, #class_name, #constant?, #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

#current_file_name, #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

#hashing_password?(call) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/brakeman/checks/check_weak_hash.rb', line 109

def hashing_password? call
  call.each_arg do |arg|
    @has_password = false

    process arg

    if @has_password
      return @has_password
    end
  end

  nil
end

#process_call(exp) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/brakeman/checks/check_weak_hash.rb', line 123

def process_call exp
  if exp.method == :password
    @has_password = exp
  else
    process_default exp
  end

  exp
end

#process_hash_result(result) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/brakeman/checks/check_weak_hash.rb', line 24

def process_hash_result result
  return unless original? result

  input = nil
  call = result[:call]

  if DIGEST_CALLS.include? call.method
    if input = user_input_as_arg?(call)
      confidence = :high
    elsif input = hashing_password?(call)
      confidence = :high
    else
      confidence = :medium
    end
  else
    confidence = :medium
  end


  alg = case call.target.last
        when :MD5
         " (MD5)"
         when :SHA1
          " (SHA1)"
        else
          ""
        end

  warn :result => result,
    :warning_type => "Weak Hash",
    :warning_code => :weak_hash_digest,
    :message => "Weak hashing algorithm#{alg} used",
    :confidence => confidence,
    :user_input => input
end

#process_hmac_result(result) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/brakeman/checks/check_weak_hash.rb', line 60

def process_hmac_result result
  return unless original? result

  call = result[:call]

  alg = case call.third_arg.last
         when :MD5
           'MD5'
         when :SHA1
           'SHA1'
         else
           return
         end

  warn :result => result,
    :warning_type => "Weak Hash",
    :warning_code => :weak_hash_hmac,
    :message => "Weak hashing algorithm (#{alg}) used in HMAC",
    :confidence => :medium
end

#process_ivar(exp) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/brakeman/checks/check_weak_hash.rb', line 133

def process_ivar exp
  if exp.value == :@password
    @has_password = exp
  end

  exp
end

#process_lvar(exp) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/brakeman/checks/check_weak_hash.rb', line 141

def process_lvar exp
  if exp.value == :password
    @has_password = exp
  end

  exp
end

#process_openssl_result(result) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/brakeman/checks/check_weak_hash.rb', line 81

def process_openssl_result result
  return unless original? result

  arg = result[:call].first_arg

  if string? arg
    alg = arg.value.upcase

    if alg == 'MD5' or alg == 'SHA1'
      warn :result => result,
        :warning_type => "Weak Hash",
        :warning_code => :weak_hash_digest,
        :message => "Weak hashing algorithm (#{alg}) used",
        :confidence => :medium
    end
  end
end

#run_checkObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/brakeman/checks/check_weak_hash.rb', line 10

def run_check
  tracker.find_call(:targets => [:'Digest::MD5', :'Digest::SHA1', :'OpenSSL::Digest::MD5', :'OpenSSL::Digest::SHA1'], :nested => true).each do |result|
    process_hash_result result
  end

  tracker.find_call(:target => :'Digest::HMAC', :methods => [:new, :hexdigest], :nested => true).each do |result|
    process_hmac_result result
  end

  tracker.find_call(:targets => [:'OpenSSL::Digest::Digest', :'OpenSSL::Digest'], :method => :new).each do |result|
    process_openssl_result result
  end
end

#user_input_as_arg?(call) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
# File 'lib/brakeman/checks/check_weak_hash.rb', line 99

def user_input_as_arg? call
  call.each_arg do |arg|
    if input = include_user_input?(arg)
      return input
    end
  end

  nil
end