PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/rex/post/meterpreter/extensions/stdapi/net/interface.rb

https://github.com/Jonono2/metasploit-framework
Ruby | 129 lines | 64 code | 16 blank | 49 comment | 3 complexity | 8e46ef4b1f82f5039d4dc8447d813267 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, GPL-3.0, LGPL-2.1, GPL-2.0
  1. # -*- coding: binary -*-
  2. require 'ipaddr'
  3. module Rex
  4. module Post
  5. module Meterpreter
  6. module Extensions
  7. module Stdapi
  8. module Net
  9. ###
  10. #
  11. # This class represents a logical physical interface
  12. # on the remote machine.
  13. #
  14. ###
  15. class Interface
  16. ##
  17. #
  18. # Constructor
  19. #
  20. ##
  21. #
  22. # Returns a logical interface and initializes it to the supplied
  23. # parameters.
  24. #
  25. def initialize(opts={})
  26. self.index = opts[:index] || -1
  27. self.mac_addr = opts[:mac_addr]
  28. self.mac_name = opts[:mac_name]
  29. self.mtu = opts[:mtu]
  30. self.flags = opts[:flags]
  31. self.addrs = opts[:addrs]
  32. self.netmasks = opts[:netmasks]
  33. self.scopes = opts[:scopes]
  34. end
  35. #
  36. # Returns a pretty string representation of the interface's properties.
  37. #
  38. def pretty
  39. macocts = []
  40. mac_addr.each_byte { |o| macocts << o }
  41. macocts += [0] * (6 - macocts.size) if macocts.size < 6
  42. info = [
  43. ["Name" , mac_name ],
  44. ["Hardware MAC" , sprintf("%02x:%02x:%02x:%02x:%02x:%02x",
  45. macocts[0], macocts[1], macocts[2],
  46. macocts[3], macocts[4], macocts[5])],
  47. ["MTU" , mtu ],
  48. ["Flags" , flags ],
  49. ]
  50. # If all went as planned, addrs and netmasks will have the same number
  51. # of elements and be properly ordered such that they match up
  52. # correctly.
  53. addr_masks = addrs.zip(netmasks)
  54. addr_masks.select { |a| Rex::Socket.is_ipv4?(a[0]) }.each { |a|
  55. info << [ "IPv4 Address", a[0] ]
  56. info << [ "IPv4 Netmask", a[1] ]
  57. }
  58. addr_masks.select { |a| Rex::Socket.is_ipv6?(a[0]) }.each { |a|
  59. info << [ "IPv6 Address", a[0] ]
  60. info << [ "IPv6 Netmask", a[1] ]
  61. }
  62. pad = info.map{|i| i[0] }.max_by{|k|k.length}.length
  63. ret = sprintf(
  64. "Interface %2d\n" +
  65. "============\n",
  66. index
  67. )
  68. info.map {|k,v|
  69. next if v.nil?
  70. ret << k.ljust(pad) + " : #{v}\n"
  71. }
  72. ret
  73. end
  74. #
  75. # The first address associated with this Interface
  76. #
  77. def ip
  78. addrs.first
  79. end
  80. #
  81. # The index of the interface.
  82. #
  83. attr_accessor :index
  84. #
  85. # An Array of IP addresses bound to the Interface.
  86. #
  87. attr_accessor :addrs
  88. #
  89. # The physical (MAC) address of the NIC.
  90. #
  91. attr_accessor :mac_addr
  92. #
  93. # The name of the interface.
  94. #
  95. attr_accessor :mac_name
  96. #
  97. # The MTU associated with the interface.
  98. #
  99. attr_accessor :mtu
  100. #
  101. # The flags associated with the interface.
  102. #
  103. attr_accessor :flags
  104. #
  105. # An Array of netmasks. This will have the same number of elements as #addrs
  106. #
  107. attr_accessor :netmasks
  108. #
  109. # An Array of IPv6 address scopes. This will have the same number of elements as #addrs
  110. #
  111. attr_accessor :scopes
  112. end
  113. end; end; end; end; end; end