Binalyzer: Phase 3이 이제 완료되었습니다!
Source: Dev.to
개요
ELF와 PE 간의 섹션 분석을 비교하고, 포맷 차이점 및 이를 어떻게 처리했는지 설명합니다.
드디어 Binalyzer의 Phase 3가 완료되었습니다! 이제 PE와 ELF 파일 모두에 대해 섹션을 나열합니다. 아래는 수행된 작업의 주요 요점입니다.
PE 섹션 파싱
ELF와 PE는 근본적으로 다른 포맷이므로, 섹션 필드를 읽고 파싱하는 방법도 각각 다릅니다.
- PE 문서 – 섹션 레이아웃을 이해하기 위해 공식 PE 사양을 참고했습니다.
- 데이터 읽기 –
read()와struct.unpack()을 사용해 바이너리 스트림에서 필드를 추출했습니다. - 필드 커버리지 – 각 섹션의 모든 필드를 읽도록 보장했습니다; 누락된 필드는 데이터 정렬 오류를 일으킬 수 있습니다.
섹션 이름 읽기
섹션 이름은 널 바이트(\x00)로 패딩됩니다. 패딩은 Python의 replace() 메서드로 제거했습니다:
clean_name = raw_name.replace(b'\x00', b'')
플래그 처리
각 플래그 값은 16진수 숫자로 저장되며, 일부 값은 여러 플래그의 조합입니다. 솔루션은 비트 연산 AND(&)를 사용해 개별 플래그를 검사합니다:
if characteristics & flag == flag:
flags_dict[flag] = description
이 코드는 키가 플래그이고 값이 해당 설명인 딕셔너리를 채웁니다.
샘플 출력
File path: /mnt/c/Windows/system32/cmd.exe
Filetype: PE
Magic number 0x20b
PE Header :
COFF Offset : 248
Signature : b'PE\x00\x00'
File Header :
Machine : x64
NumberOfSections : 8
TimeDateStamp : 2091-09-06 23:01:06+00:00
PointerToSymbolTable : 0
NumberOfSymbols : 0
SizeOfOptionalHeader (bytes) : 240
Characteristics :
0x2 : Executable file
0x20 : Can handle >2GB addresses
Optional Header :
Standard Fields :
Magic : PE32+
MajorLinkerVersion : 14
MinorLinkerVersion : 38
SizeOfCode : 233472
SizeOfInitializedData : 217088
SizeOfUnitizializedData : 0
AddressOfEntryPoint : 162592
BaseOfCode (address) : 4096
Sections :
[0]
Name : .text
VirtualSize : 0x37db6
VirtualAddress : 0x1000
SizeOfRawData : 0x38000
PointerToRawData : 0x1000
PointerToRelocations : 0x0
PointerToLinenumbers : 0x0
NumberOfRelocations : 0x0
NumberOfLinenumbers : 0x0
Characteristics :
0x0 : Reserved for future use
0x20 : Contains executable code
0x20000000 : Can be executed as code
0x40000000 : Can be read
[1]
Name : fothk
VirtualSize : 0x1000
VirtualAddress : 0x39000
SizeOfRawData : 0x1000
PointerToRawData : 0x39000
PointerToRelocations : 0x0
PointerToLinenumbers : 0x0
NumberOfRelocations : 0x0
NumberOfLinenumbers : 0x0
Characteristics :
0x0 : Reserved for future use
0x20 : Contains executable code
0x20000000 : Can be executed as code
0x40000000 : Can be read
[2]
Name : .rdata
VirtualSize : 0x9b38
VirtualAddress : 0x3a000
SizeOfRawData : 0xa000
PointerToRawData : 0x3a000
PointerToRelocations : 0x0
PointerToLinenumbers : 0x0
NumberOfRelocations : 0x0
NumberOfLinenumbers : 0x0
Characteristics :
0x0 : Reserved for future use
0x40 : Contains initialized data
0x40000000 : Can be read
다음 단계
이 업데이트에 대한 자세한 내용은 GitHub 저장소의 릴리스를 확인하세요. Phase 4가 곧 시작됩니다. 안녕!