or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

data-access.mddebug.mdhashing.mdimport-export.mdindex.mdmemory.mdordinal-lookups.mdpacker-detection.mdpe-parsing.mdresources.mdsections.md

sections.mddocs/

0

# Section Operations

1

2

Methods for working with PE sections, including accessing section data and metadata. Sections contain the actual code, data, and resources of the PE file, organized into logical segments with specific characteristics and permissions.

3

4

## Capabilities

5

6

### Section Access

7

8

Locate and access sections by address or offset.

9

10

```python { .api }

11

def get_section_by_rva(self, rva):

12

"""

13

Get section containing the specified RVA.

14

15

Args:

16

rva (int): Relative virtual address

17

18

Returns:

19

SectionStructure: Section containing the RVA, or None if not found

20

"""

21

22

def get_section_by_offset(self, offset):

23

"""

24

Get section containing the specified file offset.

25

26

Args:

27

offset (int): File offset

28

29

Returns:

30

SectionStructure: Section containing the offset, or None if not found

31

"""

32

```

33

34

### Section Data Management

35

36

Apply modifications made to section data back to the PE structure.

37

38

```python { .api }

39

def merge_modified_section_data(self):

40

"""

41

Update PE internal data with modified section data.

42

43

This method applies any changes made to section data back to the

44

main PE data structure, ensuring consistency between section objects

45

and the underlying file data. Must be called after modifying section

46

data through section.set_data() or direct manipulation.

47

"""

48

```

49

50

### SectionStructure Methods

51

52

Individual section objects provide detailed access to section data and properties.

53

54

```python { .api }

55

class SectionStructure:

56

def get_data(self, start=None, length=None, ignore_padding=False):

57

"""

58

Get section data.

59

60

Args:

61

start (int, optional): Start offset within section

62

length (int, optional): Number of bytes to read

63

ignore_padding (bool): Whether to ignore alignment padding

64

65

Returns:

66

bytes: Section data

67

"""

68

69

def get_entropy(self):

70

"""

71

Calculate Shannon entropy of section data.

72

73

Returns:

74

float: Entropy value (0.0 to 8.0), higher values indicate more randomness

75

"""

76

77

def get_hash_md5(self):

78

"""

79

Calculate MD5 hash of section data.

80

81

Returns:

82

str: MD5 hash as hexadecimal string

83

"""

84

85

def get_hash_sha1(self):

86

"""

87

Calculate SHA-1 hash of section data.

88

89

Returns:

90

str: SHA-1 hash as hexadecimal string

91

"""

92

93

def get_hash_sha256(self):

94

"""

95

Calculate SHA-256 hash of section data.

96

97

Returns:

98

str: SHA-256 hash as hexadecimal string

99

"""

100

101

def get_hash_sha512(self):

102

"""

103

Calculate SHA-512 hash of section data.

104

105

Returns:

106

str: SHA-512 hash as hexadecimal string

107

"""

108

109

def contains_rva(self, rva):

110

"""

111

Check if section contains the specified RVA.

112

113

Args:

114

rva (int): Relative virtual address

115

116

Returns:

117

bool: True if RVA is within section bounds

118

"""

119

120

def contains_offset(self, offset):

121

"""

122

Check if section contains the specified file offset.

123

124

Args:

125

offset (int): File offset

126

127

Returns:

128

bool: True if offset is within section bounds

129

"""

130

```

131

132

### Section Parsing

133

134

Parse section headers from the PE file.

135

136

```python { .api }

137

def parse_sections(self, offset):

138

"""

139

Parse section headers starting at specified offset.

140

141

Args:

142

offset (int): File offset where section headers begin

143

144

Populates:

145

self.sections: List of SectionStructure objects

146

"""

147

```

148

149

## Section Structure Attributes

150

151

Each section in the `pe.sections` list is a `SectionStructure` object with the following key attributes:

152

153

```python { .api }

154

class SectionStructure:

155

"""Represents a PE file section."""

156

Name: bytes # Section name (8 bytes, null-padded)

157

VirtualSize: int # Size of section when loaded in memory

158

VirtualAddress: int # RVA where section is loaded

159

SizeOfRawData: int # Size of section in file

160

PointerToRawData: int # File offset to section data

161

PointerToRelocations: int # File offset to relocations

162

PointerToLinenumbers: int # File offset to line numbers

163

NumberOfRelocations: int # Number of relocations

164

NumberOfLinenumbers: int # Number of line number entries

165

Characteristics: int # Section characteristics/flags

166

167

def get_data(self, start=None, length=None):

168

"""Get section data."""

169

170

def set_data(self, data):

171

"""Set section data."""

172

173

def get_entropy(self):

174

"""Calculate section entropy."""

175

176

def get_hash_sha1(self):

177

"""Get SHA1 hash of section data."""

178

179

def get_hash_sha256(self):

180

"""Get SHA256 hash of section data."""

181

182

def get_hash_md5(self):

183

"""Get MD5 hash of section data."""

184

```

185

186

## Usage Examples

187

188

### Basic Section Analysis

189

190

```python

191

import pefile

192

193

with pefile.PE('executable.exe') as pe:

194

print("Section Analysis:")

195

print("-" * 60)

196

print(f"{'Name':<10} {'VirtAddr':<10} {'VirtSize':<10} {'RawSize':<10} {'Entropy':<8}")

197

print("-" * 60)

198

199

for section in pe.sections:

200

name = section.Name.decode('utf-8').strip('\x00')

201

virt_addr = f"0x{section.VirtualAddress:08x}"

202

virt_size = f"0x{section.VirtualSize:08x}"

203

raw_size = f"0x{section.SizeOfRawData:08x}"

204

entropy = f"{section.get_entropy():.2f}"

205

206

print(f"{name:<10} {virt_addr:<10} {virt_size:<10} {raw_size:<10} {entropy:<8}")

207

```

208

209

### Section Characteristics Analysis

210

211

```python

212

import pefile

213

214

with pefile.PE('executable.exe') as pe:

215

print("Section Characteristics:")

216

print("-" * 40)

217

218

for section in pe.sections:

219

name = section.Name.decode('utf-8').strip('\x00')

220

characteristics = section.Characteristics

221

222

print(f"\nSection: {name}")

223

print(f"Characteristics: 0x{characteristics:08x}")

224

225

# Decode characteristics flags

226

flags = []

227

if characteristics & 0x00000020: # IMAGE_SCN_CNT_CODE

228

flags.append("CODE")

229

if characteristics & 0x00000040: # IMAGE_SCN_CNT_INITIALIZED_DATA

230

flags.append("INITIALIZED_DATA")

231

if characteristics & 0x00000080: # IMAGE_SCN_CNT_UNINITIALIZED_DATA

232

flags.append("UNINITIALIZED_DATA")

233

if characteristics & 0x20000000: # IMAGE_SCN_MEM_EXECUTE

234

flags.append("EXECUTABLE")

235

if characteristics & 0x40000000: # IMAGE_SCN_MEM_READ

236

flags.append("READABLE")

237

if characteristics & 0x80000000: # IMAGE_SCN_MEM_WRITE

238

flags.append("WRITABLE")

239

240

if flags:

241

print(f"Flags: {', '.join(flags)}")

242

```

243

244

### Section Data Extraction

245

246

```python

247

import pefile

248

import os

249

250

with pefile.PE('executable.exe') as pe:

251

# Create directory for section dumps

252

output_dir = 'section_dumps'

253

os.makedirs(output_dir, exist_ok=True)

254

255

for i, section in enumerate(pe.sections):

256

name = section.Name.decode('utf-8').strip('\x00')

257

258

# Get section data

259

section_data = section.get_data()

260

261

# Save section to file

262

filename = f"{output_dir}/{i:02d}_{name}.bin"

263

with open(filename, 'wb') as f:

264

f.write(section_data)

265

266

print(f"Extracted {name}: {len(section_data)} bytes -> {filename}")

267

268

# Calculate hashes

269

sha256 = section.get_hash_sha256()

270

md5 = section.get_hash_md5()

271

entropy = section.get_entropy()

272

273

print(f" SHA256: {sha256}")

274

print(f" MD5: {md5}")

275

print(f" Entropy: {entropy:.3f}")

276

```

277

278

### Section Modification

279

280

```python

281

import pefile

282

283

# Load PE file

284

pe = pefile.PE('executable.exe')

285

286

# Find text section

287

text_section = None

288

for section in pe.sections:

289

name = section.Name.decode('utf-8').strip('\x00')

290

if name == '.text':

291

text_section = section

292

break

293

294

if text_section:

295

print(f"Found .text section at RVA 0x{text_section.VirtualAddress:08x}")

296

297

# Get current section data

298

original_data = text_section.get_data()

299

print(f"Original size: {len(original_data)} bytes")

300

301

# Modify section data (example: NOP out first few bytes)

302

modified_data = bytearray(original_data)

303

modified_data[0:10] = b'\x90' * 10 # NOP instructions

304

305

# Set modified data back to section

306

text_section.set_data(bytes(modified_data))

307

308

# Apply changes to PE structure

309

pe.merge_modified_section_data()

310

311

# Write modified PE

312

pe.write('modified_executable.exe')

313

print("Modified executable saved")

314

315

pe.close()

316

```

317

318

### Finding Sections by Address

319

320

```python

321

import pefile

322

323

with pefile.PE('executable.exe') as pe:

324

# Find section containing entry point

325

entry_point = pe.OPTIONAL_HEADER.AddressOfEntryPoint

326

entry_section = pe.get_section_by_rva(entry_point)

327

328

if entry_section:

329

name = entry_section.Name.decode('utf-8').strip('\x00')

330

print(f"Entry point 0x{entry_point:08x} is in section: {name}")

331

332

# Calculate offset within section

333

section_offset = entry_point - entry_section.VirtualAddress

334

print(f"Offset within section: 0x{section_offset:08x}")

335

336

# Find section containing specific file offset

337

file_offset = 0x1000

338

offset_section = pe.get_section_by_offset(file_offset)

339

340

if offset_section:

341

name = offset_section.Name.decode('utf-8').strip('\x00')

342

print(f"File offset 0x{file_offset:08x} is in section: {name}")

343

```

344

345

### Section Entropy Analysis

346

347

```python

348

import pefile

349

350

def analyze_entropy(pe):

351

"""Analyze section entropy to detect packing."""

352

print("Entropy Analysis:")

353

print("-" * 30)

354

355

high_entropy_sections = []

356

357

for section in pe.sections:

358

name = section.Name.decode('utf-8').strip('\x00')

359

entropy = section.get_entropy()

360

361

print(f"{name:<10}: {entropy:.3f}")

362

363

# Flag high entropy sections (possibly packed/encrypted)

364

if entropy > 7.0: # High entropy threshold

365

high_entropy_sections.append((name, entropy))

366

367

if high_entropy_sections:

368

print("\nHigh entropy sections (possibly packed):")

369

for name, entropy in high_entropy_sections:

370

print(f" {name}: {entropy:.3f}")

371

372

return high_entropy_sections

373

374

# Usage

375

with pefile.PE('executable.exe') as pe:

376

high_entropy = analyze_entropy(pe)

377

```

378

379

### Section Alignment Utilities

380

381

```python

382

import pefile

383

384

with pefile.PE('executable.exe') as pe:

385

file_alignment = pe.OPTIONAL_HEADER.FileAlignment

386

section_alignment = pe.OPTIONAL_HEADER.SectionAlignment

387

388

print(f"File Alignment: 0x{file_alignment:x}")

389

print(f"Section Alignment: 0x{section_alignment:x}")

390

print()

391

392

for section in pe.sections:

393

name = section.Name.decode('utf-8').strip('\x00')

394

395

# Check alignment

396

raw_addr_aligned = (section.PointerToRawData % file_alignment) == 0

397

virt_addr_aligned = (section.VirtualAddress % section_alignment) == 0

398

399

print(f"Section {name}:")

400

print(f" Raw Address: 0x{section.PointerToRawData:08x} (aligned: {raw_addr_aligned})")

401

print(f" Virtual Address: 0x{section.VirtualAddress:08x} (aligned: {virt_addr_aligned})")

402

```