Beispiel #1
0
    *dll_pair("urlmon"),
    *dll_pair("user32"),
    *dll_pair("userenv"),
    *dll_pair("version"),
    *dll_pair("winhttp"),
    *dll_pair("wininet"),
    *dll_pair("winmm"),
    *dll_pair("winspool", extension="drv"),
    *dll_pair("ws2_32"),
    *dll_pair("wsock32"),
    *dll_pair("wtsapi32"),
    # GdiPlus ?
]

CV_RSDS_HEADER = "CV_RSDS" / Struct(
    "Signature" / Const(b"RSDS", Bytes(4)),
    "GUID" / Struct(
        "Data1" / Int32ul,
        "Data2" / Int16ul,
        "Data3" / Int16ul,
        "Data4" / Bytes(8),
    ),
    "Age" / Int32ul,
    "Filename" / CString(encoding="utf8"),
)

# Derived from rekall
TYPE_ENUM_TO_VTYPE = {
    "T_32PINT4": ["Pointer", dict(target="long")],
    "T_32PLONG": ["Pointer", dict(target="long")],
    "T_32PQUAD": ["Pointer", dict(target="long long")],
Beispiel #2
0
class WLFATFS:
    # pylint: disable=too-many-instance-attributes
    CFG_SECTORS_COUNT = 1
    DUMMY_SECTORS_COUNT = 1
    WL_CONFIG_HEADER_SIZE = 48
    WL_STATE_RECORD_SIZE = 16
    WL_STATE_HEADER_SIZE = 64
    WL_STATE_COPY_COUNT = 2
    WL_SECTOR_SIZE = 0x1000

    WL_STATE_T_DATA = Struct('pos' / Int32ul, 'max_pos' / Int32ul,
                             'move_count' / Int32ul, 'access_count' / Int32ul,
                             'max_count' / Int32ul, 'block_size' / Int32ul,
                             'version' / Int32ul, 'device_id' / Int32ul,
                             'reserved' / Const(28 * b'\x00'))

    WL_CONFIG_T_DATA = Struct('start_addr' / Int32ul,
                              'full_mem_size' / Int32ul, 'page_size' / Int32ul,
                              'sector_size' / Int32ul, 'updaterate' / Int32ul,
                              'wr_size' / Int32ul, 'version' / Int32ul,
                              'temp_buff_size' / Int32ul)
    WL_CONFIG_T_HEADER_SIZE = 48

    def __init__(self,
                 size: int = 1024 * 1024,
                 reserved_sectors_cnt: int = 1,
                 fat_tables_cnt: int = 1,
                 sectors_per_cluster: int = 1,
                 sector_size: int = 0x1000,
                 sectors_per_fat: int = 1,
                 explicit_fat_type: int = None,
                 hidden_sectors: int = 0,
                 long_names_enabled: bool = False,
                 entry_size: int = 32,
                 num_heads: int = 0xff,
                 oem_name: str = 'MSDOS5.0',
                 sec_per_track: int = 0x3f,
                 volume_label: str = 'Espressif',
                 file_sys_type: str = 'FAT',
                 use_default_datetime: bool = True,
                 version: int = 2,
                 temp_buff_size: int = 32,
                 update_rate: int = 16,
                 device_id: int = None,
                 root_entry_count: int = 512,
                 media_type: int = 0xf8) -> None:
        if sector_size != WLFATFS.WL_SECTOR_SIZE:
            raise NotImplementedError(
                f'The only supported sector size is currently {WLFATFS.WL_SECTOR_SIZE}'
            )

        self._initialized = False
        self.sector_size = sector_size
        self._version = version
        self._temp_buff_size = temp_buff_size
        self._device_id = device_id
        self._update_rate = update_rate
        self.partition_size = size
        self.total_sectors = self.partition_size // self.sector_size
        self.wl_state_size = WLFATFS.WL_STATE_HEADER_SIZE + WLFATFS.WL_STATE_RECORD_SIZE * self.total_sectors

        # determine the number of required sectors (roundup to sector size)
        self.wl_state_sectors = (self.wl_state_size + self.sector_size -
                                 1) // self.sector_size

        self.boot_sector_start = self.sector_size  # shift by one "dummy" sector
        self.fat_table_start = self.boot_sector_start + reserved_sectors_cnt * self.sector_size

        wl_sectors = WLFATFS.DUMMY_SECTORS_COUNT + WLFATFS.CFG_SECTORS_COUNT + self.wl_state_sectors * 2
        self.plain_fat_sectors = self.total_sectors - wl_sectors

        self.plain_fatfs = FATFS(explicit_fat_type=explicit_fat_type,
                                 size=self.plain_fat_sectors *
                                 self.sector_size,
                                 reserved_sectors_cnt=reserved_sectors_cnt,
                                 fat_tables_cnt=fat_tables_cnt,
                                 sectors_per_cluster=sectors_per_cluster,
                                 sector_size=sector_size,
                                 sectors_per_fat=sectors_per_fat,
                                 root_entry_count=root_entry_count,
                                 hidden_sectors=hidden_sectors,
                                 long_names_enabled=long_names_enabled,
                                 entry_size=entry_size,
                                 num_heads=num_heads,
                                 use_default_datetime=use_default_datetime,
                                 oem_name=oem_name,
                                 sec_per_track=sec_per_track,
                                 volume_label=volume_label,
                                 file_sys_type=file_sys_type,
                                 media_type=media_type)

        self.fatfs_binary_image = self.plain_fatfs.state.binary_image

    def init_wl(self) -> None:
        self.fatfs_binary_image = self.plain_fatfs.state.binary_image
        self._add_dummy_sector()
        # config must be added after state, do not change the order of these two calls!
        self._add_state_sectors()
        self._add_config_sector()
        self._initialized = True

    def _add_dummy_sector(self) -> None:
        self.fatfs_binary_image = self.sector_size * b'\xff' + self.fatfs_binary_image

    def _add_config_sector(self) -> None:
        wl_config_data = WLFATFS.WL_CONFIG_T_DATA.build(
            dict(start_addr=0,
                 full_mem_size=self.partition_size,
                 page_size=self.sector_size,
                 sector_size=self.sector_size,
                 updaterate=self._update_rate,
                 wr_size=16,
                 version=self._version,
                 temp_buff_size=self._temp_buff_size))

        crc = crc32(list(wl_config_data), UINT32_MAX)
        wl_config_crc = Int32ul.build(crc)

        # adding three 4 byte zeros to align the structure
        wl_config = wl_config_data + wl_config_crc + Int32ul.build(
            0) + Int32ul.build(0) + Int32ul.build(0)

        self.fatfs_binary_image += (
            wl_config +
            (self.sector_size - WLFATFS.WL_CONFIG_HEADER_SIZE) * b'\xff')

    def _add_state_sectors(self) -> None:
        wl_state_data = WLFATFS.WL_STATE_T_DATA.build(
            dict(
                pos=0,
                max_pos=self.plain_fat_sectors + WLFATFS.DUMMY_SECTORS_COUNT,
                move_count=0,
                access_count=0,
                max_count=self._update_rate,
                block_size=self.sector_size,
                version=self._version,
                device_id=self._device_id or generate_4bytes_random(),
            ))
        crc = crc32(list(wl_state_data), UINT32_MAX)
        wl_state_crc = Int32ul.build(crc)
        wl_state = wl_state_data + wl_state_crc
        self.fatfs_binary_image += WLFATFS.WL_STATE_COPY_COUNT * (
            (wl_state +
             (self.sector_size - WLFATFS.WL_STATE_HEADER_SIZE) * b'\xff') +
            (self.wl_state_sectors - 1) * self.sector_size * b'\xff')

    def wl_write_filesystem(self, output_path: str) -> None:
        if not self._initialized:
            raise WLNotInitialized(
                'FATFS is not initialized with WL. First call method WLFATFS.init_wl!'
            )
        with open(output_path, 'wb') as output:
            output.write(bytearray(self.fatfs_binary_image))

    def wl_generate(self, input_directory: str) -> None:
        """
        Normalize path to folder and recursively encode folder to binary image
        """
        self.plain_fatfs.generate(input_directory=input_directory)

    def wl_create_file(self,
                       name: str,
                       extension: str = '',
                       path_from_root: Optional[List[str]] = None) -> None:
        self.plain_fatfs.create_file(name, extension, path_from_root)

    def wl_create_directory(
            self,
            name: str,
            path_from_root: Optional[List[str]] = None) -> None:
        self.plain_fatfs.create_directory(name, path_from_root)

    def wl_write_content(self, path_from_root: List[str],
                         content: bytes) -> None:
        self.plain_fatfs.write_content(path_from_root, content)
Beispiel #3
0
    RESET_CUSTOM_CERTIFICATE = 19
    DELETE_APP_BY_HASH = 21
    MCU_BOOTLOADER = 0xB0


LOAD_SEGMENT_CHUNK_HEADER_LENGTH = 3
MIN_PADDING_LENGTH = 1
SCP_MAC_LENGTH = 0xE


LEDGER_HSM_URL = "https://hsmprod.hardwarewallet.com/hsm/process"
LEDGER_HSM_KEY = "perso_11"


ApduListAppsResponse = Struct(
    Const(b"\x01"),  # Version
    apps=GreedyRange(
        Struct(
            # Application
            # Prefixed by the size of the structure, size included.
            _size=Rebuild(Int8ub, 1 + 4 + 32 + 32 + len_(this.name)),
            flags=Hex(Int32ub),
            code_data_hash=Bytes(32),
            full_hash=Bytes(32),
            name=PascalString(Int8ub, "utf-8"),
        )
    ),
)

VersionInfo = Struct(
    target_id=Hex(Int32ub),
Beispiel #4
0
from .artist import Artist
from .album import Album
from .playlist import Playlist
from .playlist_map import PlaylistMap
from .artwork import Artwork
from .color import Color
from .genre import Genre
from .key import Key
from .label import Label

# a strange page exists for every (?) page type, header.u9 is 1004 and page is filled with 0xf8ffff1f
StrangePage = Struct(
  "strange_header" / Struct(
    "index" / Int32ul, # page index (same as header?)
    "next_index" / Int32ul, # index of next page containing real data or 0x3ffffff if next page empty
    Const(0x3fffffff, Int32ul),
    Padding(4),
    "entry_count" / Int16ul, # number of 4-byte values
    "u2" / Int16ul, # always 8191?
    ),
  Array(1004, Int32ul),
  Padding(20)
)

ReverseIndexedEntry = FocusedSeq(1,
  "entry_offset" / Int16ul,
  "entry" / Pointer(this._._.entries_start+this.entry_offset,
    Switch(lambda ctx: "strange" if ctx._._.is_strange_page else ctx._._.page_type, {
      "block_tracks": Track,
      "block_artists": Artist,
      "block_albums": Album,
Beispiel #5
0
"""All low level structures used for parsing eddystone packets."""
from construct import Struct, Byte, Const, Int8sl, Array, Int16ub

from ..const import IBEACON_COMPANY_ID, IBEACON_PROXIMITY_TPYE

# pylint: disable=invalid-name

IBeaconAdvertisingPacket = Struct(
    "flags" / Const(b"\x02\x01\x06"),
    "length" / Const(b"\x1A"),
    "type" / Const(b"\xFF"),
    "company_id" / Const(IBEACON_COMPANY_ID),
    "beacon_type" / Const(IBEACON_PROXIMITY_TPYE),
    "uuid" / Array(16, Byte),
    "major" / Int16ub,
    "minor" / Int16ub,
    "tx_power" / Int8sl,
)
Beispiel #6
0
#	B			unsigned byte					1
#	h			short int						2
#	H			unsigned short int				2
#	i			int								4
#	I			unsigned int					4
#	l			long							4
#	L			unsigned long					4
#	Q			unsigned long long				8
#	f			float							4
#	d			double							8
#	p			number and character			1 + number
#	s			character						number
#
########################## format sign ############################

try:
    from construct import Struct, Magic, UBInt32, Const, String
    fmt = Struct('png', Magic(b'\x89PNG\r\n\x1a\n'), UBInt32('length'),
                 Const(String('type', 4), b'IHDR'), UBInt32('width'),
                 UBInt32('height'))
    result = fmt.parse(data)
    print(result)
except:
    print(" construct error ")

# transform bytes and strings
import binascii
print(binascii.hexlify(valid_png_header))

print(binascii.unhexlify(binascii.hexlify(valid_png_header)))

def _load_string_block(c):
    """Load a string block from the construct.Struct context."""
    strings = {}
    index = 0
    for string in [s.decode('utf-8') for s in c.string_block.split(b'\x00')]:
        strings[index] = string
        index += len(string) + 1

    return strings


DBCFile = Struct(
    'header' / Struct(
        Const(b'WDBC'),
        'record_count' / Int32ul,
        'field_count' / Int32ul,
        'record_size' / Int32ul,
        'string_block_size' / Int32ul,
    ),
    'records' / Array(
        lambda c: c.header.record_count,
        Bytes(lambda c: c.header.record_size),
    ),
    'string_block' / Bytes(lambda c: c.header.string_block_size),
    'strings' / Computed(_load_string_block),
)


class MPQMultiArchive:
Beispiel #8
0
import collections

from construct import Struct, RawCopy, BitStruct, Const, Nibble, Flag, Rebuild, Int8ub, BitsInteger, Int16ub, Checksum, \
    Bytes, this, Default, Padding, Enum, Int24ub, ExprAdapter, Byte, obj_, Array, Computed, Subconstruct, \
    ValidationError, ExprSymmetricAdapter

from .adapters import PGMFlags, StatusAdapter, DateAdapter, ZoneFlags, PartitionStatus, EventAdapter, \
    ModuleSerialAdapter
from ..common import CommunicationSourceIDEnum, ProductIdEnum, calculate_checksum

LoginConfirmationResponse = Struct(
    "fields" / RawCopy(
        Struct(
            "po" / BitStruct(
                "command" / Const(0x1, Nibble), "status" / Struct(
                    "reserved" / Flag, "alarm_reporting_pending" / Flag,
                    "Windload_connected" / Flag, "NeWare_connected" / Flag)),
            "length" / Rebuild(
                Int8ub, lambda this: this._root._subcons.fields.sizeof() + this
                ._root._subcons.checksum.sizeof()), "result" /
            BitStruct("not_used0" / BitsInteger(4), "partition_2" / Flag,
                      "not_used1" / BitsInteger(3)), "callback" / Int16ub)),
    "checksum" / Checksum(Bytes(1), lambda data: calculate_checksum(data),
                          this.fields.data))

InitializeCommunication = Struct(
    "fields" / RawCopy(
        Struct(
            "po" / Struct("command" / Const(0x00, Int8ub)),
            "module_address" / Default(Int8ub, 0x00),
            "not_used0" / Padding(2),
Beispiel #9
0
class FATFS:
    """
    The class FATFS provides API for generating FAT file system.
    It contains reference to the FAT table and to the root directory.
    """
    MAX_VOL_LAB_SIZE = 11
    MAX_OEM_NAME_SIZE = 8
    MAX_FS_TYPE_SIZE = 8
    BOOT_HEADER_SIZE = 512

    BOOT_SECTOR_HEADER = Struct(
        'BS_jmpBoot' / Const(b'\xeb\xfe\x90'),
        'BS_OEMName' / PaddedString(MAX_OEM_NAME_SIZE, 'utf-8'),
        'BPB_BytsPerSec' / Int16ul, 'BPB_SecPerClus' / Int8ul,
        'BPB_RsvdSecCnt' / Int16ul, 'BPB_NumFATs' / Int8ul,
        'BPB_RootEntCnt' / Int16ul, 'BPB_TotSec16' / Int16ul,
        'BPB_Media' / Int8ul, 'BPB_FATSz16' / Int16ul,
        'BPB_SecPerTrk' / Int16ul, 'BPB_NumHeads' / Int16ul,
        'BPB_HiddSec' / Int32ul, 'BPB_TotSec32' / Int32ul,
        'BS_DrvNum' / Const(b'\x80'), 'BS_Reserved1' / Const(b'\x00'),
        'BS_BootSig' / Const(b'\x29'), 'BS_VolID' / Int32ul,
        'BS_VolLab' / PaddedString(MAX_VOL_LAB_SIZE, 'utf-8'),
        'BS_FilSysType' / PaddedString(MAX_FS_TYPE_SIZE, 'utf-8'), 'BS_EMPTY' /
        Const(448 * b'\x00'), 'Signature_word' / Const(b'\x55\xAA'))

    def __init__(self,
                 binary_image_path: Optional[str] = None,
                 size: int = 1024 * 1024,
                 reserved_sectors_cnt: int = 1,
                 fat_tables_cnt: int = 1,
                 sectors_per_cluster: int = 1,
                 sector_size: int = 0x1000,
                 sectors_per_fat: int = 1,
                 root_dir_sectors_cnt: int = 4,
                 hidden_sectors: int = 0,
                 long_names_enabled: bool = False,
                 entry_size: int = 32,
                 wl_sectors: int = 0,
                 num_heads: int = 0xff,
                 oem_name: str = 'MSDOS5.0',
                 sec_per_track: int = 0x3f,
                 volume_label: str = 'Espressif',
                 file_sys_type: str = 'FAT',
                 media_type: int = 0xf8) -> None:

        self.state = FATFSState(entry_size=entry_size,
                                sector_size=sector_size,
                                reserved_sectors_cnt=reserved_sectors_cnt,
                                root_dir_sectors_cnt=root_dir_sectors_cnt,
                                size=size,
                                file_sys_type=file_sys_type,
                                num_heads=num_heads,
                                fat_tables_cnt=fat_tables_cnt,
                                sectors_per_fat=sectors_per_fat,
                                sectors_per_cluster=sectors_per_cluster,
                                media_type=media_type,
                                hidden_sectors=hidden_sectors,
                                sec_per_track=sec_per_track,
                                long_names_enabled=long_names_enabled,
                                volume_label=volume_label,
                                wl_sectors=wl_sectors,
                                oem_name=oem_name)
        binary_image = bytearray(
            self.read_filesystem(binary_image_path)
            if binary_image_path else self.create_empty_fatfs())
        self.state.binary_image = binary_image

        self.fat = FAT(fatfs_state=self.state,
                       reserved_sectors_cnt=self.state.reserved_sectors_cnt)

        self.root_directory = Directory(
            name='A',  # the name is not important
            size=self.state.root_dir_sectors_cnt * self.state.sector_size,
            fat=self.fat,
            cluster=self.fat.clusters[1],
            fatfs_state=self.state)
        self.root_directory.init_directory()

    def create_file(self,
                    name: str,
                    extension: str = '',
                    path_from_root: Optional[List[str]] = None) -> None:
        # when path_from_root is None the dir is root
        self.root_directory.new_file(name=name,
                                     extension=extension,
                                     path_from_root=path_from_root)

    def create_directory(self,
                         name: str,
                         path_from_root: Optional[List[str]] = None) -> None:
        # when path_from_root is None the dir is root
        parent_dir = self.root_directory
        if path_from_root:
            parent_dir = self.root_directory.recursive_search(
                path_from_root, self.root_directory)
        self.root_directory.new_directory(name=name,
                                          parent=parent_dir,
                                          path_from_root=path_from_root)

    def write_content(self, path_from_root: List[str], content: str) -> None:
        """
        fat fs invokes root directory to recursively find the required file and writes the content
        """
        self.root_directory.write_to_file(path_from_root, content)

    def create_empty_fatfs(self) -> Any:
        sectors_count = self.state.size // self.state.sector_size
        volume_uuid = uuid.uuid4().int & 0xFFFFFFFF
        return (FATFS.BOOT_SECTOR_HEADER.build(
            dict(BS_OEMName=pad_string(self.state.oem_name,
                                       size=FATFS.MAX_OEM_NAME_SIZE),
                 BPB_BytsPerSec=self.state.sectors_per_cluster *
                 self.state.sector_size,
                 BPB_SecPerClus=self.state.sectors_per_cluster,
                 BPB_RsvdSecCnt=self.state.reserved_sectors_cnt,
                 BPB_NumFATs=self.state.fat_tables_cnt,
                 BPB_RootEntCnt=self.state.entries_root_count,
                 BPB_TotSec16=0x00 if self.state.fatfs_type == FATFSState.FAT32
                 else sectors_count,
                 BPB_Media=self.state.media_type,
                 BPB_FATSz16=self.state.sectors_per_fat_cnt,
                 BPB_SecPerTrk=self.state.sec_per_track,
                 BPB_NumHeads=self.state.num_heads,
                 BPB_HiddSec=self.state.hidden_sectors,
                 BPB_TotSec32=sectors_count
                 if self.state.fatfs_type == FATFSState.FAT32 else 0x00,
                 BS_VolID=volume_uuid,
                 BS_VolLab=pad_string(self.state.volume_label,
                                      size=FATFS.MAX_VOL_LAB_SIZE),
                 BS_FilSysType=pad_string(self.state.file_sys_type,
                                          size=FATFS.MAX_FS_TYPE_SIZE))) +
                (self.state.sector_size - FATFS.BOOT_HEADER_SIZE) * b'\x00' +
                self.state.sectors_per_fat_cnt * self.state.fat_tables_cnt *
                self.state.sector_size * b'\x00' +
                self.state.root_dir_sectors_cnt * self.state.sector_size *
                b'\x00' +
                self.state.data_sectors * self.state.sector_size * b'\xff')

    @staticmethod
    def read_filesystem(path: str) -> bytearray:
        with open(path, 'rb') as fs_file:
            return bytearray(fs_file.read())

    def write_filesystem(self, output_path: str) -> None:
        with open(output_path, 'wb') as output:
            output.write(bytearray(self.state.binary_image))

    def _generate_partition_from_folder(self,
                                        folder_relative_path: str,
                                        folder_path: str = '',
                                        is_dir: bool = False) -> None:
        """
        Given path to folder and folder name recursively encodes folder into binary image.
        Used by method generate
        """
        real_path = os.path.join(folder_path, folder_relative_path)
        smaller_path = folder_relative_path

        folder_relative_path = folder_relative_path.upper()

        normal_path = os.path.normpath(folder_relative_path)
        split_path = normal_path.split(os.sep)
        if os.path.isfile(real_path):
            with open(real_path) as file:
                content = file.read()
            file_name, extension = os.path.splitext(split_path[-1])
            extension = extension[1:]  # remove the dot from the extension
            self.create_file(name=file_name,
                             extension=extension,
                             path_from_root=split_path[1:-1] or None)
            self.write_content(split_path[1:], content)
        elif os.path.isdir(real_path):
            if not is_dir:
                self.create_directory(split_path[-1], split_path[1:-1])

            # sorting files for better testability
            dir_content = list(sorted(os.listdir(real_path)))
            for path in dir_content:
                self._generate_partition_from_folder(os.path.join(
                    smaller_path, path),
                                                     folder_path=folder_path)

    def generate(self, input_directory: str) -> None:
        """
        Normalize path to folder and recursively encode folder to binary image
        """
        path_to_folder, folder_name = os.path.split(input_directory)
        self._generate_partition_from_folder(folder_name,
                                             folder_path=path_to_folder,
                                             is_dir=True)
Beispiel #10
0
                       Struct, this)

from mgz.body.achievements import achievements
from mgz.enums import (DiplomacyStanceEnum, FormationEnum, GameActionModeEnum,
                       OrderTypeEnum, ReleaseTypeEnum, ResourceEnum,
                       ResourceLevelEnum, RevealMapEnum, StanceEnum,
                       StartingAgeEnum, VictoryEnum)
from mgz.util import TimeSecAdapter

# pylint: disable=invalid-name

# Not all actions are defined, not all actions are complete.

interact = "interact"/Struct(
    "player_id"/Byte,
    Const(b"\x00\x00"),
    "target_id"/Int32ul,
    "selected"/Int32ul,
    "x"/Float32l,
    "y"/Float32l,
    If(lambda ctx: ctx.selected < 0xff, Array(
        lambda ctx: ctx.selected, "unit_ids"/Int32ul
    ))
)

ai_interact = "ai_interact"/Struct(
    Padding(3),
    "target_id"/Int32ul,
    "selected"/Byte,
    Padding(3),
    "x"/Float32l,
Beispiel #11
0
    difficulty=PrefixedArray(Byte, ConstructResourceInfo),
)

ConstructNode = Struct(name=CString("utf8"),
                       heal=Flag,
                       node_type=Byte,
                       data=Switch(
                           lambda this: this.node_type, {
                               1:
                               Struct(
                                   dock_index=Byte,
                                   connected_area_asset_id=Int32ub,
                                   connected_dock_index=Byte,
                                   dock_type=Byte,
                                   dock_weakness_index=Byte,
                                   _=Const(b"\x00\x00\x00"),
                               ),
                               2:
                               Struct(
                                   pickup_index=Byte,
                                   major_location=Flag,
                               ),
                               3:
                               Struct(
                                   destination_world_asset_id=Int32ub,
                                   destination_area_asset_id=Int32ub,
                                   teleporter_instance_id=Int32ub,
                                   keep_name_when_vanilla=Flag,
                                   editable=Flag,
                               ),
                               4:
Beispiel #12
0
    @command(default_output=format_output("Indicator LED status: {result}"))
    def get_indicator_led(self):
        """Get the indicator led status."""
        return self.send("get_indicatorLamp")


class ProntoPulseAdapter(Adapter):
    def _decode(self, obj, context, path):
        return int(obj * context._.modulation_period)

    def _encode(self, obj, context, path):
        raise RuntimeError("Not implemented")


ChuangmiIrSignal = Struct(
    Const(0xA567, Int16ul),
    "edge_count" / Rebuild(Int16ul,
                           len_(this.edge_pairs) * 2 - 1),
    "times_index" / Array(16, Int32ul),
    "edge_pairs" / Array(
        (this.edge_count + 1) // 2,
        BitStruct("gap" / BitsInteger(4), "pulse" / BitsInteger(4)),
    ),
)

ProntoBurstPair = Struct("pulse" / ProntoPulseAdapter(Int16ub),
                         "gap" / ProntoPulseAdapter(Int16ub))

Pronto = Struct(
    Const(0, Int16ub),
    "_ticks" / Int16ub,
Beispiel #13
0
from construct import Struct, RawCopy, Int8ub, Const, Padding, Int16ub, Default, Bytes, Enum, Int32ub, BitStruct, \
    BitsInteger, Flag, ExprAdapter, Byte, Nibble, obj_, Int8ul

from .adapters import StatusAdapter, DateAdapter, PartitionStatusAdapter, ZoneStatusAdapter, SignalStrengthAdapter, \
    ModuleSerialAdapter
from ..common import CommunicationSourceIDEnum, ProductIdEnum, PacketChecksum

InitializeCommunication = Struct(
    "fields" / RawCopy(
        Struct(
            "po" / Struct("command" / Const(0x00, Int8ub)),
            "_not_used0" / Padding(3),
            "product_id" / ProductIdEnum,
            "firmware" /
            Struct("version" / Int8ub, "revision" / Int8ub, "build" / Int8ub),
            "panel_id" / Int16ub,
            "pc_password" / Default(Bytes(2), b'0000'),
            "_not_used1" / Bytes(1),
            "source_method" / Default(
                Enum(Int8ub, Winload_Connection=0x00, NEware_Connection=0x55),
                0x00),
            "user_code" / Default(Int32ub, 0x00000000),
            "_not_used2" / Padding(15),
            "source_id" / Default(CommunicationSourceIDEnum, 1),
            "user_id" /
            Struct("high" / Default(Int8ub, 0), "low" / Default(Int8ub, 0)),
        )), "checksum" / PacketChecksum(Bytes(1)))

InitializeCommunicationResponse = Struct(
    "fields" / RawCopy(
        Struct(
Beispiel #14
0
  umnt = 3,
  umntall = 4,
  export = 5
)

MountMntArgs = PascalString(Int32ub, encoding="utf-16-le")

NfsFhandle = Bytes(32)

MountMntRes = Struct(
  "status" / Int32ub,
  "fhandle" / If(this.status == 0, NfsFhandle)
)

RpcCall = Struct(
  "rpcvers" / Const(Int32ub, 2),
  "prog" / RpcProgram,
  "vers" / Default(Int32ub, 2),
  "proc" / Switch(this.prog, {
    "portmap": PortmapProcedure,
    "nfs": NfsProcedure,
    "mount": MountProcedure
  }),
  "cred" / RpcOpaqueAuth,
  "verf" / RpcOpaqueAuth
)

RpcMismatchInfo = Struct(
  "low" / Int32ub,
  "high" / Int32ub
)
Beispiel #15
0
from .album import Album
from .playlist import Playlist
from .playlist_map import PlaylistMap
from .artwork import Artwork
from .color import Color
from .genre import Genre
from .key import Key
from .label import Label

# a strange page exists for every (?) page type, header.u9 is 1004 and page is filled with 0xf8ffff1f
StrangePage = Struct(
    "strange_header" / Struct(
        "index" / Int32ul,  # page index (same as header?)
        "next_index" /
        Int32ul,  # index of next page containing real data or 0x3ffffff if next page empty
        Const(Int32ul, 0x3fffffff),
        Padding(4),
        "entry_count" / Int16ul,  # number of 4-byte values
        "u2" / Int16ul,  # always 8191?
    ),
    Array(1004, Int32ul),
    Padding(20))

ReverseIndexedEntry = FocusedSeq(
    1,
    "entry_offset" / Int16ul,
    "entry" / Pointer(
        this._._.entries_start + this.entry_offset,
        Switch(
            lambda ctx: "strange"
            if ctx._._.is_strange_page else ctx._._.page_type,
Beispiel #16
0
    ushort w;
    ushort h;
    uchar levels;
    uchar hqPartLevels;
    ushort depth;
    ushort bitsPerPixel;
    uchar lQmip:4;
    uchar mQmip:4;
    uchar dxtShift:4;
    uchar uQmip:4;
    uint memSz;
    uint packedSz;
};
'''
ddsx_header = Struct(
    "label" / Const(b"DDSx"),
    "d3dFormat" / String(4, encoding=StringsAsBytes),
    # there error in parsing flags, fixed somewhere on future of construct, but not on 2.9.24
    "flags" / ddsx_flags_enum,
    "w" / Int16ul,
    "h" / Int16ul,
    "levels" / Int8ul,
    "hqPartLevels" / Int8ul,
    "depth" / Int16ul,
    "bitsPerPixel" / Int16ul,
    EmbeddedBitStruct(
        "lQmip" / Nibble,
        "mQmip" / Nibble,
        "dxtShift" / Nibble,
        "uQmip" / Nibble,
    ),
Beispiel #17
0
KeepAlivePacketSubtype = Enum(
    Int8ub,
    stype_hello=0x25,
    stype_number=0x26,
    stype_mac=0x2c,
    stype_ip=0x32,
    stype_status=0x36,
    stype_change=0x29,
    stype_status_mixer=0x00  # djm 900 nxs sends this stype on type_status
)

DeviceType = Enum(Int8ub, djm=1, cdj=2, rekordbox=3)

PlayerNumberAssignment = Enum(Int8ub, auto=1, manual=2)

UdpMagic = Const("Qspt1WmJOL", String(10, encoding="ascii"))

# received on udp port 50000
KeepAlivePacket = Struct(
    "magic" / UdpMagic,
    "type" / KeepAlivePacketType,  # pairs with subtype
    Padding(1),
    "model" / Padded(20, CString(encoding="ascii")),
    "u1" / Const(1, Int8ub),
    "device_type" / Default(DeviceType, "cdj"),
    Padding(1),
    "subtype" / KeepAlivePacketSubtype,
    "content" / Switch(
        this.type,
        {
            # type=0x0a, request for other players to propose a player number?
Beispiel #18
0
# fxp/fxb file format. (VST/Cubase's preset or "bank" files from before VST3
# era)
# based on VST SDK's vst2.x/vstfxstore.h
# names as in the source

from construct import Array, BFloat32, Bytes, Const, Container, Enum, \
    LazyBound, String, Struct, Switch, UBInt32, ULInt32

vst2preset = Struct(
    'vst2preset',
    Const(Bytes('chunkMagic', 4), 'CcnK'),
    UBInt32('byteSize'),
    Enum(
        Bytes('fxMagic', 4),
        FXP_PARAMS='FxCk',
        FXP_OPAQUE_CHUNK='FPCh',
        FXB_REGULAR='FxBk',
        FXB_OPAQUE_CHUNK='FBCh',
    ),
    UBInt32('version'),
    UBInt32('fxID'),
    UBInt32('fxVersion'),
    UBInt32('count'),
    Switch(
        'data',
        lambda ctx: ctx['fxMagic'],
        {
            'FXP_PARAMS':
            Struct(
                'data',
                String('prgName', 28, padchar='\0'),
Beispiel #19
0
    ConstructError,
    Enum,
    GreedyRange,
    If,
    Int16ul,
    Int32ul,
    PaddedString,
    Pointer,
    Seek,
    Struct,
    Tell,
    this,
)
from winsign.asn1 import der_encode, make_authenticode_signeddata

dos_stub = Struct("magic" / Const(b"MZ"), "pe_offset" / Pointer(0x3C, Int16ul))

coff_header = Struct(
    "signature" / Const(b"PE\x00\x00"),
    "machine" / Int16ul,
    "nsections" / Int16ul,
    "timestamp" / Int32ul,
    "symboltable_offset" / Int32ul,
    "nsymbols" / Int32ul,
    "optionalheader_size" / Int16ul,
    "characteristics" / Int16ul,
)


pe_header = Struct(
    "offset" / Tell,
                return json.loads(decoded)
            except Exception as ex:
                # log the error when decrypted bytes couldn't be loaded
                # after trying all quirk adaptions
                if i == len(decrypted_quirks) - 1:
                    _LOGGER.error("unable to parse json '%s': %s", decoded, ex)

        return None


Message = Struct(
    # for building we need data before anything else.
    "data" / Pointer(32, RawCopy(EncryptionAdapter(GreedyBytes))),
    "header"
    / RawCopy(
        Struct(
            Const(0x2131, Int16ub),
            "length" / Rebuild(Int16ub, Utils.get_length),
            "unknown" / Default(Int32ub, 0x00000000),
            "device_id" / Hex(Bytes(4)),
            "ts" / TimeAdapter(Default(Int32ub, datetime.datetime.utcnow())),
        )
    ),
    "checksum"
    / IfThenElse(
        Utils.is_hello,
        Bytes(16),
        Checksum(Bytes(16), Utils.md5, Utils.checksum_field_bytes),
    ),
)
Beispiel #21
0
"""Definitive Edition structure."""

from construct import (
    Struct, Int32ul, Float32l, Array, Padding, Flag, If,
    Byte, Int16ul, Bytes, Int32sl, Peek, Const, RepeatUntil,
    Int64ul, Computed
)

from mgz.enums import VictoryEnum, ResourceLevelEnum, AgeEnum, PlayerTypeEnum, DifficultyEnum
from mgz.util import find_save_version

# pylint: disable=invalid-name, bad-continuation

de_string = Struct(
    Const(b"\x60\x0A"),
    "length"/Int16ul,
    "value"/Bytes(lambda ctx: ctx.length)
)

separator = Const(b"\xa3_\x02\x00")

de = "de"/Struct(
    "version"/Float32l,
    "interval_version"/Int32ul,
    "game_options_version"/Int32ul,
    "dlc_count"/Int32ul,
    "dlc_ids"/Array(lambda ctx: ctx.dlc_count, Int32ul),
    "dataset_ref"/Int32ul,
    Peek("difficulty_id"/Int32ul),
    DifficultyEnum("difficulty"/Int32ul),
    "selected_map_id"/Int32ul,
Beispiel #22
0
    def _encode(self, obj, context, path):
        return {
            "column": obj.column,
            "relation": obj.relation,
            "action": obj.action,
            "value": obj.value
        }


RuleStruct = RuleStructAdapter(RawRuleStruct)

RawRulesStruct = """
Struct that contains a list of procmon rules.
""" * Struct(
    "reserved1" / Const(1, Int8ul) * "!!Unknown field!!",
    "rules_count" / Rebuild(Int8ul, lambda this: len(this.rules)),
    "rules" / Array(lambda this: this.rules_count, RuleStruct),
    "reserved1" / Default(Bytes(3), 0) * "!!Unknown field!!",
)


class RulesStructAdapter(Adapter):
    def _decode(self, obj, context, path):
        return list(obj["rules"])

    def _encode(self, obj, context, path):
        return {"rules": obj}


RulesStruct = RulesStructAdapter(RawRulesStruct)
Beispiel #23
0
AFC_HARDLINK = 1
AFC_SYMLINK = 2

AFC_LOCK_SH = 1 | 4  #/**< shared lock */
AFC_LOCK_EX = 2 | 4  #/**< exclusive lock */
AFC_LOCK_UN = 8 | 4  #/**< unlock */


if PY3:
    AFCMAGIC = b"CFA6LPAA"
else:
    AFCMAGIC = "CFA6LPAA"

AFCPacket = Struct(
    "magic" / Const(AFCMAGIC),
    "entire_length" / Int64ul,
    "this_length" / Int64ul,
    "packet_num" / Int64ul,
    "operation" / Int64ul,
)


class AFCClient(object):
    def __init__(self, lockdown=None, serviceName="com.apple.afc", service=None, udid=None, logger=None):
        self.logger = logger or logging.getLogger(__name__)
        self.serviceName = serviceName
        self.lockdown = lockdown if lockdown else LockdownClient(udid=udid)
        self.service = service if service else self.lockdown.start_service(self.serviceName)
        self.packet_num = 0
    return c & 0xFFFF


WriteCommand = Struct("address" / Int16ul, "value" / Int16ul)

WriteMessageRequest = Struct(
    "fields" / RawCopy(
        Struct(
            "length" / Rebuild(
                Int16ul,
                len_(this.items) * WriteCommand.sizeof() // Int16ul.sizeof() +
                2,
            ),
            "command" /
            Const(vlxDevConstants.WS_WEB_UI_COMMAND_WRITE_DATA, Int16ul),
            "items" / WriteCommand[len_(this.items)],
        )),
    "checksum" /
    Checksum(Int16ul, lambda data: checksum_16(data), this.fields.data),
)

ReadTableResponse = GreedyRange(Int16ub)

ReadTableRequest = Struct(
    "fields" / RawCopy(
        Struct(
            "length" / Const(3, Int16ul),
            "command" /
            Const(vlxDevConstants.WS_WEB_UI_COMMAND_READ_TABLES, Int16ul),
            "items" / Const(0, Int16ul),
Beispiel #25
0
        if hour_min & 0x01:
            min = 30
        hour = int(hour_min / 2)

        return datetime(year=year, month=month, day=day, hour=hour, minute=min)

    def _encode(self, obj, ctx):
        if obj.year < 2000 or obj.year > 2099:
            raise Exception("Invalid year, possible [2000,2099]")
        year = obj.year - 2000
        hour = obj.hour * 2
        if obj.minute:  # we encode all minute values to h:30
            hour |= 0x01

        value = struct.pack("BBBB", obj.day, year, hour, obj.month)
        return value


Status = "Status" / Struct(
    "cmd" / Const(Int8ub, PROP_INFO_RETURN), Const(Int8ub, 0x01),
    "mode" / ModeFlags, "valve" / Int8ub, Const(Int8ub, 0x04),
    "target_temp" / TempAdapter(Int8ub), "away" / IfThenElse(
        lambda ctx: ctx.mode.AWAY, AwayDataAdapter(Bytes(4)), GreedyBytes))

Schedule = "Schedule" / Struct(
    "cmd" / Enum(Int8ub, **NAME_TO_CMD), "day" / Enum(Int8ub, **NAME_TO_DAY),
    "base_temp" / TempAdapter(Int8ub), "next_change_at" / TimeAdapter(Int8ub),
    "hours" / GreedyRange(
        Struct("target_temp" / TempAdapter(Int8ub),
               "next_change_at" / TimeAdapter(Int8ub))))
"""All low level structures used for parsing eddystone packets."""
from construct import Struct, Byte, Switch, Const, OneOf, Int8sl, Array, \
                      Int16ul, Int16ub, Int32ub, GreedyString

from ..const import EDDYSTONE_UUID, EDDYSTONE_URL_SCHEMES, EDDYSTONE_TLM_UNENCRYPTED, \
                    EDDYSTONE_TLM_ENCRYPTED, EDDYSTONE_UID_FRAME, EDDYSTONE_URL_FRAME, \
                    EDDYSTONE_TLM_FRAME, EDDYSTONE_EID_FRAME, FLAGS_DATA_TYPE, \
                    SERVICE_DATA_TYPE, SERVICE_UUIDS_DATA_TYPE

# pylint: disable=invalid-name

EddystoneUIDFrame = Struct("tx_power" / Int8sl, "namespace" / Array(10, Byte),
                           "instance" / Array(6, Byte),
                           "rfu" / Const(b"\x00\x00"))

EddystoneURLFrame = Struct(
    "tx_power" / Int8sl,
    "url_scheme" / OneOf(Byte, list(EDDYSTONE_URL_SCHEMES)),
    "url" / GreedyString(encoding="ascii"))

UnencryptedTLMFrame = Struct(
    "voltage" / Int16ub,
    "temperature" / Int16ul,
    "advertising_count" / Int32ub,
    "seconds_since_boot" / Int32ub,
)

EncryptedTLMFrame = Struct("encrypted_data" / Array(12, Byte),
                           "salt" / Int16ul, "mic" / Int16ul)

EddystoneTLMFrame = Struct(
Beispiel #27
0
                        WHITE=4,
                        GREEN=5,
                        ORANGE=6,
                        PURPLE=7,
                        BROWN=8,
                        GRAY=9),
    "score" / Byte,
    "penalty_shot" / Byte,  # penalty shot counter
    "single_shots" / Short,  # bits represent penalty shot success
    "coach_sequence" / Byte,
    "coach_message" / Bytes(253),
    Renamed("coach", RobotInfo),
    "players" / Array(11, RobotInfo))

GameState = "gamedata" / Struct(
    "header" / Const(Bytes(4), b'RGme'),
    "version" / Const(Short, 12),
    "packet_number" / Byte,
    "players_per_team" / Byte,
    "game_type" / Byte,
    "game_state" / Enum(
        Byte,
        STATE_INITIAL=0,
        # auf startposition gehen
        STATE_READY=1,
        # bereithalten
        STATE_SET=2,
        # spielen
        STATE_PLAYING=3,
        # spiel zu ende
        STATE_FINISHED=4),
Beispiel #28
0
WARC_SECTION_ENTRY = Struct(
    'header' / Int16ub,
    'unk' / Int16ub,
    'offset' / Int32ub,
    'size' / Int32ub,
)

WARC_WAVE_ENTRY = Struct(
    'header' / Int16ub,
    'unk' / Int16ub,
    'offset' / Int32ub,
    'size' / Int32ub,
)

WARC_HEADER_ENTRY = Struct(
    'magic' / Const(Int16ub, 0x2207),
    'unk' / Int16ub,
    'offset' / Int32ub,
)

WARC_ENTRY = Struct(
    'file_id' / Int32ub,
    'unk1' / Int32ub,
    'unk2' / Int32ub,
    'name_string_id' / Int32ub,
)


def extract_warc(fp, file_sec_offset, file_table_entry):
    """
    Extract binary blobs (wavs) from a warc
Beispiel #29
0
from construct import Struct, Int8ul, Int16ul, Int32ul, Array, Const, Tell, Default
from .piostring import PioString, IndexedPioString

TRACK_ENTRY_MAGIC = 0x24

Track = Struct(
    "entry_start" / Tell,
    "magic" / Const(TRACK_ENTRY_MAGIC, Int16ul),
    "index_shift" /
    Int16ul,  # the index inside the page <<5 (0x00, 0x20, 0x40, ...)
    "bitmask" / Int32ul,
    "sample_rate" / Int32ul,
    "composer_index" / Int32ul,
    "file_size" / Int32ul,
    "u1" / Int32ul,  # some id?
    "u2" / Int16ul,  # always 19048?
    "u3" / Int16ul,  # always 30967?
    "artwork_id" / Int32ul,
    "key_id" / Int32ul,  # not sure
    "original_artist_id" / Int32ul,
    "label_id" / Int32ul,
    "remixer_id" / Int32ul,
    "bitrate" / Int32ul,
    "track_number" / Int32ul,
    "bpm_100" / Int32ul,
    "genre_id" / Int32ul,
    "album_id" / Int32ul,  # album artist is set in album entry
    "artist_id" / Int32ul,
    "id" / Int32ul,  # the rekordbox track id
    "disc_number" / Int16ul,
    "play_count" / Int16ul,
Beispiel #30
0
realOrComplex = Enum(Bytes(1), REAL=b'R', COMPLEX=b'C')
variableType = Enum(Bytes(2), VARIABLE=b'VM', LIST=b'LT', MATRIX=b'MT', IMAGE=b'PC', SCREENSHOT=b'DW')

# Checksum byte needs to be removed before the packet is parsed!

signinfobyte = BitStruct(
    "isComplex" / Flag,
    "isNegative" / Flag,
    Padding(1),
    "isNegative" / Flag,
    Padding(3),
    "expSignIsPositive" / Flag
)

real_value_packet = Struct(
    Const(b":"),
    Padding(1),
    "row" / Bytes(1),
    Padding(1),
    "col" / Bytes(1),
    "real_int" / Bytes(1),
    "real_frac" / Bytes(7),
    # signinfo should be a bitstruct
    "real_signinfo" / signinfobyte,
    "real_exponent" / Bytes(1)
)

complex_value_packet = Struct(
    Const(b":"),
    Padding(1),
    "row" / Bytes(1),