1、简介
010editor 是一款十六进制编辑器,和 winhex 相比支持更灵活的脚本语法,可以对文件、内存、磁盘进行操作,是二进制分析中十分强力的工具,能够解析多种文件格式并以友好的界面呈现。其强大的内部引擎使得任何人都可以定制所需的解析脚本或解析模板。
2、语法
010Editor内置脚本语言语法类似于c++,这种结构体不同之处在于:
- 访问变量时,从文件读取并显示,赋值变量时,写入文件:
struct FILE{
DAILY_RECORD_V3_HEADER header;
struct Data {
while(!FEof()) {
DAILY_RECORD_V3 record;
}
}data;
}file;
- 可以使用控制语句如:
if for while
控制语句实例:
int i;
for( i = 0; i < file.header.numRecords; i++ )
file.record[i].salary *= 2.0;
2.1 表达式
- 支持标准c的操作符:+ - * / ~ ^ & | % ++ – ?: << >> ()
- 支持的比较操作符:< ? <= >= == != !
- 支持的赋值操作符:= += -= *= /= &= ^= %= |= <<= >>=
- 布尔运算符:&& || !
- 常数
10进制 4,5,6
16进制 0xff,25h,0EFh
8进制 013
2进制 0b011
u后缀表示unsigned L后缀表示8字节int64值
指数 1e10
浮点数 2.0f, 2.0
2.2 变量
变量定义:
int x;
float a = 3.5f;
unsigned int myVar1, myVar2;
常量:
const int TAG_EOF = 0x3545;
内建常量:(默认的关键字)
true false TRUE FALSE M_PI PI
2.3 数据类型
name | 可用的定义格式 |
---|---|
8字节 | char, byte ,CHAR, BYTE, uchar, ubyte, UCHAR, UBYTE |
16字节 | short, int16, SHORT, INT16, ushort, uint16, USHORT, UINT16 ,WORD |
32字节 | int, int32, long, INT, INT32, LONG, uint, uint32, ulong, UINT, UINT32, ULONG, DWORD |
64字节 | int64, quad, QUAD, INT64, __int64, uint64, uquad, UQUAD, UINT64, __uint64 ,QWORD |
浮点 | float, FLOAT, double, DOUBLE, hfloat, HFLOAT |
其他 | DOSDATE, DOSTIME ,FILETIME, OLETIME, time_t |
typedef
typedef unsigned int myInt;
typedef char myString[15];
myString s = "Test";
enum
enum MYENUM {
COMP_1, COMP_2 = 5, COMP_3 } var1;
enum <ushort> MYENUM {
COMP_1, COMP_2 = 5, COMP_3 } var1;
数组和字符串
int myArray[15];
int myArray[ FileSize() - myInt * 0x10 + (17 << 5) ];//大小可以是变量
char str[15] = "First";
string s = "Second";
string r1 = str + s;
string r2 = str;
r2 += s;
return (r1 == r2);
可以通过WStringToString StringToWString转换;
2.4 控制语句
if
if( x < 5 )
x = 0;
or
if( y > x )
max = y;
else
{
max = x;
y = 0;
}
for
for( i = 0, x = 0; i < 15; i++ )
{
x += i;
}
while
switch( <variable> )
{
case <expression>: <statement>; [break;]
.
.
.
default : <statement>;
}
switch( value )
{
case 2 : result = 1; break;
case 4 : result = 2; break;
case 8 : result = 3; break;
case 16 : result = 4; break;
default : result = -1;
}
2.5 函数
string str = "Apple";
return Strlen( str );
Printf( "string='%s' length='%d'\n", str, Strlen( str ) );
void OutputInt( int d )
{
Printf( "%d\n", d );
}
OutputInt( 5 );
char[] GetExtension( char filename[], int &extLength )
{
int pos = Strchr( filename, '.' );
if( pos == -1 )
{
extLength = 0;
return "";
}
else
{
extLength = Strlen( filename ) - pos - 1;
return SubStr( filename, pos + 1 );
}
}
参数可以通过值或引用传递,010editor脚本不支持指针,但是可以用[]表示数组。程序中不需要main函数,代码从第一行开始执行。
2.6 关键字
sizeof
startof 用于计算变量起始地址
SetCursorPos( startof( lines[0] ) );
exists 检查某变量是否声明
int i;
string s;
while( exists( file[i] ) )
{
s = file[i].frFileName;
Printf( "%s\n", s );
i++;
}
function_exists 检查函数是否定义
if( function_exists(CopyStringToClipboard) )
{
...
}
this 引用当前结构体
void PrintHeader( struct HEADER &h )
{
Printf( "ID1 = %d\n", h.ID1 );
Printf( "ID2 = %d\n", h.ID2 );
}
struct HEADER
{
int ID1;
int ID2;
PrintHeader( this );
} h1;
parentof 访问包含变量的结构和union
void PrintHeader( struct HEADER &h )
{
Printf( "ID1 = %d\n", h.ID1 );
Printf( "ID2 = %d\n", h.ID2 );
}
struct HEADER
{
int ID1;
int ID2;
struct SUBITEM
{
int data1;
int data2;
PrintHeader( parentof(this) );
} item1;
PrintHeader( parentof(item1) );
} h1;
预处理
define
#define PI 3.14159265
#define CHECK_VALUE if( value > 5) {
\
Printf( "Invalid value %d\n", value ); \
Exit(-1); }
#define FILE_ICON 12
#define FOLDER_ICON (FILE_ICON+100)
内建常量
_010EDITOR 010editor运行后定义
_010_WIN 运行在windows上定义
_010_MAC
_010_LINUX
_010_64BIT
条件编译
#ifdef | #ifndef <constant_name>
(...)
[ #else ]
(...)
#endif
#ifndef CONSTANTS_H
#define CONSTANTS_H
警告和错误
#ifdef NUMBITS
value = value + NUMBITS;
#else
#warning "NUMBITS not defined!"
#endif
#ifndef CURRENT_OS
#error "CURRENT_OS must be defined. Compilation stopped."
#endif
include
#include "Vector.bt"
脚本限制
禁止使用指针,可以使用引用传参数
禁止使用#if预处理
禁止使用多维数组
禁止使用goto
模板基础
声明模板变量
特殊属性
< format=hex|decimal|octal|binary,
fgcolor=<color>,
bgcolor=<color>,
comment="<string>"|<function_name>,
name="<string>"|<function_name>,
open=true|false|suppress,
hidden=true|false,
read=<function_name>,
write=<function_name>
size=<number>|<function_name> >
format设置
int crc <format=hex>;
int flags <format=binary>;
color设置
int id <fgcolor=cBlack, bgcolor=0x0000FF>;
SetForeColor( cRed );
int first; // will be colored red
int second; // will be colored red
SetForeColor( cNone );
int third; // will not be colored
大小头端设置
注释设置
int machineStatus <comment="This should be greater than 15.">;
int machineStatus <comment=MachineStatusComment>;
string MachineStatusComment( int status )
{
if( status <= 15 )
return "*** Invalid machine status";
else
return "Valid machine status";
}
显示名设置
byte _si8 <name="Signed Byte">;
顺序:在声明模板变量后,当前文件指针后移,通过FTell获取当前位置,通过FSeek FSkip移动指针 通过ReadByte ReadShort ReadInt任意读取而不移动指针
局部变量
local int i, total = 0;
int recordCounts[5];
for( i = 0; i < 5; i++ )
total += recordCounts[i];
double records[ total ];
打开状态设置
<open=true/false>展开/收敛节点
字符串
char str[];
string str;
wchar_t str[];
wstring str;
隐藏设置
<hidden=true/false>
结构体联合体
struct myStruct {
int a;
int b;
int c;
};
struct myStruct {
int a;
int b;
int c;
} s1, s2;
struct myIfStruct {
int a;
if( a > 5 )
int b;
else
int c;
} s;
struct {
int width;
struct COLOR {
uchar r, g, b;
} colors[width];
} line1;
typedef struct {
ushort id;
int size;