cybersecurity · intermediate · ~15 min · safe pentest lab

Is the ELF 64-bit?

Read the ELF class field.

Challenge

Read the ELF class field to learn the word size before parsing the rest of the header.

Task

Implement int elf_is_64bit(const unsigned char *b) that returns 1 if the ELF is 64-bit, else 0.

Input

  • b: an ELF header buffer the grader passes; the EI_CLASS byte at offset 4 is 1 for 32-bit, 2 for 64-bit.

Output

Returns int: 1 if b[4] == 2, else 0.

Example

{0x7f,'E','L','F',2}   ->   1   (64-bit)
{0x7f,'E','L','F',1}   ->   0   (32-bit)

Edge cases

  • Any EI_CLASS value other than 2 returns 0.

Input format

An ELF header buffer b with at least 5 readable bytes.

Output format

An int: 1 if the EI_CLASS byte b[4] is 2 (64-bit), else 0.

Constraints

EI_CLASS is at offset 4; 2 means 64-bit.

Starter code

int elf_is_64bit(const unsigned char *b) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.