岁月联盟 - 技术社区 - BBS.SYUE.COM's Archiver

猪猪 发表于 2007-2-13 09:45

dumps C style opcodes between two provided offsets

文章作者:nemo@felinemenace.org

[code]/*
* ~[ fm-dmpsc.c ]
*
* Dump the opcodes from a file, starting from an offset provided.
*
* written by -( nemo @ felinemenace.org )-
*
*             _,'|         _.-''``-...___..--';)
*             /_ \'.     __..-' ,     ,--...--'''
*             <\   .`--'''     `   /'
*             `-';'           ;   ; ;
*         __...--''   ___...--_..' .;.'
*       fL (,__....----'''     (,..--''
*             -( [url]http://www.felinemenace.org[/url] )-
*
*/

#include <stdio.h>
#include <errno.h>

#define MAXSC      10000

void usage(char *file)
{
     printf("usage: %s <file> <offset1> [<offset2>]\n",file);
     exit(1);
}

int main(int ac, char **av)
{
     char shellcode[MAXSC] = "char shellcode[] = {\n\"";
     unsigned int curr,offset1,offset2 = 0xffffffff;
     FILE *fp;
     char *ptr = shellcode + strlen(shellcode) - 1;
     unsigned char hex;
     int cnt = 0;

     if(ac < 3 || ac > 4)
           usage(*av);
     sscanf(av[2],"%p",&offset1);
     if(av[3])
           sscanf(av[3],"%p",&offset2);
     if(offset2 <= offset1)
           usage(*av);
     if(!(fp = fopen(av[1],"r+"))) {
           printf("Error opening file.\n");
           exit(1);
     }
     if(fseek(fp,offset1,SEEK_SET) == -1){
           printf("Error seeking to offset1.\n");
           exit(1);
     }
     do {
           if((offset1 + cnt) >= offset2) {
                 break;
           }
           if(cnt && !(cnt % 16)) {
                 strcat(ptr,"\"\n\"");
                 ptr+=3;
           }
           strcat(ptr,"\\x");
           if(!(fread(&hex, 1,1,fp) ==1)) {
                 if(errno) {
                       printf("An error has occured reading from the file.\n");
                       exit(1);
                 }      
                 break;
           }
           ptr+=3;
           sprintf(ptr,"%02x",hex);      
           cnt++;
     } while(ptr++ < (shellcode + MAXSC - 4)) ;
     strcat(ptr,"\"\n};\n");      
     printf("// shellcode generated by -( [email]nemo@felinemenace.org[/email] )-\n");
     printf("%s\n",shellcode);

     return 0;
}[/code]

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.