Skip to content

Instantly share code, notes, and snippets.

@milankragujevic
Created January 7, 2026 11:12
Show Gist options
  • Select an option

  • Save milankragujevic/acdc9de1ab4506cd44746df8d5839e2e to your computer and use it in GitHub Desktop.

Select an option

Save milankragujevic/acdc9de1ab4506cd44746df8d5839e2e to your computer and use it in GitHub Desktop.
Cudy router WR3000 /dev/mtd3 (bdinfo) decoder written in C for getting SN, MAC and WiFi PIN in OpenWrt or elsewhere (must be run on aarch64 system!)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <openssl/des.h>
const unsigned char KEY[] = {0x97, 0x5e, 0x07, 0x19, 0xc4, 0xd5, 0x92, 0x79};
#define SKIP_BYTES 4
#define READ_SIZE 56700
char *trim(char *str) {
char *end;
while(isspace((unsigned char)*str)) str++;
if(*str == 0) return str;
end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) end--;
*(end+1) = 0;
return str;
}
int main(int argc, char *argv[]) {
const char *mtd_dev = "/dev/mtd3";
if (argc > 1) {
mtd_dev = argv[1];
}
FILE *f = fopen(mtd_dev, "rb");
if (!f) {
perror("Greska pri otvaranju uredjaja");
return 1;
}
unsigned char *buffer = calloc(1, READ_SIZE + 1);
if (!buffer) {
printf("Greska: Nema memorije\n");
fclose(f);
return 1;
}
fseek(f, SKIP_BYTES, SEEK_SET);
size_t read_bytes = fread(buffer, 1, READ_SIZE, f);
fclose(f);
DES_cblock key_block;
DES_key_schedule schedule;
memcpy(key_block, KEY, 8);
DES_set_key_unchecked(&key_block, &schedule);
DES_cblock iv = {0, 0, 0, 0, 0, 0, 0, 0};
DES_ncbc_encrypt(buffer, buffer, READ_SIZE, &schedule, &iv, DES_DECRYPT);
char *ptr = (char *)buffer;
char *line_start = ptr;
for (size_t i = 0; i < read_bytes; i++) {
if (ptr[i] == '\n' || ptr[i] == '\r' || ptr[i] == '\0') {
ptr[i] = '\0';
if (strlen(line_start) > 0) {
char *delimiter = strchr(line_start, '=');
if (delimiter) {
*delimiter = '\0';
char *key = trim(line_start);
char *val = trim(delimiter + 1);
if (strcmp(key, "pin") == 0) {
printf("PIN: %s\n", val);
} else if (strcmp(key, "sn") == 0) {
printf("SN: %s\n", val);
} else if (strcmp(key, "mac") == 0) {
printf("MAC: %s\n", val);
}
}
}
line_start = ptr + i + 1;
}
}
free(buffer);
return 0;
}
# sudo apt install build-essential libssl-dev
# must be run on aarch64 system!
gcc -static -o bddecoder bddecoder.c -lcrypto -Wno-deprecated-declarations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment