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::DIR_CONST, Util::LITERALS, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_COOKIES, Util::REQUEST_ENV, Util::REQUEST_PARAMETERS, Util::REQUEST_PARAMS, Util::REQUEST_REQUEST_PARAMETERS, Util::SAFE_LITERAL, Util::SESSION, Util::SESSION_SEXP, Util::SIMPLE_LITERALS

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_array, #process_cookies, #process_default, #process_dstr, #process_if, #process_params

Methods included from Messages

#msg, #msg_code, #msg_cve, #msg_file, #msg_input, #msg_lit, #msg_plain, #msg_version

Methods included from Util

#all_literals?, #array?, #block?, #call?, #camelize, #class_name, #constant?, #contains_class?, #cookies?, #dir_glob?, #false?, #hash?, #hash_access, #hash_insert, #hash_iterate, #hash_values, #integer?, #kwsplat?, #literal?, #make_call, #node_type?, #number?, #params?, #pluralize, #rails_version, #recurse_check?, #regexp?, #remove_kwsplat, #request_headers?, #request_value?, #result?, #safe_literal, #safe_literal?, #safe_literal_target?, #set_env_defaults, #sexp?, #simple_literal?, #string?, #string_interp?, #symbol?, #template_path_to_name, #true?, #underscore

Methods included from ProcessorHelper

#current_file, #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)


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

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



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

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

  message = msg("Weak hashing algorithm used")

  case call.target.last
  when :MD5
    message << ": " << msg_lit("MD5")
  when :SHA1
    message << ": " << msg_lit("SHA1")
  end

  warn :result => result,
    :warning_type => "Weak Hash",
    :warning_code => :weak_hash_digest,
    :message => message,
    :confidence => confidence,
    :user_input => input,
    :cwe_id => [328]
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
80
# File 'lib/brakeman/checks/check_weak_hash.rb', line 60

def process_hmac_result result
  return unless original? result

  call = result[:call]

  message = msg("Weak hashing algorithm used in HMAC")

  case call.third_arg.last
  when :MD5
    message << ": " << msg_lit("MD5")
  when :SHA1
    message << ": " << msg_lit("SHA1")
  end

  warn :result => result,
    :warning_type => "Weak Hash",
    :warning_code => :weak_hash_hmac,
    :message => message,
    :confidence => :medium,
    :cwe_id => [328]
end

#process_ivar(exp) ⇒ Object



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

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

  exp
end

#process_lvar(exp) ⇒ Object



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

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

  exp
end

#process_openssl_result(result) ⇒ Object



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

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 => msg("Weak hashing algorithm used: ", msg_lit(alg)),
        :confidence => :medium,
        :cwe_id => [328]
    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)


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

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

  nil
end