Your browser does not seem to support CSS. If images appear below, please disregard them.
toggle
April
S M T W T F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Sponsored Links
Latest Postings
Topic Options
Rate This Topic
#17858 - 01/12/03 05:23 PM Crash Course in VB memory hacks and strings
Joined: Mar 2002
Posts: 1,273
SilentRage Offline
DollarDNS Owner
SilentRage Offline
DollarDNS Owner

Joined: Mar 2002
Posts: 1,273
OH, USA
I've written a previous tutorial on memory hacking with CopyMemory, but it wasn't entirely accurate or thorough. This crash course will give you the totally accurate and thorough situation on VB strings and string arrays.

A reminder:
StrPtr function returns the pointer to the string associated with a string variable.

VarPtr function returns the pointer to the actual variable.

Strings

Memory Commentary:
A string variable stores a 4 byte pointer to the first byte of the actual string. However, before the string is a 4 byte length descripter OF the string in number of bytes. In VB strings are UNICODE and are terminated by a 2-byte null character. However, make sure to only pass strings as ByVal VarPtr for the source - cause VB has a tendancy to pass the UNICODE strings as ASCII which totally screws things up.

'The returned address is identical to StrPtr(MyString)
CopyMemory Address, ByVal VarPtr(MyString), 4

'Store the length of MyString
CopyMemory Length, ByVal StrPtr(MyString) - 4, 4

'Copy the string from MyString to the Buffer
CopyMemory Buffer, ByVal StrPtr(MyString), Length

String Arrays

Memory Commentary:
A array variable stores a 4 byte pointer to the first element of the actual array. String arrays are actually an array of 4 byte pointers which point to the actual string in each element. Before the actual array is a 4 byte length descripter of the array. So like, if an array has 3 elements, the length will be 12. Each individual element being a pointer to the string associated with that element. That string holds true to strings pointed to by normal string vars. It is preceded by a 4 byte length of the actual string.

'Store the memory location of the array
CopyMemory Address, ByVal VarPtr(MyStringArray(0)), 4

'The 4 bytes before the array is the length of the array in bytes
CopyMemory Length, ByVal Address - 4, 4

'Retrieve the address to the string in the first element
CopyMemory Address, ByVal StrPtr(MyStringArray(0)), 4

'Retrieve the length of the string in the first element
CopyMemory Length, ByVal StrPtr(MyStringArray(0)) - 4, 4

'Copy the string in the first element to a string variable
CopyMemory MyString, ByVal StrPtr(MyStringArray(0)), Length


Domain Registration, Hosting, Management
http://www.dollardns.net
Top
Sponsored Links
#17859 - 01/24/03 07:37 AM Re: Crash Course in VB memory hacks and strings
Joined: Mar 2002
Posts: 256
ninjaneo Offline
UGN Security Staff
ninjaneo Offline
UGN Security Staff

Joined: Mar 2002
Posts: 256
CA, USA
thanks SR, what I was looking for

btw.. this was posted using meh port forwarde :>

Top
#17860 - 02/19/03 01:58 PM Re: Crash Course in VB memory hacks and strings
Joined: Mar 2002
Posts: 257
psychogen Offline
Member
psychogen Offline
Member

Joined: Mar 2002
Posts: 257
UK
nice nice :-P


The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean, "Someone who loves to program and enjoys being clever about it."
--------------------
"Its not a bug, its a feature" (Epic Games)
Top

Member Spotlight
Gremelin

Gremelin
Portland, OR; USA
Posts: 7,194
Joined: February 2002
Show All Member Profiles 
Forum Statistics
Forums46
Topics45,509
Posts80,677
Members2,157
Most Online1,567
Apr 25th, 2010
Top Posters(All Time)
UGN Security 38,673
Gremelin 7,194
§intå× 3,255
SilentRage 1,273
Ice 1,146
pergesu 1,136
Infinite 1,041
jonconley 955
Girlie 908
unreal 860
Newest Members
Herbert_Sherbert, codemauve, Lillysdragon1984, Brewwit, boa
2157 Registered Users
Who's Online Now
0 registered members (), 2 guests and 4 spiders.
Latest News