Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/proto/ntp/modes.rb
Views: 11704
# -*- coding: binary -*-12require 'bindata'34module Rex5module Proto6module NTP::Modes78# A very generic NTP message9#10# Uses the common/similar parts from versions 1-4 and considers everything11# after to be just one big field. For the particulars on the different versions,12# see:13# http://tools.ietf.org/html/rfc958#appendix-B14# http://tools.ietf.org/html/rfc1059#appendix-B15# pages 45/48 of http://tools.ietf.org/pdf/rfc1119.pdf16# http://tools.ietf.org/html/rfc1305#appendix-D17# http://tools.ietf.org/html/rfc5905#page-1918class NTPGeneric < BinData::Record19alias size num_bytes20# 0 1 2 321# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 122# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+23# |LI | VN | mode| Stratum | Poll | Precision |24# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+25endian :big26bit2 :li27bit3 :version28bit3 :mode29uint8 :stratum30uint8 :poll31uint8 :precision32rest :payload33end3435# An NTP control message. Control messages are only specified for NTP36# versions 2-4, but this is a fuzzer so why not try them all...37class NTPControl < BinData::Record38alias size num_bytes39# 0 1 2 340# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 141# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+42# |00 | VN | 6 |R E M| op | Sequence |43# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+44# | status | association id |45# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+46# | offset | count |47# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+48endian :big49bit2 :reserved50bit3 :version51bit3 :mode, initial_value: 652bit1 :response53bit1 :error54bit1 :more55bit5 :operation56uint16 :sequence57uint16 :status58uint16 :association_id59# TODO: there *must* be bugs in the handling of these next two fields!60uint16 :payload_offset61uint16 :payload_size62rest :payload63end6465# An NTP "private" message. Private messages are only specified for NTP66# versions 2-4, but this is a fuzzer so why not try them all...67class NTPPrivate < BinData::Record68alias size num_bytes69# 0 1 2 370# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 171# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+72# |R M| VN | 7 |A| Sequence | Implementation| Req code |73# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+74# | err | Number of data items | MBZ | Size of data item |75# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+76endian :big77bit1 :response78bit1 :more79bit3 :version80bit3 :mode, initial_value: 781bit1 :auth82bit7 :sequence83uint8 :implementation84uint8 :request_code85bit4 :error86bit12 :record_count87bit4 :mbz88bit12 :record_size89rest :payload9091def records92records = []931.upto(record_count) do |record_num|94records << payload[record_size * (record_num - 1), record_size]95end96records97end98end99100class NTPSymmetric < BinData::Record101alias size num_bytes102endian :big103bit2 :li104bit3 :version, initial_value: 3105bit3 :mode106uint8 :stratum107uint8 :poll108uint8 :precision109uint32 :root_delay110uint32 :root_dispersion111uint32 :reference_id112uint64 :reference_timestamp113uint64 :origin_timestamp114uint64 :receive_timestamp115uint64 :transmit_timestamp116rest :payload117end118119def ntp_control(version, operation, payload = nil)120n = NTPControl.new121n.version = version122n.operation = operation123if payload124n.payload_offset = 0125n.payload_size = payload.size126n.payload = payload127end128n129end130131def ntp_private(version, implementation, request_code, payload = nil)132n = NTPPrivate.new133n.version = version134n.implementation = implementation135n.request_code = request_code136n.payload = payload if payload137n138end139140def ntp_generic(version, mode)141n = NTPGeneric.new142n.version = version143n.mode = mode144n145end146147# Parses the given message and provides a description about the NTP message inside148def describe(message)149ntp = NTPGeneric.new.read(message)150"#{message.size}-byte version #{ntp.version} mode #{ntp.mode} reply"151end152end153end154end155156157