elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Security Series.XSS. [Cross Site Scripting]


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Codigo de un "hexdump2c"
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Codigo de un "hexdump2c"  (Leído 1,634 veces)
Miky Gonzalez

Desconectado Desconectado

Mensajes: 87

http://mikygonzalez.comule.com/blog/


Ver Perfil WWW
Codigo de un "hexdump2c"
« en: 8 Marzo 2013, 17:48 pm »

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:

Código
  1. #include <stdio.h>
  2.  
  3. double factorial(short int numero) {
  4.    int pred = 0;
  5.    double tmp = numero;
  6.    for (pred = --numero; pred > 1; --pred)
  7.        tmp *= pred;
  8.    return tmp;
  9. }
  10.  
  11. int main(void) {
  12.    double factor87 = factorial(87);
  13.    printf("Factorial de 87: %.20lg", factor87);
  14.    getchar();  
  15.    return 0;
  16. }

En esto:

Código
  1. char hexdump2c[] = {
  2. 0x23, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x65, 0x20, 0x3c, 0x73, 0x74, 0x64, 0x69, 0x6f, 0x2e, 0x68, 0x3e,
  3. 0x0d, 0x0a, 0x0d, 0x0a, 0x64, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x20, 0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x69,
  4. 0x61, 0x6c, 0x28, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x20, 0x69, 0x6e, 0x74, 0x20, 0x6e, 0x75, 0x6d, 0x65, 0x72,
  5. 0x6f, 0x29, 0x20, 0x7b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x69, 0x6e, 0x74, 0x20, 0x70, 0x72, 0x65, 0x64,
  6. 0x20, 0x3d, 0x20, 0x30, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x64, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x20,
  7. 0x74, 0x6d, 0x70, 0x20, 0x3d, 0x20, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x6f, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20,
  8. 0x20, 0x66, 0x6f, 0x72, 0x20, 0x28, 0x70, 0x72, 0x65, 0x64, 0x20, 0x3d, 0x20, 0x2d, 0x2d, 0x6e, 0x75, 0x6d,
  9. 0x65, 0x72, 0x6f, 0x3b, 0x20, 0x70, 0x72, 0x65, 0x64, 0x20, 0x3e, 0x20, 0x31, 0x3b, 0x20, 0x2d, 0x2d, 0x70,
  10. 0x72, 0x65, 0x64, 0x29, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x74, 0x6d, 0x70, 0x20,
  11. 0x2a, 0x3d, 0x20, 0x70, 0x72, 0x65, 0x64, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75,
  12. 0x72, 0x6e, 0x20, 0x74, 0x6d, 0x70, 0x3b, 0x0d, 0x0a, 0x7d, 0x0d, 0x0a, 0x0d, 0x0a, 0x69, 0x6e, 0x74, 0x20,
  13. 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x76, 0x6f, 0x69, 0x64, 0x29, 0x20, 0x7b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20,
  14. 0x64, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x20, 0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x38, 0x37, 0x20, 0x3d, 0x20,
  15. 0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x69, 0x61, 0x6c, 0x28, 0x38, 0x37, 0x29, 0x3b, 0x0d, 0x0a, 0x20, 0x20,
  16. 0x20, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x28, 0x22, 0x46, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x69, 0x61,
  17. 0x6c, 0x20, 0x64, 0x65, 0x20, 0x38, 0x37, 0x3a, 0x20, 0x25, 0x2e, 0x32, 0x30, 0x6c, 0x67, 0x22, 0x2c, 0x20,
  18. 0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x38, 0x37, 0x29, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x67, 0x65,
  19. 0x74, 0x63, 0x68, 0x61, 0x72, 0x28, 0x29, 0x3b, 0x20, 0x20, 0x20, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72,
  20. 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x30, 0x3b, 0x0d, 0x0a, 0x7d, 0x0d, 0x0a};

El código fuente del programa es:
Código
  1. /* Simple dumpeador de archivos a hexadecimal y guardar en C
  2.  * http://mikygonzalez.16mb.com
  3. */
  4.  
  5. #include <stdio.h>
  6.  
  7. #define TAM_HEXCODE 18
  8.  
  9. /*! Función principal hexdump */
  10. void hexdump(FILE *archivo_fd) {
  11. unsigned short int bucle_i;
  12. char caracter_actual = '\0';
  13. fseek(archivo_fd, 0, SEEK_SET);
  14.  
  15. while(!feof(archivo_fd)) {
  16. for(bucle_i = 0; bucle_i < TAM_HEXCODE; bucle_i++) {
  17. caracter_actual = fgetc(archivo_fd);
  18. if(caracter_actual == EOF)
  19. return (void) 0;
  20. printf("0x%.2x, ", caracter_actual);
  21. }
  22. printf("\n");
  23. }
  24. }
  25.  
  26. int main(int argc, char *argv[]) {
  27. FILE *archivo_fd;
  28.  
  29. if(argc < 2)
  30. return 1;
  31. if(!(archivo_fd = fopen(argv[1], "rb")))
  32. return 1;
  33.  
  34. printf("char hexdump2c[] = {\n");
  35. hexdump(archivo_fd);
  36. printf("\b\b};\n");
  37.  
  38. return 0;
  39. }

También pueden encontrarse este y otros programas en: http://pastes.mikygonzalez.16mb.com/?ad2cea935c65385a#c0bI9LQ9BD6f8snTLpklw/LgzXRnOEs6lJHJWqbbb9c= o en mi pagina personal (en la firma).

Saludos


En línea

Mi blog personal, con información acerca de programación, seguridad, desarrollo y electrónica:


EN CONSTRUCCIÓN
0xDani


Desconectado Desconectado

Mensajes: 1.077



Ver Perfil
Re: Codigo de un "hexdump2c"
« Respuesta #1 en: 8 Marzo 2013, 20:35 pm »

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.

Código
  1. #include <elf.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6.  
  7. void hexaDump(const void *ptr, size_t mem_sz)
  8. {
  9. uint8_t *u8_ptr = ptr;
  10. size_t i=0;
  11.  
  12. for(; i<mem_sz-1; i++)
  13. {
  14.  printf("%x, ", u8_ptr[i]);
  15. }
  16. printf("%x", u8_ptr[++i]);
  17. }
  18.  
  19.  
  20. int main(int argc, char **argv)
  21. {
  22. Elf32_Ehdr  elf_hdr;
  23. Elf32_Shdr *elf_shdr;
  24. Elf32_Shdr  shStrTab;
  25.  
  26. char *shStrTabData, *sectionData;
  27.  
  28. size_t i;
  29.  
  30. if(argc!=3)
  31. {
  32.  printf("Usage: %s <file> <section>\n", argv[0]);
  33.  return 0;
  34. }
  35.  
  36. FILE *fileToDump = fopen(argv[1], "r");
  37. if(!fileToDump)
  38. {
  39.  printf("Could not open the file, please check the path\n");
  40.  return 1;
  41. }
  42.  
  43. fread(&elf_hdr, sizeof(Elf32_Ehdr), 1, fileToDump);
  44. fseek(fileToDump, elf_hdr.e_shoff + elf_hdr.e_shentsize*elf_hdr.e_shstrndx, SEEK_SET);
  45.  
  46. fread(&shStrTab, sizeof(Elf32_Shdr), 1, fileToDump);
  47. fseek(fileToDump, shStrTab.sh_offset, SEEK_SET);
  48.  
  49. shStrTabData = malloc(shStrTab.sh_size);
  50.  
  51. fread(shStrTabData, 1, shStrTab.sh_size, fileToDump);
  52.  
  53. elf_shdr = malloc(elf_hdr.e_shnum*sizeof(Elf32_Shdr));
  54.  
  55. fseek(fileToDump, elf_hdr.e_shoff, SEEK_SET);
  56. fread(elf_shdr, sizeof(Elf32_Shdr), elf_hdr.e_shnum, fileToDump);
  57.  
  58. _Bool foundFlag=false;
  59. for(i=0; i<elf_hdr.e_shnum; i++)
  60. {
  61.  if(!strcmp(shStrTabData+elf_shdr[i].sh_name, argv[2]))
  62.  {
  63.   foundFlag = true;
  64.   break;
  65.  }  
  66. }
  67.  
  68. if(!foundFlag)
  69. {
  70.  printf("Could not find the requested section\n");
  71.  return 2;
  72. }
  73.  
  74. sectionData = malloc(elf_shdr[i].sh_size);
  75. fseek(fileToDump, elf_shdr[i].sh_offset, SEEK_SET);
  76. fread(sectionData, 1, elf_shdr[i].sh_size, fileToDump);
  77.  
  78. printf("char hexdump[] = { ");
  79. hexaDump(sectionData, elf_shdr[i].sh_size);
  80. printf(" }\n\n");
  81.  
  82.  
  83. free(shStrTabData);
  84. free(elf_shdr);
  85. free(sectionData);
  86.  
  87. fclose(fileToDump);
  88.  
  89. return 0;
  90. }


« Última modificación: 8 Marzo 2013, 21:39 pm por 0xDani » En línea

I keep searching for something that I never seem to find, but maybe I won't, because I left it all behind!

I code for $$$
Hago trabajos en C/C++
Contactar por PM
Miky Gonzalez

Desconectado Desconectado

Mensajes: 87

http://mikygonzalez.comule.com/blog/


Ver Perfil WWW
Re: Codigo de un "hexdump2c"
« Respuesta #2 en: 8 Marzo 2013, 23:18 pm »

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
En línea

Mi blog personal, con información acerca de programación, seguridad, desarrollo y electrónica:


EN CONSTRUCCIÓN
0xDani


Desconectado Desconectado

Mensajes: 1.077



Ver Perfil
Re: Codigo de un "hexdump2c"
« Respuesta #3 en: 9 Marzo 2013, 14:38 pm »

@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.
En línea

I keep searching for something that I never seem to find, but maybe I won't, because I left it all behind!

I code for $$$
Hago trabajos en C/C++
Contactar por PM
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines