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/auxiliary/cloud/aws/enum_ec2.rb
Views: 11655
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'aws-sdk-ec2'67class MetasploitModule < Msf::Auxiliary8include Msf::Auxiliary::Report9def initialize(info = {})10super(11update_info(12info,13'Name' => 'Amazon Web Services EC2 instance enumeration',14'Description' => %q{15Provided AWS credentials, this module will call the authenticated16API of Amazon Web Services to list all EC2 instances associated17with the account18},19'Author' => [20'Aaron Soto <[email protected]>',21'RageLtMan <rageltman[at]sempervictus>'22],23'License' => MSF_LICENSE,24'Notes' => {25'SideEffects' => [IOC_IN_LOGS],26'Stability' => [CRASH_SAFE],27'Reliability' => []28}29)30)3132register_options(33[34OptInt.new('LIMIT', [false, 'Only return the specified number of results from each region']),35OptString.new('REGION', [false, 'AWS Region (eg. "us-west-2")']),36OptString.new('ACCESS_KEY_ID', [true, 'AWS Access Key ID (eg. "AKIAXXXXXXXXXXXXXXXX")', '']),37OptString.new('SECRET_ACCESS_KEY', [true, 'AWS Secret Access Key (eg. "CA1+XXXXXXXXXXXXXXXXXXXXXX6aYDHHCBuLuV79")', ''])38]39)40end4142def enumerate_regions43regions = []4445ec2 = Aws::EC2::Resource.new(46region: 'us-west-1',47access_key_id: datastore['ACCESS_KEY_ID'],48secret_access_key: datastore['SECRET_ACCESS_KEY']49)5051ec2_regions = ec2.client.describe_regions.data.regions52ec2_regions.each do |r|53regions.append(r.region_name)54end5556regions57end5859def describe_ec2_instance(inst)60print_good " #{inst.id} (#{inst.state.name})"61print_good " Creation Date: #{inst.launch_time}"62print_good " Public IP: #{inst.public_ip_address} (#{inst.public_dns_name})"63print_good " Private IP: #{inst.private_ip_address} (#{inst.private_dns_name})"64# Report hosts and info65mac_addr = inst.network_interfaces.select do |iface|66iface.private_ip_address == inst.private_ip_address67end.first.mac_address68iname = inst.tags.find { |t| t.key == 'Name' } ? inst.tags.find { |t| t.key == 'Name' }.value : inst.private_dns_name69iinfo = inst.tags.find { |t| t.key == 'Description' } ? inst.tags.find { |t| t.key == 'Description' }.value : nil70report_host(71host: inst.private_ip_address,72mac: mac_addr,73os_name: inst.platform_details,74os_flavor: inst.architecture,75name: iname,76info: iinfo,77comments: "ec2-id: #{inst.id} (#{inst.placement.availability_zone})"78)79if inst.public_ip_address80report_note(81host: inst.private_ip_address,82type: 'ec2.public_ip',83data: inst.public_ip_address84)85end86#eips = inst.network_interfaces.map {|i| i.association && i.association.public_ip}.compact # <-- works in pry, breaks at runtime in AWS SDK87#report_note(88# host: inst.private_ip_address,89# type: 'ec2.public_ips',90# data: eips.join(' ')91#) unless eips.empty?92if inst.public_ip_address && !inst.public_dns_name.empty?93report_note(94host: inst.private_ip_address,95type: 'ec2.public_dns',96data: "#{inst.public_dns_name} #{inst.public_ip_address}"97)98end99if inst.hypervisor100report_note(101host: inst.private_ip_address,102type: 'ec2.hypervisor',103data: inst.hypervisor104)105end106inst.security_groups.each do |s|107print_good " Security Group: #{s.group_id}"108report_note(109host: inst.private_ip_address,110type: "ec2.#{s.group_id}",111data: s.group_name112)113end114inst.tags.each do |t|115print_good " Tag: #{t.key} = #{t.value}"116report_note(117host: inst.private_ip_address,118type: "ec2.tag #{t.key}",119data: t.value120)121end122end123124def run125all_regions = enumerate_regions126if datastore['REGION'].blank?127regions = all_regions128elsif !all_regions.include?(datastore['REGION'])129fail_with(Failure::BadConfig, "Invalid AWS region: #{datastore['REGION']}")130else131regions = [datastore['REGION']]132end133134regions.uniq.each do |region|135vprint_status "Checking #{region}..."136ec2 = Aws::EC2::Resource.new(137region: region,138access_key_id: datastore['ACCESS_KEY_ID'],139secret_access_key: datastore['SECRET_ACCESS_KEY']140)141142instances = datastore['LIMIT'] ? ec2.instances.limit(datastore['LIMIT']) : ec2.instances143print_status "Found #{ec2.instances.count} instances in #{region}"144145instances.each do |i|146describe_ec2_instance(i)147end148end149rescue Seahorse::Client::NetworkingError => e150print_error e.message151print_error 'Confirm region name (eg. us-west-2) is valid or blank before retrying'152rescue Aws::EC2::Errors::ServiceError => e153fail_with(Failure::UnexpectedReply, e.message)154end155end156157158