mirror of
https://github.com/inspec/inspec
synced 2024-11-27 07:00:39 +00:00
lint port resource
Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
This commit is contained in:
parent
416499cd64
commit
9885e7683b
2 changed files with 108 additions and 101 deletions
|
@ -158,17 +158,18 @@ class LinuxPorts < PortsInfo
|
|||
return nil if cmd.exit_status.to_i != 0
|
||||
|
||||
ports = []
|
||||
# split on each newline
|
||||
# parse all lines
|
||||
cmd.stdout.each_line do |line|
|
||||
# parse each line
|
||||
# 1 - Proto, 2 - Recv-Q, 3 - Send-Q, 4 - Local Address, 5 - Foreign Address, 6 - State, 7 - Inode, 8 - PID/Program name
|
||||
parsed = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/.match(line)
|
||||
port_info = parse_netstat_line(line)
|
||||
|
||||
if !parsed.nil?
|
||||
protocol = parsed[1].downcase
|
||||
# only push protocols we are interested in
|
||||
next unless %w{tcp tcp6 udp udp6}.include?(port_info[:protocol])
|
||||
ports.push(port_info)
|
||||
end
|
||||
ports
|
||||
end
|
||||
|
||||
# parse ip4 and ip6 addresses
|
||||
net_addr = parsed[4]
|
||||
def parse_net_address(net_addr, protocol)
|
||||
if protocol.eql?('tcp6') || protocol.eql?('udp6')
|
||||
# prep for URI parsing, parse ip6 port
|
||||
ip6 = /^(\S+:)(\d+)$/.match(net_addr)
|
||||
|
@ -184,6 +185,18 @@ class LinuxPorts < PortsInfo
|
|||
host = ip_addr.host
|
||||
port = ip_addr.port
|
||||
end
|
||||
[host, port]
|
||||
end
|
||||
|
||||
def parse_netstat_line(line)
|
||||
# parse each line
|
||||
# 1 - Proto, 2 - Recv-Q, 3 - Send-Q, 4 - Local Address, 5 - Foreign Address, 6 - State, 7 - Inode, 8 - PID/Program name
|
||||
parsed = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/.match(line)
|
||||
return {} if parsed.nil?
|
||||
|
||||
# parse ip4 and ip6 addresses
|
||||
protocol = parsed[1].downcase
|
||||
host, port = parse_net_address(parsed[4], protocol)
|
||||
|
||||
# extract PID
|
||||
process = parsed[9].split('/')
|
||||
|
@ -192,19 +205,13 @@ class LinuxPorts < PortsInfo
|
|||
process = process[1]
|
||||
|
||||
# map data
|
||||
port_info = {
|
||||
{
|
||||
port: port,
|
||||
address: host,
|
||||
protocol: protocol,
|
||||
process: process,
|
||||
pid: pid,
|
||||
}
|
||||
|
||||
# push data, if its a known protocol tcp, tcp6, udp, udp6
|
||||
ports.push(port_info) if %w{tcp tcp6 udp udp6}.include?(protocol)
|
||||
end
|
||||
end
|
||||
ports
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -217,14 +224,16 @@ class FreeBsdPorts < PortsInfo
|
|||
ports = []
|
||||
# split on each newline
|
||||
cmd.stdout.each_line do |line|
|
||||
# 1 - USER, 2 - COMMAND, 3 - PID, 4 - FD 5 - PROTO, 6 - LOCAL ADDRESS, 7 - FOREIGN ADDRESS
|
||||
parsed = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/.match(line)
|
||||
port_info = parse_sockstat_line(line)
|
||||
|
||||
if !parsed.nil?
|
||||
protocol = parsed[5].downcase
|
||||
net_addr = parsed[6]
|
||||
# push data, if not headerfile
|
||||
next unless %w{tcp tcp6 udp udp6}.include?(port_info[:protocol])
|
||||
ports.push(port_info)
|
||||
end
|
||||
ports
|
||||
end
|
||||
|
||||
# extract ip information
|
||||
def parse_net_address(net_addr, protocol)
|
||||
case protocol
|
||||
when 'tcp4', 'udp4'
|
||||
# replace * with 0.0.0.0
|
||||
|
@ -233,7 +242,7 @@ class FreeBsdPorts < PortsInfo
|
|||
host = ip_addr.host
|
||||
port = ip_addr.port
|
||||
when 'tcp6', 'udp6'
|
||||
next if net_addr == '*:*' # abort for now
|
||||
return [] if net_addr == '*:*' # abort for now
|
||||
# replace * with 0:0:0:0:0:0:0:0
|
||||
net_addr = net_addr.gsub(/^\*:/, '0:0:0:0:0:0:0:0:') if /^*:(\d+)$/.match(net_addr)
|
||||
# extract port
|
||||
|
@ -244,6 +253,18 @@ class FreeBsdPorts < PortsInfo
|
|||
host = ip_addr.host[1..ip_addr.host.size-2]
|
||||
port = ip_addr.port
|
||||
end
|
||||
[host, port]
|
||||
end
|
||||
|
||||
def parse_sockstat_line(line)
|
||||
# 1 - USER, 2 - COMMAND, 3 - PID, 4 - FD 5 - PROTO, 6 - LOCAL ADDRESS, 7 - FOREIGN ADDRESS
|
||||
parsed = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/.match(line)
|
||||
return {} if parsed.nil?
|
||||
|
||||
# extract ip information
|
||||
protocol = parsed[5].downcase
|
||||
host, port = parse_net_address(parsed[6], protocol)
|
||||
return {} if host.nil? or port.nil?
|
||||
|
||||
# extract process
|
||||
process = parsed[2]
|
||||
|
@ -257,18 +278,12 @@ class FreeBsdPorts < PortsInfo
|
|||
protocol = 'udp' if protocol.eql?('udp4')
|
||||
|
||||
# map data
|
||||
port_info = {
|
||||
{
|
||||
port: port,
|
||||
address: host,
|
||||
protocol: protocol,
|
||||
process: process,
|
||||
pid: pid,
|
||||
}
|
||||
|
||||
# push data, if not headerfile
|
||||
ports.push(port_info) if %w{tcp tcp6 udp udp6}.include?(protocol)
|
||||
end
|
||||
end
|
||||
ports
|
||||
end
|
||||
end
|
||||
|
|
|
@ -14,12 +14,4 @@ require 'vulcano/resource'
|
|||
require 'vulcano/rspec_json_formatter'
|
||||
require 'vulcano/rule'
|
||||
require 'vulcano/runner'
|
||||
|
||||
require 'matchers/matchers'
|
||||
|
||||
# Dummy module for handling additional attributes
|
||||
# which may be injected by the user. This covers data
|
||||
# like passwords, usernames, or configuration flags.
|
||||
def attributes(what, required: false)
|
||||
nil
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue