Class: Msf::RPC::RPC_Auth

Inherits:
RPC_Base show all
Defined in:
lib/msf/core/rpc/v10/rpc_auth.rb

Constant Summary collapse

@@loaded_sha2 =

Dynamic load test for SHA2 (needed for SHA512)

false

Instance Attribute Summary

Attributes inherited from RPC_Base

#framework, #service, #tokens, #users

Instance Method Summary collapse

Methods inherited from RPC_Base

#error, #initialize

Constructor Details

This class inherits a constructor from Msf::RPC::RPC_Base

Instance Method Details

#rpc_login_noauth(user, pass) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/msf/core/rpc/v10/rpc_auth.rb', line 14

def (user,pass)

  if not (user.kind_of?(::String) and pass.kind_of?(::String))
    error(401, "Login Failed")
  end

  # handle authentication here
  fail = true
  self.users.each do |u|
    if(u[0] == user and u[1] == pass)
      fail = false
      break
    end
  end

  fail = db_validate_auth(user,pass) if fail

  if fail
    # Introduce a random delay in the response to annoy brute forcers
    delay = [ ( rand(3000) / 1000.0 ), 0.50 ].max
    ::IO.select(nil, nil, nil, delay)

    # Send back a 401 denied error
    error(401, "Login Failed")
  end

  token = "TEMP" + Rex::Text.rand_text_alphanumeric(28)
  self.service.tokens[token] = [user, Time.now.to_i, Time.now.to_i]
  { "result" => "success", "token" => token }
end

#rpc_logout(token) ⇒ Object


45
46
47
48
49
50
51
52
53
54
# File 'lib/msf/core/rpc/v10/rpc_auth.rb', line 45

def rpc_logout(token)
  found = self.service.tokens[token]
  error("500", "Invalid Authentication Token") if not found
  error("500", "Permanent Authentication Token") if found[3] == true

  # Delete the token if its not marked as permanent
  self.service.tokens.delete(token)

  { "result" => "success" }
end

#rpc_token_add(token) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/msf/core/rpc/v10/rpc_auth.rb', line 69

def rpc_token_add(token)
  db = false
  begin
    if framework.db and framework.db.active
      t = ::Mdm::ApiKey.new
      t.token = token
      t.save!
      db = true
    end
  rescue ::Exception
  end

  if not db
    self.service.tokens[token] = [nil, nil, nil, true]
  end

  { "result" => "success" }
end

#rpc_token_generateObject


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/msf/core/rpc/v10/rpc_auth.rb', line 88

def rpc_token_generate
  token = Rex::Text.rand_text_alphanumeric(32)
  db = false
  begin
    if framework.db and framework.db.active
      t = ::Mdm::ApiKey.new
      t.token = token
      t.save!
      db = true
    end
  rescue ::Exception
  end

  if not db
    token = "TEMP" + Rex::Text.rand_text_numeric(28)
    self.service.tokens[token] = [nil, nil, nil, true]
  end

  { "result" => "success", "token" => token }
end

#rpc_token_listObject


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/msf/core/rpc/v10/rpc_auth.rb', line 56

def rpc_token_list
  res = self.service.tokens.keys
  begin
    if framework.db and framework.db.active
      ::Mdm::ApiKey.find(:all).each do |k|
        res << k.token
      end
    end
  rescue ::Exception
  end
  { "tokens" => res }
end

#rpc_token_remove(token) ⇒ Object


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/msf/core/rpc/v10/rpc_auth.rb', line 109

def rpc_token_remove(token)
  db = false
  begin
    if framework.db and framework.db.active
      t = ::Mdm::ApiKey.find_by_token(token)
      t.destroy if t
      db = true
    end
  rescue ::Exception
  end

  self.service.tokens.delete(token)

  { "result" => "success" }
end