Module: PacketGen

Defined in:
lib/packetgen.rb,
lib/packetgen/pcap.rb,
lib/packetgen/proto.rb,
lib/packetgen/types.rb,
lib/packetgen/utils.rb,
lib/packetgen/config.rb,
lib/packetgen/header.rb,
lib/packetgen/inject.rb,
lib/packetgen/packet.rb,
lib/packetgen/pcapng.rb,
lib/packetgen/capture.rb,
lib/packetgen/inspect.rb,
lib/packetgen/version.rb,
lib/packetgen/header/ip.rb,
lib/packetgen/types/int.rb,
lib/packetgen/types/oui.rb,
lib/packetgen/types/tlv.rb,
lib/packetgen/header/arp.rb,
lib/packetgen/header/dns.rb,
lib/packetgen/header/dns.rb,
lib/packetgen/header/eap.rb,
lib/packetgen/header/eth.rb,
lib/packetgen/header/gre.rb,
lib/packetgen/header/llc.rb,
lib/packetgen/header/mld.rb,
lib/packetgen/header/tcp.rb,
lib/packetgen/header/tcp.rb,
lib/packetgen/header/udp.rb,
lib/packetgen/headerable.rb,
lib/packetgen/pcapng/epb.rb,
lib/packetgen/pcapng/idb.rb,
lib/packetgen/pcapng/shb.rb,
lib/packetgen/pcapng/spb.rb,
lib/packetgen/types/enum.rb,
lib/packetgen/deprecation.rb,
lib/packetgen/header/base.rb,
lib/packetgen/header/dhcp.rb,
lib/packetgen/header/icmp.rb,
lib/packetgen/header/igmp.rb,
lib/packetgen/header/ipv6.rb,
lib/packetgen/header/ipv6.rb,
lib/packetgen/header/mdns.rb,
lib/packetgen/header/snmp.rb,
lib/packetgen/header/tftp.rb,
lib/packetgen/pcapng/file.rb,
lib/packetgen/types/array.rb,
lib/packetgen/header/bootp.rb,
lib/packetgen/header/dot11.rb,
lib/packetgen/header/dot1q.rb,
lib/packetgen/header/dot1x.rb,
lib/packetgen/header/mldv2.rb,
lib/packetgen/pcapng/block.rb,
lib/packetgen/types/fields.rb,
lib/packetgen/types/string.rb,
lib/packetgen/header/dhcpv6.rb,
lib/packetgen/header/dns/rr.rb,
lib/packetgen/header/icmpv6.rb,
lib/packetgen/header/igmpv3.rb,
lib/packetgen/header/ospfv2.rb,
lib/packetgen/header/ospfv3.rb,
lib/packetgen/types/cstring.rb,
lib/packetgen/header/dns/opt.rb,
lib/packetgen/header/eap/md5.rb,
lib/packetgen/header/eap/tls.rb,
lib/packetgen/header/ip/addr.rb,
lib/packetgen/unknown_packet.rb,
lib/packetgen/header/dns/name.rb,
lib/packetgen/header/eap/fast.rb,
lib/packetgen/header/eap/ttls.rb,
lib/packetgen/pcaprub_wrapper.rb,
lib/packetgen/types/fieldable.rb,
lib/packetgen/header/asn1_base.rb,
lib/packetgen/header/igmpv3/mq.rb,
lib/packetgen/header/igmpv3/mr.rb,
lib/packetgen/header/ip/option.rb,
lib/packetgen/header/ipv6/addr.rb,
lib/packetgen/header/mldv2/mlq.rb,
lib/packetgen/header/mldv2/mlr.rb,
lib/packetgen/types/int_string.rb,
lib/packetgen/header/dns/option.rb,
lib/packetgen/header/dot11/data.rb,
lib/packetgen/header/http/verbs.rb,
lib/packetgen/header/ip/options.rb,
lib/packetgen/header/ospfv2/lsa.rb,
lib/packetgen/header/ospfv3/lsa.rb,
lib/packetgen/header/tcp/option.rb,
lib/packetgen/types/length_from.rb,
lib/packetgen/utils/arp_spoofer.rb,
lib/packetgen/header/dhcp/option.rb,
lib/packetgen/header/dhcpv6/duid.rb,
lib/packetgen/header/tcp/options.rb,
lib/packetgen/types/abstract_tlv.rb,
lib/packetgen/header/dhcp/options.rb,
lib/packetgen/header/dhcpv6/relay.rb,
lib/packetgen/header/dns/question.rb,
lib/packetgen/header/http/headers.rb,
lib/packetgen/header/http/request.rb,
lib/packetgen/header/ospfv2/hello.rb,
lib/packetgen/header/ospfv3/hello.rb,
lib/packetgen/header/dhcpv6/option.rb,
lib/packetgen/header/dns/qdsection.rb,
lib/packetgen/header/dns/rrsection.rb,
lib/packetgen/header/dot11/control.rb,
lib/packetgen/header/dot11/element.rb,
lib/packetgen/header/http/response.rb,
lib/packetgen/header/ospfv2/ls_ack.rb,
lib/packetgen/header/ospfv3/ls_ack.rb,
lib/packetgen/pcapng/unknown_block.rb,
lib/packetgen/header/dhcpv6/options.rb,
lib/packetgen/header/dot11/sub_mngt.rb,
lib/packetgen/header/ipv6/extension.rb,
lib/packetgen/header/ipv6/hop_by_hop.rb,
lib/packetgen/header/dot11/management.rb,
lib/packetgen/header/ospfv2/ls_update.rb,
lib/packetgen/header/ospfv3/ls_update.rb,
lib/packetgen/header/ospfv2/ls_request.rb,
lib/packetgen/header/ospfv2/lsa_header.rb,
lib/packetgen/header/ospfv3/ls_request.rb,
lib/packetgen/header/ospfv3/lsa_header.rb,
lib/packetgen/header/ospfv3/ipv6_prefix.rb,
lib/packetgen/header/igmpv3/group_record.rb,
lib/packetgen/header/ospfv2/db_description.rb,
lib/packetgen/header/ospfv3/db_description.rb,
lib/packetgen/header/mldv2/mcast_address_record.rb

Overview

This file is part of PacketGen See github.com/sdaubert/packetgen for more informations Copyright (C) 2016 Sylvain Daubert <[email protected]> This program is published under MIT license.

Defined Under Namespace

Modules: Deprecation, Header, Headerable, Inject, Inspect, PCAPRUBWrapper, Pcap, PcapNG, Proto, Types, Utils Classes: BindingError, Capture, Config, Error, FormatError, Packet, ParseError, UnknownPacket, WireError

Constant Summary collapse

VERSION =

PacketGen version

'3.2.1'

Class Method Summary collapse

Class Method Details

.capture(**kwargs) {|packet| ... } ⇒ Array<Packet>

Yield Parameters:

Returns:

See Also:


69
70
71
# File 'lib/packetgen.rb', line 69

def self.capture(**kwargs)
  Packet.capture(**kwargs) { |packet| yield packet if block_given? }
end

.default_ifaceString

Get default network interface (ie. first non-loopback declared interface)

Returns:

  • (String)

97
98
99
100
101
102
103
104
105
106
107
# File 'lib/packetgen.rb', line 97

def self.default_iface
  return @default_iface if defined? @default_iface

  @default_iface = Interfacez.raw_interface_addresses.each do |iface|
    next unless iface.broadaddr
    next unless Interfacez.ipv4_address_of(iface.name)
    next unless Interfacez.ipv6_address_of(iface.name)

    break iface.name
  end
end

.force_binary(str) ⇒ String

Force binary encoding for str

Parameters:

  • str (String)

Returns:

  • (String)

    binary encoded string


91
92
93
# File 'lib/packetgen.rb', line 91

def self.force_binary(str)
  str.dup.force_encoding(Encoding::BINARY)
end

.gen(protocol, options = {}) ⇒ Packet

Shortcut for PacketGen::Packet.gen

Parameters:

  • protocol (String)

    base protocol for packet

  • options (Hash) (defaults to: {})

    specific options for protocol

Returns:


52
53
54
# File 'lib/packetgen.rb', line 52

def self.gen(protocol, options={})
  Packet.gen protocol, options
end

.header(protocol, options = {}) ⇒ Header::Base

Shortcut to get a header class

Examples:

builtin class

# same as PacketGen::Header::Dot11:Data.new(id: 0xfedc)
dot11 = PacketGen.header('Dot11::Data', id: 0xfedc)  #=> PacketGen::Header::Dot11:Data

plugin class

require 'packet-plugin-smb'
# same as PacketGen::Plugin::SMB::CloseRequest.new(fid: 0x1234)
smbclose = PacketGen.header('SMB::CloseRequest', fid: 0x1234)

Parameters:

  • protocol (String)

    protocol from which generate a header

  • options (Hash) (defaults to: {})

    specific options for protocol

Returns:


126
127
128
# File 'lib/packetgen.rb', line 126

def self.header(protocol, options={})
  Header.get_header_class_by_name(protocol).new(options)
end

.loopback_ifaceString

Get loopback network interface

Returns:

  • (String)

111
112
113
# File 'lib/packetgen.rb', line 111

def self.loopback_iface
  Interfacez.loopback
end

.parse(binary_str, first_header: nil) ⇒ Packet

Parameters:

  • binary_str (String)
  • first_header (String) (defaults to: nil)

    First protocol header

Returns:


60
61
62
# File 'lib/packetgen.rb', line 60

def self.parse(binary_str, first_header: nil)
  Packet.parse binary_str, first_header: first_header
end

.read(filename) ⇒ Array<Packet>

Shortcut for PacketGen::Packet.read

Parameters:

  • filename (String)

    PcapNG file

Returns:


76
77
78
# File 'lib/packetgen.rb', line 76

def self.read(filename)
  Packet.read filename
end

.write(filename, packets) ⇒ void

This method returns an undefined value.

Shortcut for PacketGen::Packet.write

Parameters:

  • filename (String)
  • packets (Array<Packet>)

    packets to write


84
85
86
# File 'lib/packetgen.rb', line 84

def self.write(filename, packets)
  Packet.write filename, packets
end