定长整型与端序

定长整型与端序

计算机存储的一些小常识


定长整型

使用IDA分析pseudocode时,在变量名前往往会有cast,表示将这个变量视作这个类型

常见的定长整型有:
BYTE WORD DWORD QWORD

与标准C类型有如下对应关系
定长整型

2个16进制数=8个2进制数=1字节

DWORD example: 0x12345678(32bit四字节)

QWORD example: 0x0001000200030004(64bit八字节)


地址

存放数据的地方,指针变量存放的就是地址

32位操作系统 地址位宽=32bit=4byte
64位操作系统 地址位宽=64bit=8byte

在计算机中,地址是由小变大,从低到高


端序

端序(Endian),计算机中字节的存储方式,又称字节序
分为小端序(Little endian),大端序(Big endian)

在计算机至少x86-64架构是本地存储为小端序,即低地址(小端)的字节在前(左),高地址的字节在后(右)
由于整型数字是高位在左,低位在右,所以将整型从地址上提取出来时,会自动转为大端序*(实际上与端序无关,但形式是一致的)*

e.g.
连续地址的四个byte: 01 02 03 04
DWORD: 0x04030201

大端序是网络传输与特定CPU架构使用的,RE一般不会碰到

小技巧:IDA在.data段,可以按U直接展开成按地址小端序,再按ctrl+z撤回


互转脚本

将十六进制定长整型转为字符串输出

1
2
uint32_t example = 0x44434241;
cout.write(reinterpret_cast<char *>(&example), sizeof(example));

将hex_string(兼容spaced情况)转为字符串输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
using namespace std;

vector<unsigned char> hexToBytes(const string& hex) {
vector<unsigned char> bytes;
string clean;
for (char c : hex)
if (!isspace(static_cast<unsigned char>(c))) clean += c;

for (size_t i = 0; i + 1 < clean.size(); i += 2)
bytes.push_back(static_cast<unsigned char>(
stoul(clean.substr(i, 2), nullptr, 16)
));
return bytes;
}

string hex_str="";

int main() {
auto bytes = hexToBytes(hex_str);
string result(bytes.begin(), bytes.end());
cout << result;
return 0;
}