Class: PacketFu::TCPHeader
- Includes:
- StructFu
- Defined in:
- lib/packetfu/protos/tcp.rb
Overview
TCPHeader is a complete TCP struct, used in TCPPacket. Most IP traffic is TCP-based, by volume.
For more on TCP packets, see www.networksorcery.com/enp/protocol/tcp.htm
Header Definition
Int16 :tcp_src Default: random
Int16 :tcp_dst
Int32 :tcp_seq Default: random
Int32 :tcp_ack
TcpHlen :tcp_hlen Default: 5 # Must recalc as options are set.
TcpReserved :tcp_reserved Default: 0
TcpEcn :tcp_ecn
TcpFlags :tcp_flags
Int16 :tcp_win, Default: 0 # WinXP's default syn packet
Int16 :tcp_sum, Default: calculated # Must set this upon generation.
Int16 :tcp_urg
TcpOptions :tcp_opts
String :body
See also TcpHlen, TcpReserved, TcpEcn, TcpFlags, TcpOpts
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#flavor ⇒ Object
Returns the value of attribute flavor.
-
#tcp_ack ⇒ Object
Getter for the TCP ackowlegement number.
-
#tcp_dst ⇒ Object
Getter for the TCP destination port.
-
#tcp_ecn ⇒ Object
Getter for the ECN bits.
-
#tcp_flags ⇒ Object
Returns the value of attribute tcp_flags.
-
#tcp_hlen ⇒ Object
Getter for the TCP Header Length value.
-
#tcp_opts ⇒ Object
Getter for TCP Options.
-
#tcp_reserved ⇒ Object
Getter for the TCP Reserved field.
-
#tcp_seq ⇒ Object
Getter for the TCP sequence number.
-
#tcp_src ⇒ Object
Getter for the TCP source port.
-
#tcp_sum ⇒ Object
Getter for the TCP checksum.
-
#tcp_urg ⇒ Object
Getter for the TCP urgent field.
-
#tcp_win ⇒ Object
Getter for the TCP window size number.
Instance Method Summary collapse
-
#bits_to_s ⇒ Object
Helper function to create the string for Hlen, Reserved, ECN, and Flags.
-
#initialize(args = {}) ⇒ TCPHeader
constructor
A new instance of TCPHeader.
-
#rand_port ⇒ Object
Generates a random high port.
-
#read(str) ⇒ Object
Reads a string to populate the object.
- #tcp_ack_readable ⇒ Object
-
#tcp_calc_hlen ⇒ Object
Sets and returns the true length of the TCP Header.
-
#tcp_calc_seq ⇒ Object
Resets the sequence number to a new random number.
-
#tcp_calc_src ⇒ Object
Resets the source port to a new random number.
-
#tcp_dport ⇒ Object
Equivalent to tcp_dst.
-
#tcp_dport=(arg) ⇒ Object
Equivalent to tcp_dst=.
-
#tcp_flags_dotmap ⇒ Object
(also: #tcp_flags_readable)
Gets a more readable flags list.
-
#tcp_options ⇒ Object
Gets a more readable option list.
-
#tcp_options=(arg) ⇒ Object
Sets a more readable option list.
-
#tcp_opts_len ⇒ Object
Returns the actual length of the TCP options.
- #tcp_opts_readable ⇒ Object
-
#tcp_recalc(arg = :all) ⇒ Object
Recalculates calculated fields for TCP (except checksum which is at the Packet level).
- #tcp_seq_readable ⇒ Object
-
#tcp_sport ⇒ Object
Equivalent to tcp_src.
-
#tcp_sport=(arg) ⇒ Object
Equivalent to tcp_src=.
- #tcp_sum_readable ⇒ Object
-
#to_s ⇒ Object
Returns the object in string form.
Methods included from StructFu
#clone, #set_endianness, #sz, #typecast
Methods inherited from Struct
Constructor Details
#initialize(args = {}) ⇒ TCPHeader
Returns a new instance of TCPHeader.
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
# File 'lib/packetfu/protos/tcp.rb', line 670 def initialize(args={}) @random_seq = rand(0xffffffff) @random_src = rand_port super( Int16.new(args[:tcp_src] || tcp_calc_src), Int16.new(args[:tcp_dst]), Int32.new(args[:tcp_seq] || tcp_calc_seq), Int32.new(args[:tcp_ack]), TcpHlen.new(:hlen => (args[:tcp_hlen] || 5)), TcpReserved.new(args[:tcp_reserved] || 0), TcpEcn.new(args[:tcp_ecn]), TcpFlags.new(args[:tcp_flags]), Int16.new(args[:tcp_win] || 0x4000), Int16.new(args[:tcp_sum] || 0), Int16.new(args[:tcp_urg]), TcpOptions.new.read(args[:tcp_opts]), StructFu::String.new.read(args[:body]) ) end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body
662 663 664 |
# File 'lib/packetfu/protos/tcp.rb', line 662 def body @body end |
#flavor ⇒ Object
Returns the value of attribute flavor.
690 691 692 |
# File 'lib/packetfu/protos/tcp.rb', line 690 def flavor @flavor end |
#tcp_ack ⇒ Object
Getter for the TCP ackowlegement number.
662 663 664 |
# File 'lib/packetfu/protos/tcp.rb', line 662 def tcp_ack @tcp_ack end |
#tcp_dst ⇒ Object
Getter for the TCP destination port.
662 663 664 |
# File 'lib/packetfu/protos/tcp.rb', line 662 def tcp_dst @tcp_dst end |
#tcp_ecn ⇒ Object
Getter for the ECN bits.
662 663 664 |
# File 'lib/packetfu/protos/tcp.rb', line 662 def tcp_ecn @tcp_ecn end |
#tcp_flags ⇒ Object
Returns the value of attribute tcp_flags
662 663 664 |
# File 'lib/packetfu/protos/tcp.rb', line 662 def tcp_flags @tcp_flags end |
#tcp_hlen ⇒ Object
Getter for the TCP Header Length value.
662 663 664 |
# File 'lib/packetfu/protos/tcp.rb', line 662 def tcp_hlen @tcp_hlen end |
#tcp_opts ⇒ Object
Getter for TCP Options.
662 663 664 |
# File 'lib/packetfu/protos/tcp.rb', line 662 def tcp_opts @tcp_opts end |
#tcp_reserved ⇒ Object
Getter for the TCP Reserved field.
662 663 664 |
# File 'lib/packetfu/protos/tcp.rb', line 662 def tcp_reserved @tcp_reserved end |
#tcp_seq ⇒ Object
Getter for the TCP sequence number.
662 663 664 |
# File 'lib/packetfu/protos/tcp.rb', line 662 def tcp_seq @tcp_seq end |
#tcp_src ⇒ Object
Getter for the TCP source port.
662 663 664 |
# File 'lib/packetfu/protos/tcp.rb', line 662 def tcp_src @tcp_src end |
#tcp_sum ⇒ Object
Getter for the TCP checksum.
662 663 664 |
# File 'lib/packetfu/protos/tcp.rb', line 662 def tcp_sum @tcp_sum end |
#tcp_urg ⇒ Object
Getter for the TCP urgent field.
662 663 664 |
# File 'lib/packetfu/protos/tcp.rb', line 662 def tcp_urg @tcp_urg end |
#tcp_win ⇒ Object
Getter for the TCP window size number.
662 663 664 |
# File 'lib/packetfu/protos/tcp.rb', line 662 def tcp_win @tcp_win end |
Instance Method Details
#bits_to_s ⇒ Object
Helper function to create the string for Hlen, Reserved, ECN, and Flags.
693 694 695 696 697 698 699 700 701 702 |
# File 'lib/packetfu/protos/tcp.rb', line 693 def bits_to_s bytes = [] bytes[0] = (self[:tcp_hlen].to_i << 4) + (self[:tcp_reserved].to_i << 1) + self[:tcp_ecn].n.to_i bytes[1] = (self[:tcp_ecn].c.to_i << 7) + (self[:tcp_ecn].e.to_i << 6) + self[:tcp_flags].to_i bytes.pack("CC") end |
#rand_port ⇒ Object
Generates a random high port. This is affected by packet flavor.
852 853 854 |
# File 'lib/packetfu/protos/tcp.rb', line 852 def rand_port rand(0xffff - 1025) + 1025 end |
#read(str) ⇒ Object
Reads a string to populate the object.
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 |
# File 'lib/packetfu/protos/tcp.rb', line 723 def read(str) force_binary(str) return self if str.nil? self[:tcp_src].read(str[0,2]) self[:tcp_dst].read(str[2,2]) self[:tcp_seq].read(str[4,4]) self[:tcp_ack].read(str[8,4]) self[:tcp_hlen].read(str[12,1]) self[:tcp_reserved].read(str[12,1]) self[:tcp_ecn].read(str[12,2]) self[:tcp_flags].read(str[13,1]) self[:tcp_win].read(str[14,2]) self[:tcp_sum].read(str[16,2]) self[:tcp_urg].read(str[18,2]) self[:tcp_opts].read(str[20,((self[:tcp_hlen].to_i * 4) - 20)]) self[:body].read(str[(self[:tcp_hlen].to_i * 4),str.size]) self end |
#tcp_ack_readable ⇒ Object
919 920 921 |
# File 'lib/packetfu/protos/tcp.rb', line 919 def tcp_ack_readable "0x%08x" % tcp_ack end |
#tcp_calc_hlen ⇒ Object
Sets and returns the true length of the TCP Header. TODO: Think about making all the option stuff safer.
847 848 849 |
# File 'lib/packetfu/protos/tcp.rb', line 847 def tcp_calc_hlen self[:tcp_hlen] = TcpHlen.new(:hlen => ((20 + tcp_opts_len) / 4)) end |
#tcp_calc_seq ⇒ Object
Resets the sequence number to a new random number.
836 |
# File 'lib/packetfu/protos/tcp.rb', line 836 def tcp_calc_seq; @random_seq; end |
#tcp_calc_src ⇒ Object
Resets the source port to a new random number.
838 |
# File 'lib/packetfu/protos/tcp.rb', line 838 def tcp_calc_src; @random_src; end |
#tcp_dport ⇒ Object
Equivalent to tcp_dst.
886 887 888 |
# File 'lib/packetfu/protos/tcp.rb', line 886 def tcp_dport self.tcp_dst.to_i end |
#tcp_dport=(arg) ⇒ Object
Equivalent to tcp_dst=.
891 892 893 |
# File 'lib/packetfu/protos/tcp.rb', line 891 def tcp_dport=(arg) self.tcp_dst=(arg) end |
#tcp_flags_dotmap ⇒ Object Also known as: tcp_flags_readable
Gets a more readable flags list
862 863 864 865 866 867 868 |
# File 'lib/packetfu/protos/tcp.rb', line 862 def tcp_flags_dotmap dotmap = tcp_flags.members.map do |flag| status = self.tcp_flags.send flag status == 0 ? "." : flag.to_s.upcase[0].chr end dotmap.join end |
#tcp_options ⇒ Object
Gets a more readable option list.
857 858 859 |
# File 'lib/packetfu/protos/tcp.rb', line 857 def self[:tcp_opts].decode end |
#tcp_options=(arg) ⇒ Object
Sets a more readable option list.
871 872 873 |
# File 'lib/packetfu/protos/tcp.rb', line 871 def (arg) self[:tcp_opts].encode arg end |
#tcp_opts_len ⇒ Object
Returns the actual length of the TCP options.
841 842 843 |
# File 'lib/packetfu/protos/tcp.rb', line 841 def tcp_opts_len self[:tcp_opts].to_s.size end |
#tcp_opts_readable ⇒ Object
931 932 933 |
# File 'lib/packetfu/protos/tcp.rb', line 931 def tcp_opts_readable end |
#tcp_recalc(arg = :all) ⇒ Object
Recalculates calculated fields for TCP (except checksum which is at the Packet level).
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 |
# File 'lib/packetfu/protos/tcp.rb', line 896 def tcp_recalc(arg=:all) case arg when :tcp_hlen tcp_calc_hlen when :tcp_src @random_tcp_src = rand_port when :tcp_sport @random_tcp_src = rand_port when :tcp_seq @random_tcp_seq = rand(0xffffffff) when :all tcp_calc_hlen @random_tcp_src = rand_port @random_tcp_seq = rand(0xffffffff) else raise ArgumentError, "No such field `#{arg}'" end end |
#tcp_seq_readable ⇒ Object
923 924 925 |
# File 'lib/packetfu/protos/tcp.rb', line 923 def tcp_seq_readable "0x%08x" % tcp_seq end |
#tcp_sport ⇒ Object
Equivalent to tcp_src.
876 877 878 |
# File 'lib/packetfu/protos/tcp.rb', line 876 def tcp_sport self.tcp_src.to_i end |
#tcp_sport=(arg) ⇒ Object
Equivalent to tcp_src=.
881 882 883 |
# File 'lib/packetfu/protos/tcp.rb', line 881 def tcp_sport=(arg) self.tcp_src=(arg) end |
#tcp_sum_readable ⇒ Object
927 928 929 |
# File 'lib/packetfu/protos/tcp.rb', line 927 def tcp_sum_readable "0x%04x" % tcp_sum end |
#to_s ⇒ Object
Returns the object in string form.
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 |
# File 'lib/packetfu/protos/tcp.rb', line 705 def to_s hdr = self.to_a.map do |x| if x.kind_of? TcpHlen bits_to_s elsif x.kind_of? TcpReserved next elsif x.kind_of? TcpEcn next elsif x.kind_of? TcpFlags next else x.to_s end end hdr.flatten.join end |