-2

As a part of an assignment I'm working on, I'm trying to read hex values from a file using the following code steps:

char buf[2048];
FILE *fp = fopen("/home/httpd/AS1/binary.bin", "r");
fgets(buf, 1024, fp);

I created a binary file that has values like for example /xff/xff/xff .... etc

and every time I read the file using the provided code, the hex values (shellcode) get change into different numerical values

I also cannot modify to ready the values in a loop one at a time as I am supposed to use the code as is.

I tried creating the file containing the values in various ways:

$ vi shcode.bin
$ vi shcode

I input the values into the file in various ways:

/xff/xff/xff
"/xff/xff/xff"
ÿÿÿ

and every time I read the file, the values gets changed.

I spent countless hours trying to find a solution with no result. Could you please help with what I'm doing wrong

JB.
  • 40,344
  • 12
  • 79
  • 106
PaNed
  • 21
  • 1
  • 1
  • 1
  • 2
    The code you give has nothing to do with numerical values, so how can they get changed? All you are doing is to read a character string from a file, which might contain anything. – Weather Vane Sep 13 '15 at 17:05
  • At least you must open the file with `"rb"`, that is, in binary mode. And vi is a _text_ editor, not a binary file editor. – Paul Ogilvie Sep 13 '15 at 17:52

1 Answers1

4

This opens a text file and using fgets, reads a line containing the hex values. The sscanf format, " /x%x%n" skips any whitespace, scans a /, then an x, a hex value and by way of %n saves the number of characters processed in offset. used and offset allow sscanf to work through the hex buffer.

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char *argv[])
{
    FILE *fp = NULL;
    char hex[1024] = "";
    int each = 0;
    size_t used = 0;
    size_t offset = 0;

    if ( ( fp = fopen ( "shcodes", "r")) == NULL) {
        printf ( "could not open file\n");
        return 1;
    }
    while ( fgets ( hex, sizeof ( hex), fp)) {
        used = 0;
         //work through the string as long the format is matched
        while ( ( sscanf ( hex + used, " /x%x%n", &each, &offset)) == 1) {
            printf ( "sscanf this int %d as hex %x\n", each, each);
            used += offset;
        }
    }
    fclose ( fp);
    return 0;
}

The text file used is

/xff/xff/xff/xff/xff/xff/xff/xff/xff/xff/xff/xff/xff/xff/xff
/x0f/x1f/x2f/x3f/x4f/x5f/x6f/x7f/x8f/x9f/xaf/xbf/xcf/xff/xff
/x01/x11/x21/x31/x41/x51/x61/x71/x81/x91/xa1/xb1/xc1/xf1/xff

This should work for a binary file. fread is a better choice for reading binary data. bytes will store the number of bytes read.

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char *argv[])
{
    FILE *fp = NULL;
    unsigned char hex[1024] = "";
    int each = 0;
    size_t bytes = 0;

    if ( ( fp = fopen ( "shcodes.bin", "rb")) == NULL) {
        printf ( "could not open file\n");
        return 1;
    }
    //loop as long as there are bytes to read
    while ( ( bytes = fread ( &hex, 1, 1024, fp)) > 0) {
        for ( each = 0; each < bytes; each++) {
            printf ( "read this char as int %u and as hex %x\n", hex[each], hex[each]);
        }
    }
    fclose ( fp);
    return 0;
}
user3121023
  • 8,181
  • 5
  • 18
  • 16