Haber, no sabía como poner esto
hexactamente, así que pense en un nombre:
Hexadecimal Dumper And To C Convertor
Creo que no hace falta explicar mucho, es un dumpeador hexadecimal para convertir cualquier archivo a variable del tipo char en C. En otras palabras, convierte:
#include <stdio.h>
double factorial(short int numero) {
int pred = 0;
double tmp = numero;
for (pred = --numero; pred > 1; --pred)
tmp *= pred;
return tmp;
}
int main(void) {
double factor87 = factorial(87);
printf("Factorial de 87: %.20lg", factor87);
getchar();
return 0;
}
En esto:
char hexdump2c[] = {
0x23, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x65, 0x20, 0x3c, 0x73, 0x74, 0x64, 0x69, 0x6f, 0x2e, 0x68, 0x3e,
0x0d, 0x0a, 0x0d, 0x0a, 0x64, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x20, 0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x69,
0x61, 0x6c, 0x28, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x20, 0x69, 0x6e, 0x74, 0x20, 0x6e, 0x75, 0x6d, 0x65, 0x72,
0x6f, 0x29, 0x20, 0x7b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x69, 0x6e, 0x74, 0x20, 0x70, 0x72, 0x65, 0x64,
0x20, 0x3d, 0x20, 0x30, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x64, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x20,
0x74, 0x6d, 0x70, 0x20, 0x3d, 0x20, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x6f, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20,
0x20, 0x66, 0x6f, 0x72, 0x20, 0x28, 0x70, 0x72, 0x65, 0x64, 0x20, 0x3d, 0x20, 0x2d, 0x2d, 0x6e, 0x75, 0x6d,
0x65, 0x72, 0x6f, 0x3b, 0x20, 0x70, 0x72, 0x65, 0x64, 0x20, 0x3e, 0x20, 0x31, 0x3b, 0x20, 0x2d, 0x2d, 0x70,
0x72, 0x65, 0x64, 0x29, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x74, 0x6d, 0x70, 0x20,
0x2a, 0x3d, 0x20, 0x70, 0x72, 0x65, 0x64, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75,
0x72, 0x6e, 0x20, 0x74, 0x6d, 0x70, 0x3b, 0x0d, 0x0a, 0x7d, 0x0d, 0x0a, 0x0d, 0x0a, 0x69, 0x6e, 0x74, 0x20,
0x6d, 0x61, 0x69, 0x6e, 0x28, 0x76, 0x6f, 0x69, 0x64, 0x29, 0x20, 0x7b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20,
0x64, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x20, 0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x38, 0x37, 0x20, 0x3d, 0x20,
0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x69, 0x61, 0x6c, 0x28, 0x38, 0x37, 0x29, 0x3b, 0x0d, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x28, 0x22, 0x46, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x69, 0x61,
0x6c, 0x20, 0x64, 0x65, 0x20, 0x38, 0x37, 0x3a, 0x20, 0x25, 0x2e, 0x32, 0x30, 0x6c, 0x67, 0x22, 0x2c, 0x20,
0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x38, 0x37, 0x29, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x67, 0x65,
0x74, 0x63, 0x68, 0x61, 0x72, 0x28, 0x29, 0x3b, 0x20, 0x20, 0x20, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72,
0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x30, 0x3b, 0x0d, 0x0a, 0x7d, 0x0d, 0x0a};
El código fuente del programa es:
/* Simple dumpeador de archivos a hexadecimal y guardar en C
* http://mikygonzalez.16mb.com
*/
#include <stdio.h>
#define TAM_HEXCODE 18
/*! Función principal hexdump */
void hexdump(FILE *archivo_fd) {
unsigned short int bucle_i;
char caracter_actual = '\0';
fseek(archivo_fd, 0, SEEK_SET);
while(!feof(archivo_fd)) {
for(bucle_i = 0; bucle_i < TAM_HEXCODE; bucle_i++) {
caracter_actual = fgetc(archivo_fd);
if(caracter_actual == EOF)
return (void) 0;
printf("0x%.2x, ", caracter_actual);
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
FILE *archivo_fd;
if(argc < 2)
return 1;
if(!(archivo_fd = fopen(argv[1], "rb")))
return 1;
printf("char hexdump2c[] = {\n");
hexdump(archivo_fd);
printf("\b\b};\n");
return 0;
}
También pueden encontrarse este y otros programas en: http://pastes.mikygonzalez.16mb.com/?ad2cea935c65385a#c0bI9LQ9BD6f8snTLpklw/LgzXRnOEs6lJHJWqbbb9c= (http://pastes.mikygonzalez.16mb.com/?ad2cea935c65385a#c0bI9LQ9BD6f8snTLpklw/LgzXRnOEs6lJHJWqbbb9c=) o en mi pagina personal (en la firma).
Saludos
Hace un dump hexadecimal de un archivo? No es gran cosa, si me da tiempo despues subo uno que te dumpee la sección .text de un ejecutable ELF, si no mañana ;)
EDIT: Aqui esta, lo he hecho para 32 bits, para hacerlo portable a 64 solo hay que cambiar las estructuras del header elf.h por sus equivalentes de 64 bits.
#include <elf.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
void hexaDump(const void *ptr, size_t mem_sz)
{
uint8_t *u8_ptr = ptr;
size_t i=0;
for(; i<mem_sz-1; i++)
{
printf("%x, ", u8_ptr[i]);
}
printf("%x", u8_ptr[++i]);
}
int main(int argc, char **argv)
{
Elf32_Ehdr elf_hdr;
Elf32_Shdr *elf_shdr;
Elf32_Shdr shStrTab;
char *shStrTabData, *sectionData;
size_t i;
if(argc!=3)
{
printf("Usage: %s <file> <section>\n", argv[0]);
return 0;
}
FILE *fileToDump = fopen(argv[1], "r");
if(!fileToDump)
{
printf("Could not open the file, please check the path\n");
return 1;
}
fread(&elf_hdr, sizeof(Elf32_Ehdr), 1, fileToDump);
fseek(fileToDump, elf_hdr.e_shoff + elf_hdr.e_shentsize*elf_hdr.e_shstrndx, SEEK_SET);
fread(&shStrTab, sizeof(Elf32_Shdr), 1, fileToDump);
fseek(fileToDump, shStrTab.sh_offset, SEEK_SET);
shStrTabData = malloc(shStrTab.sh_size);
fread(shStrTabData, 1, shStrTab.sh_size, fileToDump);
elf_shdr = malloc(elf_hdr.e_shnum*sizeof(Elf32_Shdr));
fseek(fileToDump, elf_hdr.e_shoff, SEEK_SET);
fread(elf_shdr, sizeof(Elf32_Shdr), elf_hdr.e_shnum, fileToDump);
_Bool foundFlag=false;
for(i=0; i<elf_hdr.e_shnum; i++)
{
if(!strcmp(shStrTabData+elf_shdr[i].sh_name, argv[2]))
{
foundFlag = true;
break;
}
}
if(!foundFlag)
{
printf("Could not find the requested section\n");
return 2;
}
sectionData = malloc(elf_shdr[i].sh_size);
fseek(fileToDump, elf_shdr[i].sh_offset, SEEK_SET);
fread(sectionData, 1, elf_shdr[i].sh_size, fileToDump);
printf("char hexdump[] = { ");
hexaDump(sectionData, elf_shdr[i].sh_size);
printf(" }\n\n");
free(shStrTabData);
free(elf_shdr);
free(sectionData);
fclose(fileToDump);
return 0;
}
0xDani: Gran aporte, yo tan sólo quería mostrar el cometido de una manera sencilla, fácil y eficaz; dije que gran aporte?, si no lo dije, gran aporte. xD
@Miky Gonzalez, es que sacar un dump de un archivo para ponerlo en una variable de C me sugirio que quizas podias estar practicando shellcoding, por eso decidi ponerte ese code, si no lo estas haciendo pues como aporte y ya esta :P.