metasploit-framework/lib/msf/core/post/unix.rb

84 lines
1.9 KiB
Ruby
Raw Normal View History

# -*- coding: binary -*-
2012-03-27 16:52:16 +00:00
module Msf::Post::Unix
2012-03-27 16:52:16 +00:00
2013-08-30 21:28:33 +00:00
#
# Returns an array of hashes each representing a user
# Keys are name, uid, gid, info, dir and shell
#
def get_users
users = []
etc_passwd = nil
[
"/etc/passwd",
"/etc/security/passwd",
"/etc/master.passwd",
].each { |f|
if file_exist?(f)
etc_passwd = f
break
end
}
cmd_out = read_file(etc_passwd).split("\n")
cmd_out.each do |l|
entry = {}
user_field = l.split(":")
entry[:name] = user_field[0]
entry[:uid] = user_field[2]
entry[:gid] = user_field[3]
entry[:info] = user_field[4]
entry[:dir] = user_field[5]
entry[:shell] = user_field[6]
users << entry
end
return users
end
2012-03-27 16:52:16 +00:00
2013-08-30 21:28:33 +00:00
#
# Returns an array of hashes each hash representing a user group
# Keys are name, gid and users
#
def get_groups
groups = []
cmd_out = read_file("/etc/group").split("\n")
cmd_out.each do |l|
entry = {}
user_field = l.split(":")
entry[:name] = user_field[0]
entry[:gid] = user_field[2]
entry[:users] = user_field[3]
groups << entry
end
return groups
end
2012-03-27 16:52:16 +00:00
2013-08-30 21:28:33 +00:00
#
# Enumerates the user directories in /Users or /home
#
def enum_user_directories
user_dirs = []
2012-03-27 16:52:16 +00:00
2013-08-30 21:28:33 +00:00
# get all user directories from /etc/passwd
read_file("/etc/passwd").each_line do |passwd_line|
user_dirs << passwd_line.split(/:/)[5]
end
2012-03-27 16:52:16 +00:00
2013-08-30 21:28:33 +00:00
# also list other common places for home directories in the event that
# the users aren't in /etc/passwd (LDAP, for example)
case session.platform
when 'osx'
user_dirs << cmd_exec('ls /Users').each_line.map { |l| "/Users/#{l}" }
else
user_dirs << cmd_exec('ls /home').each_line.map { |l| "/home/#{l}" }
end
2012-03-27 16:52:16 +00:00
2013-08-30 21:28:33 +00:00
user_dirs.flatten!
user_dirs.compact!
user_dirs.sort!
user_dirs.uniq!
2012-03-27 16:52:16 +00:00
2013-08-30 21:28:33 +00:00
user_dirs
end
2012-03-27 16:52:16 +00:00
end