Skip to content

Instantly share code, notes, and snippets.

@kernelsmith
Created January 9, 2013 18:50
Show Gist options
  • Save kernelsmith/4495741 to your computer and use it in GitHub Desktop.
Save kernelsmith/4495741 to your computer and use it in GitHub Desktop.
irb/ruby code snippets to find all the active Exceptions or open file handles etc. You may encounter and possibly want to handle these exceptions in code you are writing for msf.
# open file handles
ObjectSpace.each_object(File) {|o| puts o.inspect}
# finding all the active Exceptions which one might have to catch in metasploit
# start msfconsole, proceed to the environment you want to examine, for instance if
# you want to see what exceptions are available in a meterp session, then establish
# one, interace with it, and then...
# run the irb command and enter the following
ObjectSpace.each_object(Class) {|o| puts o.inspect if o.ancestors.include?(Exception)}
# Example from a psexec created meterpreter session:
# meterpreter > irb
# [*] Starting IRB shell
# [*] The 'client' variable holds the meterpreter client
# >> ObjectSpace.each_object(Class) {|o| puts o.inspect if o.ancestors.include?(Exception)}
# Rex::ConnectionProxyError
# Rex::UnsupportedProtocol
# Rex::AddressInUse
# Rex::ConnectionTimeout
# Rex::HostUnreachable
# Rex::ConnectionRefused
# Rex::ConnectionError
# Rex::StreamClosedError
# Rex::AmbiguousArgumentError
# Rex::ArgumentParseError
# Rex::ArgumentError
# Rex::RuntimeError
# Rex::NotImplementedError
# Rex::TimeoutError
# Rex::Text::IllegalSequence
# Msf::NoKeyError
# Msf::EncodingError
# Msf::ValidationError
# Msf::OptionValidateError
# Msf::NoNopsSucceededError
# Msf::NoCompatiblePayloadError
# Msf::IncompatiblePayloadError
# Msf::MissingActionError
# Msf::MissingPayloadError
# Msf::MissingTargetError
# Msf::BadGenerateError
# Msf::NoEncodersSucceededError
# Msf::BadcharError
# Msf::PluginLoadError
# Rex::Post::Meterpreter::RequestError
# Rex::Poly::InvalidRegisterError
# Example from a base msfconsole session
# msf > irb
# [*] Starting IRB shell...
# >> ObjectSpace.each_object(Class) {|o| puts o.inspect if o.ancestors.include?(Exception)}
# Msf::NoKeyError
# Msf::EncodingError
# Msf::ValidationError
# Msf::OptionValidateError
# Msf::NoNopsSucceededError
# Msf::NoCompatiblePayloadError
# Msf::IncompatiblePayloadError
# Msf::MissingActionError
# Msf::MissingPayloadError
# Msf::MissingTargetError
# Msf::BadGenerateError
# Msf::NoEncodersSucceededError
# Msf::BadcharError
# Msf::PluginLoadError
@kernelsmith
Copy link
Author

here's a possibly better one, haven't tried it yet tho

exceptions = []
tree = {}
ObjectSpace.each_object(Class) do |cls|
  next unless cls.ancestors.include? Exception
  next if exceptions.include? cls
  next if cls.superclass == SystemCallError # avoid dumping Errno's
  exceptions << cls
  cls.ancestors.delete_if {|e| [Object, Kernel].include? e }.reverse.inject(tree) {|memo,cls| memo[cls] ||= {}}
end

indent = 0
tree_printer = Proc.new do |t|
  t.keys.sort { |c1,c2| c1.name <=> c2.name }.each do |k|
    space = (' ' * indent); space ||= ''
    puts space + k.to_s
    indent += 2; tree_printer.call t[k]; indent -= 2
  end
end
tree_printer.call tree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment