SHARE
TWEET

Untitled

a guest Apr 5th, 2012 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *    Contabilizaci√≥n patatera de NOPs dentro de un ejecutable.
  3.  *    Copyright (C) 2012  Gonzalo J. Carracedo
  4.  *
  5.  *    This program is free software: you can redistribute it and/or modify
  6.  *    it under the terms of the GNU General Public License as published by
  7.  *    the Free Software Foundation, either version 3 of the License, or
  8.  *    (at your option) any later version.
  9.  *
  10.  *    This program is distributed in the hope that it will be useful,
  11.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *    GNU General Public License for more details.
  14.  *
  15.  *    You should have received a copy of the GNU General Public License
  16.  *    along with this program.  If not, see <http://www.gnu.org/licenses/>
  17.  */
  18.  
  19. #include <stdio.h>
  20.  
  21. int
  22. main (int argc, char *argv[])
  23. {
  24.   FILE *fp;
  25.   unsigned char byte;
  26.   int offset;
  27.   int stripsize;
  28.   int state;
  29.   int total;
  30.  
  31.   if (argc != 2)
  32.   {
  33.     fprintf (stderr, "Usage: %s <executable file>\n", argv[0]);
  34.     return 1;
  35.   }
  36.  
  37.   if ((fp = fopen (argv[1], "rb")) == NULL)
  38.   {
  39.     perror (argv[1]);
  40.     return 1;
  41.   }
  42.  
  43.   state = 0;
  44.   offset = 0;
  45.   total = 0;
  46.    
  47.   while (fread (&byte, 1, 1, fp) == 1)
  48.   {
  49.     if (!state)
  50.     {
  51.       if (byte == 0x90) /* RET */
  52.       {
  53.         stripsize = 1;
  54.         state++;
  55.       }
  56.     }
  57.     else
  58.     {
  59.       if (byte == 0x90)
  60.         stripsize++;
  61.       else
  62.       {
  63.         if (stripsize > 4)
  64.         {
  65.         //  fprintf (stderr, "%s: %d nops at 0x%08x\n", argv[1], stripsize, offset - stripsize);
  66.           total += stripsize;
  67.         }
  68.        
  69.         state--;
  70.       }
  71.     }
  72.    
  73.     offset++;
  74.   }
  75.  
  76.   printf ("%s: %d nops available for fun and profit\n", argv[1], total);
  77.  
  78.   return 0;
  79. }
RAW Paste Data
Top