CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

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