Shoutbox

Hex Help - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Hex Help (/showthread.php?tid=70751)

Hex Help by TheGuruSupremacy on 01-16-2007 at 04:20 PM

Hi man, i need help....:I make this function:

function SetDataPatch(string){
var newstring =""
for (var i=0;i<string.length;i++){
newstring += "\x" + Hex(string.charCodeAt(i)) + "\x00"}
Debug.Trace(newstring)
return newstring}}

but plus found this error,this is in italian but i'll try to translate it in english:

Lo script è stato fermato(the script has been stopped)
Errore: Prevista cifra esadecimale.(Error: expect digit hex)
       Linea: 43 Codice: -2146827265(line:43 Code: -2146827265)

i need to make a string like this "\xFF\x00\xFC" CAN SOMEONE HELP ME PLEASE??? Sorry for my english


RE: Hex Help by Matti on 01-16-2007 at 05:04 PM

So, if I understand, you have a string like

code:
var thing = "FF00FC";
and you need a function which makes it like this:
code:
var new_thing = "\xFF\x00\xFC";

If so, this should do:
code:
function SetDataPatch(string) {
  var newstring = "";
  for(var i=0; i<string.length; i+=2) {
    var hex = string.substr(i, 2); //The hexadecimal character code
    var dec = parseInt(hex, 16); //The decimal character code
    newstring += String.fromCharCode(dec); //Adds the character to the string
  }
  return newstring;
}

RE: RE: Hex Help by TheGuruSupremacy on 01-16-2007 at 05:16 PM

quote:
Originally posted by Mattike
So, if I understand, you have a string like
code:
var thing = "FF00FC";
and you need a function which makes it like this:
code:
var new_thing = "\xFF\x00\xFC";

If so, this should do:
code:
function SetDataPatch(string) {
  var newstring = "";
  for(var i=0; i<string.length; i+=2) {
    var hex = string.substr(i, 2); //The hexadecimal character code
    var dec = parseInt(hex, 16); //The decimal character code
    newstring += String.fromCharCode(dec); //Adds the character to the string
  }
  return newstring;
}


I thank you but this code for me don't work

function SetDataPatch(string) {
  var newstring = "";
    for(var i=0; i<string.length; i+=2) {
    var hex = string.substr(i, 2); //The hexadecimal character code
    var dec = parseInt(hex, 16); //The decimal character code
    newstring += String.fromCharCode(dec); //Adds the character to the string
  }
  Debug.Trace(newstring)
  return newstring;
}

RE: Hex Help by Plik on 01-16-2007 at 07:28 PM

In what way doesn't it work?
Because it works fine here :-/


RE: Hex Help by phalanxii on 01-17-2007 at 04:15 AM

I think he wants a function which takes a string like

code:
var string = "ÿü"
and turns it into
code:
var newstring = "\xFF\x00\xFC"
or by the looks of his code
code:
var newstring = "\xFF\x00\xFC\x00"
In other words, he wants to take each character in the string, turn them into the hex unicode notation ("\x__"), insert "\x00" between each character and return the whole thing as a string. For example, SetDataPatch("abc") will return "\x61\x62\x62".

As far as I know, a function like this is completely useless because "a" === "\x61" (someone correct me on this one ^o)). However, if the function is to give "\\xFF\\x00\\xFC\\x00" (which will display as "\xFF\x00\xFC\x00" to the user), you can use this:
code:
function SetDataPatch(string) {
   var newstring = "";
   for(var i = 0; i < string.length; i++) {
      newstring += "\\x" + string.charCodeAt(i).toString(16) + "\\x00";
   }
   Debug.Trace(newstring);
   return newstring;
}

RE: RE: Hex Help by CookieRevised on 01-17-2007 at 05:13 AM

quote:
Originally posted by TheGuruSupremacy
Hi man, i need help....:I make this function:

function SetDataPatch(string){
var newstring =""
for (var i=0;i<string.length;i++){
newstring += "\x" + Hex(string.charCodeAt( i)) + "\x00"}
Debug.Trace(newstring)
return newstring}}

but plus found this error,this is in italian but i'll try to translate it in english:

Lo script è stato fermato(the script has been stopped)
Errore: Prevista cifra esadecimale.(Error: expect digit hex)
       Linea: 43 Codice: -2146827265(line:43 Code: -2146827265)

i need to make a string like this "\xFF\x00\xFC" CAN SOMEONE HELP ME PLEASE??? Sorry for my english

The error is because the function Hex doesn't exist in JScript. And if you've made your own Hex function then note that "\x" must always be followed by a 2 digit hexadecimal number.
Thus "\xE" will not work either, it must be "\x0E".

PS: If you get such errors, try to split up the line which caused the error to see what part exactly triggered it...

Anyways, even if Hex existed and it does always return a 2 digit number, your function wouldn't work in all cases either though, see below.

Also "\x00" will make a string with the ascii code character 0, not a string which will display \x00. The "\" character is an escape character, meaning it has special function if used inside a string (like defining a character by its hexadecimal code). To show the slash ("\") you must escape it with the escape character, thus "\\".

quote:
Originally posted by phalanxii
As far as I know, a function like this is completely useless because "a" === "\x61" (someone correct me on this one ^o)).
Not always (it would only be useless in option a, see below).

Seeing the function name ("patch") I suspect he wants to add null characters between the original characters (thus option b, see below). In other words convert a string to its unicode counterpart. Or to put in in context with JScript: to convert a unicode string to double unicode notation...

eg: to convert the this byte data
    FF 00 0A 00 0E 14
to
    FF 00 00 00 0A 00 00 00 0E 00 14 00
where 00 are the added bytes

This could be usefull for patching and other related stuff...



--------------------------



However, TheGuruSupremacy,

Always specify exactly and in detail what you want and what the purpose of the function is when you request help. As you see above, the solution highly depends on what you exactly want with that function. Be very specific and give clear examples.

In situation likes this, it is even recommended to show the entire script so people can clearly see what the purpose it.

Since this function deals with unicode, ascii code, string literals, screen outputs, etc, it is very important to state clearly what is what.

eg: assuming the input string contains the two characters 0xFF and 0xFC (you didn't specify this either), when you say "i need to make a string like this "\xFF\x00\xFC" ", then what do you mean by 'string':

a) Do you mean a 2 character long string containing the unicode characters FF and FC

b) Do you mean a 4 character long string containing the characters FF, 00, FC and 00

c) Do you mean you want a 'string' which would literally display \xFF\x00\xFC\x00



To helpers: Try not to guess. If you aren't sure what is requested ask details and examples first ;)



--------------------------



For now I assume that phalanxii guessed correctly and that you want to show literally the hexadecimal codes which make up a string, thus option c:

quote:
Originally posted by phalanxii
However, if the function is to give "\\xFF\\x00\\xFC\\x00" (which will display as "\xFF\x00\xFC\x00" to the user), you can use this:
code:
function SetDataPatch(string) {
   var newstring = "";
   for(var i = 0; i < string.length; i++) {
      newstring += "\\x" + string.charCodeAt(i).toString(16) + "\\x00";
   }
   Debug.Trace(newstring);
   return newstring;
}

But don't assume the characters in the string are always between ASCII 0 and 255, they can be anything from 0 to 65535. In other words they are always unicode.

Thus charCodeAt(i) can return anything from 0 to 65535.

This means that you should never just add "\x00" to the string.

Thus, toString(16) will not always return a 2 digit hexadecimal number, it might just be 1 digit or even up to 4 digits (and it will always be lowercased). This also means that in the case of 1 digit the function will fail too (see reply to TheGuruSupremacy above).

The fixed function:
code:
function SetDataPatch(string) {
    var newstring = "";
    for (var i = 0; i < string.length; i++) {
        // Get character and convert to hexadecimal

        var charHexcode = string.charCodeAt(i).toString(16).toUpperCase();
        // Since hexcode can be anything from 1 to 4 digits long
        // we need to properly format it by adding 0's.
        charHexcode = ("000" + charHexcode).substr(charHexcode.length-1);
        // The least significant part (last 2 digits) is the
        // first part to return in LE (little endian) format
        newstring += "\\x" + charHexcode.substr(2);
        // The most significant part (first 2 digits) is the
        // last part to return in LE (little endian) format
        newstring += "\\x" + charHexcode.substring(0, 2);
    }
    Debug.Trace(newstring);
    return newstring;
}

RE: RE: RE: Hex Help by TheGuruSupremacy on 01-17-2007 at 01:51 PM

OK Thank all boys...I'll try to explain what i want better:
Well...this is the code I used:


code:
function Hex(number) {return number.toString(16).toUpperCase()}//function by CookieRevised

function SetDataPatch(string){
var newstring=0
for (var i=0;i<string.length;i++){
newstring += "\x" + Hex(string.charCodeAt(i)) + "\x00"}
Debug.Trace(newstring)
return newstring}


function Patch(nAddress, sByteString) {
    var sBuffer = Interop.Allocate(++sByteString.length);
    for (var j = 0; j < sByteString.length; j++) sBuffer.WriteWORD(j, sByteString.charCodeAt(j));
    var hProc = Interop.Call("Kernel32", "GetCurrentProcess");
    Interop.Call("Kernel32", "WriteProcessMemory", hProc, nAddress, sBuffer.DataPtr, sByteString.length, 0);
}//Function By CookieRevised


i need to make a function that return in hex the bytes that i have to write in memory but i don't know i have to do it...example

if i used this code

This code:

code:
Patch(0x5F6D5C,"\x00\xFF\xFC")

it works perfectly

but i need to make the sByteString because it is not the constant and the data i have to write memory is an unicode string so i add to my function + "\x00"....for example if i pass to SetPatchData funtion this string "Example" i need to return

"\x45\x00\x78\x00\x61\x00\x6D\x00\x70\x00\x6C\x00\x65\x00"

that then i pass to Patch function.....if i use "\\x" instead "\x" don't work because the data that return is not in hex...

I hope that you understand me....

Thanks in advice and sorry for my english
RE: Hex Help by TheGuruSupremacy on 01-17-2007 at 06:06 PM

No one can help me????


RE: Hex Help by CookieRevised on 01-17-2007 at 07:00 PM

quote:
Originally posted by TheGuruSupremacy
No one can help me????
yes we can, but be patient please... In the mean time I very strongly suggest you read up on what strings are, how they are represented in memory and the differences between unicode, ascii and byte string and/or byte arrays. You'll really need this knowledge to understand everything involved. Otherwise, you just be copy/pasting code without knowing what you actually do.

This post will be updated when I finished writing my reply...
So no need to reply to this one either


;)




EDIT: read underlined part... ^o)
I guess I'll make a new post instead of updating this one then...:S
RE: RE: Hex Help by TheGuruSupremacy on 01-17-2007 at 07:16 PM

quote:
Originally posted by CookieRevised
quote:
Originally posted by TheGuruSupremacy
No one can help me????
yes we can, but be patient please... In the mean time I very strongly suggest you read up on what strings are, how they are represented in memory and the differences between unicode, ascii and byte string and/or byte arrays. You'll really need this knowledge to understand everything involved. Otherwise, you just be copy/pasting code without knowing what you actually do.

This post will be updated when I finished writing my reply...
So no need to reply to this one either

;)



i don't know how represent in memory with jscript the different strings
...if i had known it i would have not to post for help......I'm searching on google how to do it but i didn't find anything...(probably because I not spend too time)....Before posting i always trying to resolve my problems myself and if i can't to do it i'll post for help so i'll be patient but please don't tell me that i ask for help without knowing what i actually do....it's offensive....bye
RE: Hex Help by CookieRevised on 01-17-2007 at 08:29 PM

quote:
Originally posted by TheGuruSupremacy
Before posting i always trying to resolve my problems myself and if i can't to do it i'll post for help so i'll be patient but please don't tell me that i ask for help without knowing what i actually do....it's offensive....bye
I didn't say you didn't know what you were doing and that you just copy/paste stuff.

I said it involves some knoweldge of what unicode and ascii strings are and I said IF you simply copy/paste code you don't learn anything from it... What I said is actually nothing more than plain logic: if you want to know something, read up on some stuff about it. How is suggesting to read up on some background info about this while you wait on the detailed explaination, being any offensive??

On the other hand, it would be majorly offensive towards the people who try to help you with something if you just did copy/paste stuff though. Because like with this post, I simply could have posted the code and be done with it in 5 minutes, thus I took a lot of time to make this post and trying to explain the stuff behind it...




----------------------------------------------------------------------


quote:
Originally posted by TheGuruSupremacy
[SNIPPED]

I hope that you understand me....
I understand, and I already suspected you wanted to do that, but it involves a very good knowledge of ascii, unicode, and how everything is stored in memory.


Thus:
quote:
Originally posted by TheGuruSupremacy
for example if i pass to SetPatchData funtion this string "Example" i need to return

"\x45\x00\x78\x00\x61\x00\x6D\x00\x70\x00\x6C\x00\x65\x00"

This is correct as you state it, but do you exactly know what you just stated? Don't get me wrong though, I simply ask this because there can be a lot of confusion if one isn't used to deal with unicode vs. ascii vs. bytes. And as I said earlier it is absolutely vital that you post it in the correct form. eg: "\x45" is not the same as 0x45... "\x45" is 0x45 0x00

"Example" is the same as "\x45\x78\x61\x6D\x70\x6C\x65".

So if you meant to show the byte representation of this string with "\x45\x00\x78\x00..." then you got the bytes correct but you posted a totally wrong thing.

Because "\x45\x00\x78\x00etc" actually consists of the bytes 0x45 0x00 0x00 0x00 0x78 0x00 0x00 0x00.

So you should have said: "I need to return 0x45 0x00 0x78 0x00.". But in that case you got the wrong idea in what needs to be passed to the Patch() function and thus it wouldn't be correct......

Again, I say this because if you thought you needed to pass 0x45 0x00 0x78 0x00 (but used the wrong form in your post), and I said you where correct, then I might teach you the wrong thing.

-----------

Anyways,

You first need to take the unicode value of each character. This is a number between 0x0000 and 0xFFFF. It is after the high order and after the low order of that number that you need to add the "\x00", because that is what is being ignored by the Patch function since it expects a byte string, aka: a string consisting of only ascii characters from 0x00 to 0xFF.

To make it clear with an example:
The normal JScript string (thus which is always unicode) "This" has the bytes: 0x54 0x00 0x68 0x00 0x69 0x00 0x73 0x00

But this string, and let's say # is a unicode character: "#This" has the bytes: 0x11 0xEF 0x54 0x00 0x68 0x00 0x69 0x00 0x73 0x00

Notice that the character # does not have a null byte! It takes two bytes to represent a character in a unicode string, and that is also what you need to patch, 2 bytes per character.

Thus what you need to pass to the Patch function is not:
0x11 0xEF 0x54 0x00 0x68 0x00 0x69 0x00 0x73 0x00
but
0x11 0x00 0xEF 0x00 0x54 0x00 0x00 0x00 0x68 0x00 0x00 0x00 0x69 0x00 0x00 0x00 0x73 0x00 0x00 0x00
where 0x00 are the added null bytes which will be ignored.

This is indeed what your original function intends to do:
code:
function SetDataPatch(string){
var newstring=0
for (var i=0;i<string.length;i++){
newstring += "\x" + Hex(string.charCodeAt( i)) + "\x00"}
Debug.Trace(newstring)
return newstring}
However there are some problems with it which I also already told you in my first reply in this thread though....

You can not simply add a string like "EF" to the string "\x" and expect it to be the string "\xEF".


For starters, the Hex() function can return a string of any length (in this case 1 to 4 digits) since characters can be anything from code 0x0 to 0xFFFF...

But even if it did always return 2 digits, then "\x" would still be invalid, since \x is not an actual string consisting of the characters '\' and 'x' but an escape command, and the whole escape command must be \x??   where ?? must be a 2 digit hexadecimal number.

What SetDataPatch() must do is putting a null byte (0x00) after each byte of the string and that can only be done by manipulating the original string on byte level.

To do this forget about using the Hex() function, you don't need that. There are more ways than using the escape command \x?? to add characters to a string. What you need is the function fromCharCode().

First do what the previous posted SetDataPatch() does: get each character of the string one by one and split the unicode code up into the lower order and higher order byte. Then append these bytes (0 to 255) as being new characters to a string.

code:
function SetDataPatch(sUnicodeString) {
    var newstring = "";
    for (var i = 0; i < sUnicodeString.length; i++) {
        // Get character
        var charCode = sUnicodeString.charCodeAt(i);
        // Get the low order byte (with an AND operation)
        // And write it as a new character
        newstring += String.fromCharCode(charCode & 0xFF);
        // Get the high order byte (with a right bit shift operation)
        // And write it as a new character
        newstring += String.fromCharCode(charCode >>> 8);
    }
    return newstring;
}
Note that fromCharCode() will write a unicode character! Thus if you say fromCharCode(210) it will write actually two bytes: 0xD2 and 0x00. And since we make sure the character code passed to this function is always between 0 and 255, the second byte written will always be 0x00.

However, note that all this is a very slow process.
To overcome this, I've used ASM in one of my script patches though... anyways...

But the most simple solution is to forget the whole SetDataPatch() function and alter the Patch() function. If you have good knowledge of what you actually are doing with all this you would have come to that conclussion already though (just to say again that copy/pasting something isn't good if you don't understand what is being involved).

However, this solution has drawbacks because you would only be able to write unicode strings and not byte arrays (without even more dodgy unicode to ascii manipulations).

What does Patch() do? The important stuff in that function is the use of the WriteProcessMemory API which does nothing more than writing a passed byte array to memory. A unicode string is just a array of bytes, and this is how simple it gets... So if you want to write an unicode string to memory, then all you need to do is pass the unicode string directly to the WriteProcessMemory API.

The reason why the original Patch() function has some string manipulations in it is exactly because strings in JScript are always unicode. And it is very rare to patch only unicode strings so it needs this string manipulation so you can pass byte arrays in a convenient fast way. But if you do want to patch only unicode strings and nothing else, then you don't need those string manipulations at all. This is also explained (without so much words) in the original comments of that function btw...
RE: RE: Hex Help by TheGuruSupremacy on 01-17-2007 at 09:35 PM


quote:
I said it involves some knoweldge of what unicode and ascii strings are and I said IF you simply copy/paste code you don't learn anything from it... What I said is actually nothing more than plain logic: if you want to know something, read up on some stuff about it. How is suggesting to read up on some background info about this while you wait on the detailed explaination, being any offensive??



ok now i have understood how you wanted say me and i'm completely agree....:)...so sorry if i offended you 





----------------------------------------------------------------------

quote:
This is correct as you state it, but do you exactly know what you just stated? Don't get me wrong though, I simply ask this because there can be a lot of confusion if one isn't used to deal with unicode vs. ascii vs. bytes. And as I said earlier it is absolutely vital that you post it in the correct form. eg: "\x45" is not the same as 0x45... "\x45" is 0x45 0x00

I didn't knew that "\x45" was 0x45 0x00 i though that it was 0x45  and i was confused  because i didn't know that unicode was an array of bytes....Thanks now i know it :)



I have understand most of you said(not all cause of my bad knowing of english)tomorrow i read again attentively...However i thank you very much you have always helped me and you'll be credited in my next scripts for it....I have also tried your function and it works perfectly...Thanks again