Skip to content

Instantly share code, notes, and snippets.

@bluerise
Created July 19, 2017 20:30
Show Gist options
  • Save bluerise/e47c7124d17ff6edd09177804b1cabc5 to your computer and use it in GitHub Desktop.
Save bluerise/e47c7124d17ff6edd09177804b1cabc5 to your computer and use it in GitHub Desktop.
Swizzle extraction toolkit
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
int check(uint8_t *);
void print(uint8_t *);
int
main(int argv, char **argc)
{
int fd = open("bios.rom", O_RDONLY);
if (fd < 0)
return 1;
struct stat st;
if (fstat(fd, &st) < 0)
return 2;
uint8_t *buf = malloc(st.st_size);
if (buf == NULL)
return 3;
if (read(fd, buf, st.st_size) != st.st_size)
return 4;
for (int off = 0; off < st.st_size - 32; off++)
if (check(buf+off)) {
printf("match at offset 0x%x:\n", off);
print(buf+off);
}
return 0;
}
int
check(uint8_t *buf)
{
uint32_t bits = 0;
for (int i = 0; i < 32; i++)
bits |= (1ULL << buf[i]);
if (bits == 0xffffffff)
return 1;
return 0;
}
void
print(uint8_t *buf)
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 8; j++)
printf("0x%02x, ", buf[i*8+j]);
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment