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/acpp/message.rb
Views: 11704
# -*- coding: binary -*-12module Rex3module Proto4module ACPP5# From what I've been able to gather from the very limited findings on the6# web about this protocol, playing with it against a real Airport device and7# referencing the airport-utils package in Debian/Ubuntu, the format of (at8# least) the login message is:9#10# acpp # the header tag, always exactly acpp (4 bytes)11# unknown1 # unknown 4-byte field. Almost always 0x0000000112# messageChecksum # checksum of the message, 4 bytes13# payloadChecksum # checksum of the payload, 4 bytes14# payloadSize # size of the payload, 4 bytes15# unknown2 # unknown 8-byte field. probably some sort of16# request/response identifier. generally 0 for requests, 1 for replies17# messageType # the type of message, 4 bytes. see below.18# status # the status of this message, 4 bytes.19# generally 0 for success and !0 for failure.20# unknown3 # unknown 12-byte field, seemingly always 0. Probably 'reserved'21# password # 32-byte password, 'encrypted' by XOR'ing it with a 256-byte static key (see XOR_KEY)22# unknown4 # unknown 48-byte field, always 0.23#24# There are several possible message types:25#26# * 20 -- retrieve settings (payload is some list of settings to obtain)27# * 21 -- update settings (and if the 'acRB' setting is set, it reboots)28# * 3 -- Upload firmware29#30# TODO: if you find more, add them above.31#32# When the message type is anything other than 20 or 3, payloadSize is set to -1 and33# payloadChecksum is set to 1. It may be a bug that 21 doesn't look at the34# checksum. Adler32 is used to compute the checksum.35#36# The message payload is a bit of an unknown right now, as it *seems* like37# the payload always comes in a subsequent request. Simply appending38# a payload to the existing message does not appear to work (but this needs39# more testing)4041# This was taken from airport-util's AirportInforRecord for ease of copying, but can42# also be obtained by XOR'ing the null-padded known plain text with the appropriate 32-byte43# ciphertext from an airport-util request44XOR_KEY = [4514, 57, -8, 5, -60, 1, 85, 79, 12, -84,46-123, 125, -122, -118, -75, 23, 62, 9, -56, 53,47-12, 49, 101, 127, 60, -100, -75, 109, -106, -102,48-91, 7, 46, 25, -40, 37, -28, 33, 117, 111,4944, -116, -91, -99, 102, 106, 85, -9, -34, -23,5040, -43, 20, -47, -123, -97, -36, 124, 85, -115,51118, 122, 69, -25, -50, -7, 56, -59, 4, -63,52-107, -113, -52, 108, 69, -67, 70, 74, 117, -41,53-2, -55, 8, -11, 52, -15, -91, -65, -4, 92,54117, -83, 86, 90, 101, -57, -18, -39, 24, -27,5536, -31, -75, -81, -20, 76, 101, -35, 38, 42,5621, -73, -98, -87, 104, -107, 84, -111, -59, -33,57-100, 60, 21, -51, 54, 58, 5, -89, -114, -71,58120, -123, 68, -127, -43, -49, -116, 44, 5, -3,596, 10, 53, -105, -66, -119, 72, -75, 116, -79,60-27, -1, -68, 28, 53, -19, 22, 26, 37, -121,61-82, -103, 88, -91, 100, -95, -11, -17, -84, 12,6237, 29, -26, -22, -43, 119, 94, 105, -88, 85,63-108, 81, 5, 31, 92, -4, -43, 13, -10, -6,64-59, 103, 78, 121, -72, 69, -124, 65, 21, 15,6576, -20, -59, 61, -58, -54, -11, 87, 126, 73,66-120, 117, -76, 113, 37, 63, 124, -36, -11, 45,67-42, -38, -27, 71, 110, 89, -104, 101, -92, 97,6853, 47, 108, -52, -27, 93, -90, -86, -107, 55,6930, 41, -24, 21, -44, 17, 69, 95, 28, -68,70-107, 77, -74, -70, -123, 3971].pack("C*")7273class Message74# @return [Integer] the type of this message75attr_accessor :type76# @return [String] the password to attempt to authenticate with77attr_accessor :password78# @return [String] the optional message payload79attr_accessor :payload80# @return [Integer] the status of this message81attr_accessor :status8283def initialize84@payload = ''85@type = 086@status = 087@password = ''88@unknown1 = 189@unknown2 = ''90@unknown3 = ''91@unknown4 = ''92end9394# Determines if this message has a successful status code95#96# @return [Boolean] true iff @status is 0, false otherwise97def successful?98@status == 099end100101# Get this Message as a String102#103# @return [String] the string representation of this Message104def to_s105with_checksum(Zlib.adler32(with_checksum(0)))106end107108# Compares this Message and another Message for equality109#110# @param other [Message] the Message to compare111# @return [Boolean] true iff the two messages are equal, false otherwise112def ==(other)113other.type == @type &&114other.status == @status &&115other.password == @password &&116other.payload == @payload117end118119# Decodes the provided data into a Message120#121# @param data [String] the data to parse as a Message122# @param validate_checksum [Boolean] true to validate the message and123# payload checksums, false to not. Defaults to true.124# @return [Message] the decoded Message125def self.decode(data, validate_checksum = true)126data = data.dup127fail "Incorrect ACPP message size #{data.size} -- must be 128" unless data.size == 128128fail 'Unexpected header' unless 'acpp' == data.slice!(0, 4)129_unknown1 = data.slice!(0, 4)130read_message_checksum = data.slice!(0, 4).unpack('N').first131read_payload_checksum = data.slice!(0, 4).unpack('N').first132_read_payload_size = data.slice!(0, 4).unpack('N').first133_unknown2 = data.slice!(0, 8)134type = data.slice!(0, 4).unpack('N').first135status = data.slice!(0, 4).unpack('N').first136_unknown3 = data.slice!(0, 12)137password = Rex::Encoding::Xor::Generic.encode(data.slice!(0, 32), XOR_KEY).first.strip138_unknown4 = data.slice!(0, 48)139payload = data140m = new141m.type = type142m.password = password143m.status = status144m.payload = payload145146# we can now validate the checksums if desired147if validate_checksum148actual_message_checksum = Zlib.adler32(m.with_checksum(0))149if actual_message_checksum != read_message_checksum150fail "Invalid message checksum (expected #{read_message_checksum}, calculated #{actual_message_checksum})"151end152# I'm not sure this can ever happen -- if the payload checksum is wrong, then the153# message checksum will also be wrong. So, either I misunderstand the protocol154# or having two checksums is useless155actual_payload_checksum = Zlib.adler32(payload)156if actual_payload_checksum != read_payload_checksum157fail "Invalid payload checksum (expected #{read_payload_checksum}, calculated #{actual_payload_checksum})"158end159end160m161end162163def with_checksum(message_checksum)164[165'acpp',166@unknown1,167message_checksum,168Zlib.adler32(@payload),169@payload.size,170@unknown2,171@type,172@status,173@unknown3,174Rex::Encoding::Xor::Generic.encode([@password].pack('a32').slice(0, 32), XOR_KEY).first,175@unknown4,176payload177].pack('a4NNNNa8NNa12a32a48a*')178end179end180end181end182end183184185