• Welcome to Valhalla Legends Archive.
 

Can't figure out why this isn't working

Started by Eli_1, March 03, 2004, 03:48 PM

Previous topic - Next topic

Eli_1

This is just a little program I wanted to try to make because I don't know how to do StrToHex conversion in C/C++...


#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {

   if (argc == 1) {
      printf("Argument needed");
      return -1;
   }

   for (int i = 0; i < argc; i++) {
      char buffer[2048];
      if (i == 0)
         continue; //Ghetto :(
      
      if (argv[i] == NULL)
         continue;
      
      char *argument = argv[i];
      for (int i2; i2 < sizeof(argument); i2++)
         sprintf(buffer, "%x", argument[i2]);
      
      printf("Hex Output: %s\r\n", buffer);
      delete [] buffer;
   }
   
   return 0;
}


Command:

Hex.exe test1 test2 test3


Output:

Hex Output:
Hex Output:
Hex Output:


Any ideas?

iago

What is the inner loop (i2) supposed to do?
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Adron

Quote from: iago on March 03, 2004, 05:36 PM
What is the inner loop (i2) supposed to do?

Studying the logic flow of the program makes me believe it's intended to go through the characters in the argument, adding the hexadecimal value for each to the buffer.

Arta

You shouldn't be calling delete on buffer. Only call delete on memory allocated with new.  Also, every time the inner loop iterates (other than the first) it'll overwrite whatever was in buffer.

Eli_1

#4
Thanks guys, I got it fixed
the main problem was with my inner loop

for (int i2; i2 < sizeof(argument); i2++)


the above was never working because I didn't have it as int i2 = 0; ect...

for anymore who wants to see, here's the working code


#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {

   if (argc == 1) {
      printf("Argument needed");
      return -1;
   }

   for (int i = 0; i < argc; i++) {
      char buffer[2048];
      if (i == 0)
         continue; //Ghetto :(
      
      if (argv[i] == NULL)
         continue;
      
      char *argument = argv[i], tmp[4];
      for (int i2 = 0; i2 < strlen(argument); i2++) {
         sprintf(tmp, "%x", argument[i2]);
         strcat(buffer, tmp);   
      }
      
      printf("Hex Output: %s\r\n", buffer);
   }
   
   return 0;
}

Skywing

Why don't you use for(i = 1 ....)...?

Eli_1


Kp

hmm, I think that will behave badly if you use an upper-ASCII character in an argument.  Assuming your architecture considers char to be signed, it will sign extend the character to pass it to sprintf.  Since you're using the horrible sprintf instead of snprintf, it can overwrite your array if you pass a too-large value.  Sign extending (char)-1 produces 0xffffffff, which is going to be 8 bytes of output (you aren't asking sprintf to do the 0x).  8 > 4, so you'll overflow the tmp buffer.  Bad things result.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!