-
Couldn't load subscription status.
- Fork 26
binary
thawk edited this page Sep 22, 2020
·
1 revision
Binary二进制文件处理
包含:
-
.ksy语言,用于进行结构描述 - 编译器,把
.ksy翻译为多种语言的代码 - WebIDE,可视化分析数据结构
- Visualizer
- 一系列常见格式的定义
-
anamal.ksy
meta: id: animal_record endian: be seq: - id: uuid size: 16 - id: name type: str size: 24 encoding: UTF-8 - id: birth_year type: u2 - id: weight type: f8 - id: rating type: s4
- id : 结构名称
-
endian : 缺省字节序(
be/le)
字段数组。每项包含
-
id : 字段名
-
type : 字段类型
-
无类型 : 原始字节数组,
size描述了数组大小 -
整数
-
s1,s2,s4,u1,u2,u4等 -
s表示有符号数,u表示无符号数 - 可以加字节序后缀 :
s4be,u8le
-
-
精确到位的整数
-
b1: 只能true/false -
bX: 整数 - 后面的以字节位单位的整数将对齐字节边界
-
-
浮点数
-
f4/f8: IEEE 754 浮点数,4和8表示字节数,即float和double类型 - 可以加字节序后缀 :
f4be,f8le
-
-
字符串
str- 与无类型差不多,但可以用
encoding指定字符编码
-
无类型 : 原始字节数组,
-
contents : magic signatures
seq: - id: magic contents: [0xca, 0xfe, 0xba, 0xbe]
可以是
- UTF-8字符串
- 数组,成员可以是以下任意类型
- 十进制字节
- 十六进制字节,以
0x开头 - UTF-8字符串
- id: magic3 contents: [CAFE, 0, BABE] # expects bytes: 43 41 46 45 00 42 41 42 45
-
单独的长度字段
seq: - id: my_len type: u4 - id: my_str type: str size: my_len * 2 encoding: UTF-16LE
size可以引用其他字段,可以包含计算 -
字段结束于数据流末尾(
size-eos: true)seq: - id: some_int type: u4 - id: string_spanning_to_the_end_of_file type: str encoding: UTF-8 size-eos: true
-
分隔符
seq: - id: my_string type: str terminator: 0 encoding: UTF-8
type: strz可以代替最常见的type: str和terminator: 0 -
有最大长度的分隔符
如在一个固定16字节大小缓冲区中放置C格式变长字符串:
seq: - id: name type: str size: 16 terminator: 0 encoding: UTF-8
enums:
ip_protocol:
1: icmp
6: tcp
17: udpenums:
ip_protocol:
1:
id: icmp
doc: Internet Control Message Protocol
doc-ref: https://www.ietf.org/rfc/rfc792
6:
id: tcp
doc: Transmission Control Protocol
doc-ref: https://www.ietf.org/rfc/rfc793
17:
id: udp
doc: User Datagram Protocol
doc-ref: https://www.ietf.org/rfc/rfc768seq:
- id: header
type: main_header
- id: body
size: header.body_len
types:
main_header:
seq:
- id: magic
contents: MY-SUPER-FORMAT
- id: body_len
type: u4- 用
.作为类型与字段的分隔符 - 特殊字段
-
_parent: 父节点 -
_root: 根节点
-
- id: rating
type: s4
doc: Rating, can be negative- id: opcode
type: u1
doc: |
Operation code that defines which operation should be performed
by a virtual machine. Subsequent parameters for operation depend
on the value of opcode.seq:
- id: my_animal
type: u1
enum: animal
- id: dog_tag
type: u4
# Comparing to enum literal
if: my_animal == animal::dog
enums:
animal:
1: cat
2: dog-
重复到流结尾(
repeat: eos)seq: - id: numbers type: u4 repeat: eos
-
重复指定次数(
repeat: expr)seq: - id: numbers type: u4 repeat: expr repeat-expr: 12
-
重复,直到指定条件成立(
repeat: until)seq: - id: numbers type: s4 repeat: until repeat-until: _ == -1
特殊变量
_代表最近读取的元素。
seq:
- id: rec_type
type: u1
- id: len
type: u4
- id: body
size: len
type:
switch-on: rec_type
cases:
1: rec_type_1
2: rec_type_2
_: rec_type_unknown_可以作为缺省,匹配其他值
meta:
id: big_file
endian: le
instances:
some_integer:
pos: 0x400000
type: u4
a_string:
pos: 0x500fff
type: str
size: 0x11
encoding: ASCIIseq:
- id: file_name
type: str
size: 8 + 3
encoding: ASCII
- id: file_offset
type: u4
- id: file_size
type: u4
instances:
body:
pos: file_offset
size: file_sizeseq:
- id: length_in_feet
type: f8
instances:
length_in_m:
value: length_in_feet * 0.3048seq:
- id: short_pairs
type: kv_pair(3)
repeat: expr
repeat-expr: 0x100
- id: long_pairs
type: kv_pair(8)
repeat: expr
repeat-expr: 0x100
types:
kv_pair:
params:
- id: len_key
type: u2
seq:
- id: key
size: len_key
type: str
- id: value
type: strz- 以
-开头的属性会被忽略,因此可以用作注释。
Python的库。
https://github.com/construct/construct
Python的库。
Hachoir is a Python library to view and edit a binary stream field by field. In other words, Hachoir allows you to "browse" any binary stream just like you browse directories and files.