Adapters - Wrappers to use with construct lib

Adapters and other Construct utility classes

class xbox.sg.utils.adapters.CryptoTunnel(subcon)

Bases: construct.core.Subconstruct

Adapter/Tunnel for inline decryption of protected payloads.

Depending on the packet-type, acquiring the Initialization Vector and decrypting the protected payload happens differently: * ConnectResponse: The IV is delivered in the unprotected payload section of the packet and can be used directly * Messages: The IV is generated by encrypting the first 16 bytes of the packet header using the IV key.

Inline encryption using this Tunnel is not (yet) possible due to limitations in Construct. As explained above, the IV for Messages is generated by encrypting the first 16 bytes of the header. However, there’s currently no method of determining the length of the payload without building it first. Therefor, the IV wouldn’t be correct.

static decrypt(stream, context)
class xbox.sg.utils.adapters.JsonAdapter(subcon)

Bases: construct.core.Adapter

Construct-Adapter for JSON field.

Parses and dumps JSON.

class xbox.sg.utils.adapters.UUIDAdapter(encoding=None)

Bases: construct.core.Adapter

__init__(encoding=None)

Construct-Adapter for UUID field.

Parses either utf8 encoded or raw byte strings into UUID instances.

Parameters

encoding (str) – The encoding to use.

class xbox.sg.utils.adapters.CertificateAdapter

Bases: construct.core.Adapter

__init__()

Construct-Adapter for Certificate field.

Parses and dumps the DER certificate as used in the discovery response messages.

class xbox.sg.utils.adapters.CertificateInfo(raw_cert)

Bases: object

__init__(raw_cert)

Helper class for parsing a x509 certificate.

Extracts common_name and public_key from the certificate.

Parameters

raw_cert (bytes) – The DER certificate to parse.

dump(encoding=<Encoding.DER: 'DER'>)
class xbox.sg.utils.adapters.XSwitch(keyfunc, cases, default=None)

Bases: construct.core.Switch

class xbox.sg.utils.adapters.XEnum(subcon, enum=None)

Bases: construct.core.Adapter

__init__(subcon, enum=None)

Construct-Adapter for Enum field.

Parses numeric fields into XEnumInt’s, which display the Enum name and value.

Parameters
  • subcon (Construct) – The subcon to adapt.

  • enum (Enum) – The enum to parse into.

class xbox.sg.utils.adapters.XInject(code)

Bases: construct.core.Construct

class xbox.sg.utils.adapters.TerminatedField(subcon, length=1, pattern=b'\x00')

Bases: construct.core.Subconstruct

__init__(subcon, length=1, pattern=b'\x00')

A custom Subconstruct that adds a termination character at the end of the child struct.

Parameters
  • subcon (Construct) – The subcon to add the terminated character to.

  • length (int) – The amount of termination characters to add.

  • pattern (bytes) – The termination pattern to use.

xbox.sg.utils.adapters.SGString(encoding='utf8')

Defines a null terminated PascalString.

The Smartglass protocol seems to always add a termination character behind a length prefixed string. This utility function combines a PascalString with a TerminatedField.

Parameters

encoding (str) – The string encoding to use for the PascalString.

Returns

A null byte terminated PascalString.

Return type

SGString

xbox.sg.utils.adapters.PrefixedBytes(lengthfield)

Defines a length prefixed bytearray.

Parameters

lengthfield (Subconstruct) – The length subcon.

Returns

A length prefixed bytesarray

Return type

PrefixedBytes

class xbox.sg.utils.adapters.FieldIn(field, options)

Bases: object

Helper class for creating an in conditional.

Operates like field in options.

Parameters
  • field – The struct field to use.

  • options (list) – A list with options to execute the in conditional on.

Returns

Whether or not the field value was in the options.

Return type

bool