Contribute  :  Web Resources  :  Past Polls  :  Site Statistics  :  Downloads  :  Forum  
    BiW ReversingThe challenge is yours    
 Welcome to BiW Reversing
 Saturday, December 03 2022 @ 12:48 AM CET
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Integer hack (C/C++ typecast hack)

 
Post new topic   Reply to topic    www.reversing.be Forum Index -> Coding Corner
View previous topic :: View next topic  
Author Message
BoR0
Regular
Regular


Joined: 28 Feb 2005
Posts: 105
Location: Europe

PostPosted: Sun Oct 09, 2005 1:31 pm    Post subject: Integer hack (C/C++ typecast hack) Reply with quote

This hack is used to store a string in integer.

The following example shows how one can store string "BoR0 ; )", 0
in 2 integers.

(Note: AT&T syntax is used for O/S'es like FreeBSD, etc)

Code:
// comment this line if you want to compile it with AT&T syntax
#define WIN

#include <stdio.h>

int str1, str2 = 0;

int main() {

// str1[0] = 'B';
// type cast error? ok. lets do some asm

#ifdef WIN

__asm {
mov dword ptr [str1], 0x30526F42; // "0RoB" -> "BoR0"
mov dword ptr [str2], 0x00293B20; // " ); " -> " ;)", 0
}

printf("%s\n", &str1, &str2);

#else

__asm("movl $0x30526F42, (str1)\n"); // "0RoB" -> "BoR0"
__asm("movl $0x293B20, (str2)\n")  ; // ");  " -> " ;)", 0

printf("%s%s\n", &str1, &str2);

#endif

return 0;

}


Good luck! Wink


Last edited by BoR0 on Sun Oct 09, 2005 4:17 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
moniker
Regular
Regular


Joined: 05 Sep 2005
Posts: 123
Location: lage lande

PostPosted: Sun Oct 09, 2005 1:52 pm    Post subject: Reply with quote

asuming an int is a 32bit quantity

wouldn't this work too

Code:

char t[]="BoR0 ;)";
int a,b;
a=(t[0]<<24) | (t[1]<<16) | (t[2] <<8) | (t[3]);


or playing around with the
reinterpret_cast thingie

like so
Code:

char text[]="Bor0 ;)\0";
int a=*(reinterpret_cast<int *>(text));


post is for c++, i don't bother to much with c
Back to top
View user's profile Send private message
BoR0
Regular
Regular


Joined: 28 Feb 2005
Posts: 105
Location: Europe

PostPosted: Sun Oct 09, 2005 4:19 pm    Post subject: Reply with quote

The idea with shifts and ors is a good one.

Anyway, what inspired me to code this hack was an op saying "You cannot store strings in an integer" on the #C channel on IRC Wink
Back to top
View user's profile Send private message Visit poster's website
moniker
Regular
Regular


Joined: 05 Sep 2005
Posts: 123
Location: lage lande

PostPosted: Sun Oct 09, 2005 4:38 pm    Post subject: Reply with quote

goes to show the level of intelligence on IRC from time to time Wink

i like the reinterpret cast too, although not C, but C++
it's basicly telling the compiler: "F*CK OFF i know what i'm doing."
Back to top
View user's profile Send private message
BoR0
Regular
Regular


Joined: 28 Feb 2005
Posts: 105
Location: Europe

PostPosted: Mon Oct 10, 2005 12:23 am    Post subject: Reply with quote

That's the spirit. You and I have the same meaning Very Happy
Back to top
View user's profile Send private message Visit poster's website
parabytes
Frequent poster
Frequent poster


Joined: 14 Apr 2005
Posts: 92
Location: Israel

PostPosted: Thu Oct 13, 2005 10:54 am    Post subject: Reply with quote

I'm not much into HLL....

But didn't you forget one '%s' in the windows code? Smile

_________________
thoughts roam free and endless..
Back to top
View user's profile Send private message Visit poster's website
Knight
Regular
Regular


Joined: 21 Aug 2005
Posts: 122

PostPosted: Thu Oct 13, 2005 4:06 pm    Post subject: Reply with quote

And why to use inline asm when you can do this:

str1 = 0x30526F42;
str2 = 0x293B20;

Result the same, so asm no needed here.

And about that guy form irc... It looks like he has no idea how computers works. Isolated from real world in hll cage. But some time ago i was the same. Asm makes you understand things in very different way, and i like that Smile

Regards,
Knight
Back to top
View user's profile Send private message
BoR0
Regular
Regular


Joined: 28 Feb 2005
Posts: 105
Location: Europe

PostPosted: Thu Oct 13, 2005 11:03 pm    Post subject: Reply with quote

parabytes: I tried it on Windows, works only with one "%s".. probably because it stores the data one after another, *nix does not so..

Knight: yes, that is another way of doing it Wink
Back to top
View user's profile Send private message Visit poster's website
parabytes
Frequent poster
Frequent poster


Joined: 14 Apr 2005
Posts: 92
Location: Israel

PostPosted: Fri Oct 14, 2005 12:52 am    Post subject: Reply with quote

Ah yeah, cause the str1 has no \0 char at the end of it Wink

Then pushing str2 is redundant since the function isn't usin' it anyway... Smile

_________________
thoughts roam free and endless..
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.reversing.be Forum Index -> Coding Corner All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
 Copyright © 2022 BiW Reversing
 All trademarks and copyrights on this page are owned by their respective owners.
Powered By Geeklog 
Created this page in 0.89 seconds