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_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_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

#hashing_password?(call) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/brakeman/checks/check_weak_hash.rb', line 112

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



126
127
128
129
130
131
132
133
134
# File 'lib/brakeman/checks/check_weak_hash.rb', line 126

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
59
# File 'lib/brakeman/checks/check_weak_hash.rb', line 24

def process_hash_result result
  return if duplicate? result
  add_result result

  input = nil
  call = result[:call]

  if DIGEST_CALLS.include? call.method
    if input = user_input_as_arg?(call)
      confidence = CONFIDENCE[:high]
    elsif input = hashing_password?(call)
      confidence = CONFIDENCE[:high]
    else
      confidence = CONFIDENCE[:med]
    end
  else
    confidence = CONFIDENCE[:med]
  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



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

def process_hmac_result result
  return if duplicate? result
  add_result 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 => CONFIDENCE[:med]
end

#process_ivar(exp) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/brakeman/checks/check_weak_hash.rb', line 136

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

  exp
end

#process_lvar(exp) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/brakeman/checks/check_weak_hash.rb', line 144

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

  exp
end

#process_openssl_result(result) ⇒ Object



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

def process_openssl_result result
  return if duplicate? result
  add_result 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 => CONFIDENCE[:med]
    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)


102
103
104
105
106
107
108
109
110
# File 'lib/brakeman/checks/check_weak_hash.rb', line 102

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

  nil
end