VB Decompiler Forum Index VB Decompiler
Hosted by TheAutomaters.com
 
  MemberlistMemberlist
 

Visual Basic .NET Binary File Class

 
   VB Decompiler Forum Index -> Utilities & User Contributions
Author Message
_aLfa_
Site Admin


Joined: 21 Sep 2002
Posts: 233
Location: Aveiro, Portugal

Posted: Sat Aug 07, 2004 1:26 am     Post subject: Visual Basic .NET Binary File Class

I've made a handy class to help me with decompiling stuff.
I think it's worth to share, thanks to MrU for the idea

[vbnet:1c8s7yjx]
  1. Option Explicit On
  2. Option Strict On
  3.  
  4. Public Class cFile
  5.   Private fsFile As IO.FileStream
  6.   Private brFile As IO.BinaryReader
  7.  
  8.   Public ReadOnly Property Length() As Long
  9.     Get
  10.       Return brFile.BaseStream.Length
  11.     End Get
  12.   End Property
  13.   Public Property Offset() As Long
  14.     Get
  15.       Return brFile.BaseStream.Position
  16.     End Get
  17.     Set(ByVal newValue As Long)
  18.       brFile.BaseStream.Position = newValue
  19.     End Set
  20.   End Property
  21.  
  22.   Public Function ReadByte(Optional ByVal Offset As Long = -1) As Byte
  23.     If Offset > -1 Then
  24.       brFile.BaseStream.Position = Offset
  25.     End If
  26.     Return brFile.ReadByte
  27.   End Function
  28.   Public Function ReadBytes(ByVal Count As Integer, Optional ByVal Offset As Long = -1) As Byte()
  29.     If Offset > -1 Then
  30.       brFile.BaseStream.Position = Offset
  31.     End If
  32.     Return brFile.ReadBytes(Count)
  33.   End Function
  34.   Public Function ReadInt16(Optional ByVal Offset As Long = -1) As Short
  35.     If Offset > -1 Then
  36.       brFile.BaseStream.Position = Offset
  37.     End If
  38.     Return brFile.ReadInt16
  39.   End Function
  40.   Public Function ReadInt32(Optional ByVal Offset As Long = -1) As Integer
  41.     If Offset > -1 Then
  42.       brFile.BaseStream.Position = Offset
  43.     End If
  44.     Return brFile.ReadInt32
  45.   End Function
  46.   Public Function ReadString(Optional ByVal Offset As Long = -1) As String
  47.     ' change offset
  48.     If Offset > -1 Then
  49.       brFile.BaseStream.Position = Offset
  50.     End If
  51.  
  52.     ' read the string
  53.     ' note: the string is prefixed with the length, encoded as an integer 7 bits at a time
  54.     Return brFile.ReadString()
  55.   End Function
  56.   Public Function ReadNullTerminatedString(Optional ByVal Offset As Long = -1) As String
  57.     Dim bByte As Char, sString As String = ""
  58.  
  59.     ' change offset
  60.     If Offset > -1 Then
  61.       brFile.BaseStream.Position = Offset
  62.     End If
  63.  
  64.     ' read first byte
  65.     bByte = brFile.ReadChar()
  66.     ' loop until it founds null char
  67.     While Asc(bByte) > 0
  68.       sString &= bByte
  69.       bByte = brFile.ReadChar
  70.     End While
  71.  
  72.     ' return the string readed
  73.     Return sString
  74.   End Function
  75.   Public Function ReadNullTerminatedUnicodeString(Optional ByVal Offset As Long = -1) As String
  76.     Dim sString As String = ""
  77.  
  78.     'change offset
  79.     If Offset > -1 Then
  80.       brFile.BaseStream.Position = Offset
  81.     End If
  82.  
  83.     ' read first byte
  84.     Dim bByte As Char = brFile.ReadChar()
  85.     ' loop until it founds null char
  86.     While Asc(bByte) > 0
  87.       sString &= bByte
  88.       brFile.BaseStream.Seek(1, IO.SeekOrigin.Current)
  89.       bByte = brFile.ReadChar
  90.     End While
  91.  
  92.     ' return the string readed
  93.     Return sString
  94.   End Function
  95.   Public Function ReadStructure(ByRef Struct As Object, Optional ByVal Offset As Long = -1) As Integer
  96.     ' get structure size
  97.     Dim iStructLen As Integer = Runtime.InteropServices.Marshal.SizeOf(Struct) - 4
  98.  
  99.     ' change file offset
  100.     If Offset > -1 Then
  101.       brFile.BaseStream.Position = Offset
  102.     End If
  103.     ' read our byte array from file
  104.     Dim StructData() As Byte = brFile.ReadBytes(iStructLen)
  105.  
  106.     ' allocate buffer
  107.     Dim DataBuffer As IntPtr = Runtime.InteropServices.Marshal.AllocHGlobal(iStructLen)
  108.     ' copy our byte array to buffer
  109.     Runtime.InteropServices.Marshal.Copy(StructData, 0, DataBuffer, iStructLen)
  110.     ' change our structure pointer to buffer
  111.     Struct = Runtime.InteropServices.Marshal.PtrToStructure(DataBuffer, Struct.GetType)
  112.     ' free memory
  113.     Runtime.InteropServices.Marshal.FreeHGlobal(DataBuffer)
  114.  
  115.     ' return bytes read
  116.     Return iStructLen
  117.   End Function
  118.   Public Function ReadStructure(ByRef StructType As Type, Optional ByVal Offset As Long = -1) As Object
  119.     ' get structure size
  120.     Dim iStructLen As Integer = Runtime.InteropServices.Marshal.SizeOf(StructType) - 4
  121.  
  122.     ' change file offset
  123.     If Offset > -1 Then
  124.       brFile.BaseStream.Position = Offset
  125.     End If
  126.     ' read our byte array from file
  127.     Dim StructData() As Byte = brFile.ReadBytes(iStructLen)
  128.  
  129.     ' allocate buffer
  130.     Dim DataBuffer As IntPtr = Runtime.InteropServices.Marshal.AllocHGlobal(iStructLen)
  131.     ' copy our byte array to buffer
  132.     Runtime.InteropServices.Marshal.Copy(StructData, 0, DataBuffer, iStructLen)
  133.     ' get a pointer to our buffer and redirect our function pointer to there (casting it)
  134.     ReadStructure = Runtime.InteropServices.Marshal.PtrToStructure(DataBuffer, StructType)
  135.     ' free memory
  136.     Runtime.InteropServices.Marshal.FreeHGlobal(DataBuffer)
  137.   End Function
  138.  
  139.   Public Sub New(ByVal FilePath As String)
  140.     fsFile = New IO.FileStream(FilePath, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
  141.     brFile = New IO.BinaryReader(fsFile)
  142.   End Sub
  143.   Protected Overrides Sub Finalize()
  144.     brFile.Close()
  145.     MyBase.Finalize()
  146.   End Sub
  147. End Class
[/vbnet:1c8s7yjx]

BTW: This is VB.NET
_________________
One thing only I know, and that is that I know nothing. (Socrates)


Last edited by _aLfa_ on Sat Aug 07, 2004 1:43 pm; edited 4 times in total
Back to top
MrUnleaded
Site Admin


Joined: 21 Sep 2002
Posts: 385
Location: California

Posted: Sat Aug 07, 2004 3:36 am     Post subject:

Very Nice


I love OOP
_________________
-MrU
Back to top
_aLfa_
Site Admin


Joined: 21 Sep 2002
Posts: 233
Location: Aveiro, Portugal

Posted: Sat Aug 07, 2004 9:26 am     Post subject:

Class updated:
Added ReadBytes function to read an array of bytes from the file.
Added ReadNullTerminatedUnicodeString (same as ReaddNullTerminatedString, but it skips 1 byte at a time)
Added some comments on ReadString (the name may be confusing)

And don't forget, if you find any additions/bugs whatever, poke me
_________________
One thing only I know, and that is that I know nothing. (Socrates)
Back to top
_aLfa_
Site Admin


Joined: 21 Sep 2002
Posts: 233
Location: Aveiro, Portugal

Posted: Sat Aug 07, 2004 9:32 am     Post subject:

Class updated again:
I forgot to add the FileLength property
_________________
One thing only I know, and that is that I know nothing. (Socrates)
Back to top
_aLfa_
Site Admin


Joined: 21 Sep 2002
Posts: 233
Location: Aveiro, Portugal

Posted: Sat Aug 07, 2004 10:18 am     Post subject:

Bug Fix:
SizeOf operator on ReadStructure was returning wrong size for structure (for some weird reason it was returning the real value plus 4 bytes)
_________________
One thing only I know, and that is that I know nothing. (Socrates)
Back to top
_aLfa_
Site Admin


Joined: 21 Sep 2002
Posts: 233
Location: Aveiro, Portugal

Posted: Sat Aug 07, 2004 1:45 pm     Post subject:

Added another ReadStructure (now you can use this one even with Option Strict On)

Examples:
Public DOSHeader As IMAGE_DOS_HEADER

(Option Strict Off)
StructLength = File.ReadStructure(DOSHeader)
-or-
DOSHeader = File.ReadStructure(DOSHeader.GetType)

(Option Strict On)
DOSHeader = CType(File.ReadStructure(DOSHeader.GetType), IMAGE_DOS_HEADER)

P.S.: If you don't understand something, feel free to ask
_________________
One thing only I know, and that is that I know nothing. (Socrates)


Last edited by _aLfa_ on Sat Aug 07, 2004 1:49 pm; edited 1 time in total
Back to top
vbgamer45
Regular user


Joined: 07 Jul 2004
Posts: 93
Location: 127.0.0.1

Posted: Sat Aug 07, 2004 1:47 pm     Post subject:

Good Job keep up the good work
Back to top
_aLfa_
Site Admin


Joined: 21 Sep 2002
Posts: 233
Location: Aveiro, Portugal

Posted: Sat Aug 07, 2004 1:50 pm     Post subject:

Just edited the post
_________________
One thing only I know, and that is that I know nothing. (Socrates)
Back to top
MrUnleaded
Site Admin


Joined: 21 Sep 2002
Posts: 385
Location: California

Posted: Sat Aug 07, 2004 4:29 pm     Post subject:

[="_aLfa_":3w5aoyk5]Class updated:
Added ReadNullTerminatedUnicodeString (same as ReaddNullTerminatedString, but it skips 1 byte at a time)
[/:3w5aoyk5]


Technically....a unicode string is not every other byte....Unicode is comprised of 16 bit characters(you probably know)

there are some Unicode to Ascii conversion stuff in a .net library.


I think it is the System.Text.Encoding Namespace.

And here is a link on proper byte to unicode conversion

http://msdn.microsoft.com/library/defau ... topic1.asp
_________________
-MrU
Back to top
MrUnleaded
Site Admin


Joined: 21 Sep 2002
Posts: 385
Location: California

Posted: Thu Aug 12, 2004 5:38 pm     Post subject:

Hey i Updated your class to support writing to files as well as Unsigned Int/long

[vbnet:9hwh66el]
  1. Public Class cFile
  2.     Private fsFile As IO.FileStream
  3.     Private brFile As IO.BinaryReader
  4.     Private bwFile As IO.BinaryWriter
  5.  
  6.     Public ReadOnly Property Length() As Long
  7.         Get
  8.             Return brFile.BaseStream.Length
  9.         End Get
  10.     End Property
  11.     Public Property Offset() As Long
  12.         Get
  13.             Return brFile.BaseStream.Position
  14.         End Get
  15.         Set(ByVal newValue As Long)
  16.             brFile.BaseStream.Position = newValue
  17.             bwFile.BaseStream.Position = newValue
  18.         End Set
  19.     End Property
  20.  
  21.    
  22.     Public Function WriteByte(ByVal Value As Byte, Optional ByVal Offset As Long = -1) As Boolean
  23.         Try
  24.             If Offset > -1 Then
  25.                 bwFile.BaseStream.Position = Offset
  26.             End If
  27.             bwFile.Write(Value)
  28.             Return True
  29.         Catch
  30.             Return False
  31.         End Try
  32.     End Function
  33.     Public Function WriteBytes(ByVal Value As Byte(), Optional ByVal Offset As Long = -1) As Boolean
  34.         Try
  35.             If Offset > -1 Then
  36.                 bwFile.BaseStream.Position = Offset
  37.  
  38.             End If
  39.             bwFile.Write(Value)
  40.             ' Me.Offset += Value.Length
  41.             Return True
  42.         Catch
  43.             Return False
  44.         End Try
  45.     End Function
  46.     Public Function WriteInt16(ByVal Value As Int16, Optional ByVal Offset As Long = -1) As Boolean
  47.         Try
  48.             If Offset > -1 Then
  49.                 bwFile.BaseStream.Position = Offset
  50.  
  51.             End If
  52.             bwFile.Write(Value)
  53.             'Me.Offset += 2
  54.             Return True
  55.         Catch
  56.             Return False
  57.         End Try
  58.     End Function
  59.     Public Function WriteInt32(ByVal Value As Int32, Optional ByVal Offset As Long = -1) As Boolean
  60.         Try
  61.             If Offset > -1 Then
  62.                 bwFile.BaseStream.Position = Offset
  63.             End If
  64.             bwFile.Write(Value)
  65.             'Me.Offset += 4
  66.             Return True
  67.         Catch
  68.             Return False
  69.         End Try
  70.     End Function
  71.     Public Function WriteUInt16(ByVal Value As UInt16, Optional ByVal Offset As Long = -1) As Boolean
  72.         Try
  73.             If Offset > -1 Then
  74.                 bwFile.BaseStream.Position = Offset
  75.  
  76.             End If
  77.             bwFile.Write(Value)
  78.             'Me.Offset += 2
  79.             Return True
  80.         Catch
  81.             Return False
  82.         End Try
  83.     End Function
  84.     Public Function WriteUInt32(ByVal Value As UInt32, Optional ByVal Offset As Long = -1) As Boolean
  85.         Try
  86.             If Offset > -1 Then
  87.                 bwFile.BaseStream.Position = Offset
  88.             End If
  89.             bwFile.Write(Value)
  90.             'Me.Offset += 4
  91.             Return True
  92.         Catch
  93.             Return False
  94.         End Try
  95.     End Function
  96.     Public Function WriteString(ByVal Value As String, Optional ByVal Offset As Long = -1) As Boolean
  97.         Try
  98.             If Offset > -1 Then
  99.                 bwFile.BaseStream.Position = Offset
  100.                 bwFile.Write(Value)
  101.             End If
  102.             Return True
  103.         Catch
  104.             Return False
  105.         End Try
  106.     End Function
  107.  
  108.     Public Function ReadByte(Optional ByVal Offset As Long = -1) As Byte
  109.         If Offset > -1 Then
  110.             brFile.BaseStream.Position = Offset
  111.         End If
  112.         Return brFile.ReadByte
  113.     End Function
  114.     Public Function ReadBytes(ByVal Count As Integer, Optional ByVal Offset As Long = -1) As Byte()
  115.         If Offset > -1 Then
  116.             brFile.BaseStream.Position = Offset
  117.         End If
  118.         Return brFile.ReadBytes(Count)
  119.     End Function
  120.     Public Function ReadInt16(Optional ByVal Offset As Long = -1) As Short
  121.         If Offset > -1 Then
  122.             brFile.BaseStream.Position = Offset
  123.         End If
  124.         Return brFile.ReadInt16
  125.     End Function
  126.     Public Function ReadInt32(Optional ByVal Offset As Long = -1) As Integer
  127.         If Offset > -1 Then
  128.             brFile.BaseStream.Position = Offset
  129.         End If
  130.         Return brFile.ReadInt32
  131.     End Function
  132.     Public Function ReadUInt16(Optional ByVal Offset As Long = -1) As UShort
  133.         If Offset > -1 Then
  134.             brFile.BaseStream.Position = Offset
  135.         End If
  136.         Return brFile.ReadUInt16
  137.     End Function
  138.     Public Function ReadUInt32(Optional ByVal Offset As Long = -1) As UInteger
  139.         If Offset > -1 Then
  140.             brFile.BaseStream.Position = Offset
  141.         End If
  142.         Return brFile.ReadUInt32
  143.     End Function
  144.     Public Function ReadString(Optional ByVal Offset As Long = -1) As String
  145.         ' change offset
  146.         If Offset > -1 Then
  147.             brFile.BaseStream.Position = Offset
  148.         End If
  149.  
  150.         ' read the string
  151.         ' note: the string is prefixed with the length, encoded as an integer 7 bits at a time
  152.         Return brFile.ReadString()
  153.     End Function
  154.     Public Function ReadNullTerminatedString(Optional ByVal Offset As Long = -1) As String
  155.         Dim bByte As Char, sString As String = ""
  156.  
  157.         ' change offset
  158.         If Offset > -1 Then
  159.             brFile.BaseStream.Position = Offset
  160.         End If
  161.  
  162.         ' read first byte
  163.         bByte = brFile.ReadChar()
  164.         ' loop until it founds null char
  165.         While Asc(bByte) > 0
  166.             sString &= bByte
  167.             bByte = brFile.ReadChar
  168.         End While
  169.  
  170.         ' return the string readed
  171.         Return sString
  172.     End Function
  173.     Public Function ReadNullTerminatedUnicodeString(Optional ByVal Offset As Long = -1) As String
  174.         Dim sString As String = ""
  175.  
  176.         'change offset
  177.         If Offset > -1 Then
  178.             brFile.BaseStream.Position = Offset
  179.         End If
  180.  
  181.         ' read first byte
  182.         Dim bByte As Char = brFile.ReadChar()
  183.         ' loop until it founds null char
  184.         While Asc(bByte) > 0
  185.             sString &= bByte
  186.             brFile.BaseStream.Seek(1, IO.SeekOrigin.Current)
  187.             bByte = brFile.ReadChar
  188.         End While
  189.  
  190.         ' return the string readed
  191.         Return sString
  192.     End Function
  193.     Public Function ReadStructure(ByRef Struct As Object, Optional ByVal Offset As Long = -1) As Integer
  194.         ' get structure size
  195.         Dim iStructLen As Integer = Runtime.InteropServices.Marshal.SizeOf(Struct) - 4
  196.  
  197.         ' change file offset
  198.         If Offset > -1 Then
  199.             brFile.BaseStream.Position = Offset
  200.         End If
  201.         ' read our byte array from file
  202.         Dim StructData() As Byte = brFile.ReadBytes(iStructLen)
  203.  
  204.         ' allocate buffer
  205.         Dim DataBuffer As IntPtr = Runtime.InteropServices.Marshal.AllocHGlobal(iStructLen)
  206.         ' copy our byte array to buffer
  207.         Runtime.InteropServices.Marshal.Copy(StructData, 0, DataBuffer, iStructLen)
  208.         ' change our structure pointer to buffer
  209.         Struct = Runtime.InteropServices.Marshal.PtrToStructure(DataBuffer, Struct.GetType)
  210.         ' free memory
  211.         Runtime.InteropServices.Marshal.FreeHGlobal(DataBuffer)
  212.  
  213.         ' return bytes read
  214.         Return iStructLen
  215.     End Function
  216.     Public Function ReadStructure(ByRef StructType As Type, Optional ByVal Offset As Long = -1) As Object
  217.         ' get structure size
  218.         Dim iStructLen As Integer = Runtime.InteropServices.Marshal.SizeOf(StructType) - 4
  219.  
  220.         ' change file offset
  221.         If Offset > -1 Then
  222.             brFile.BaseStream.Position = Offset
  223.         End If
  224.         ' read our byte array from file
  225.         Dim StructData() As Byte = brFile.ReadBytes(iStructLen)
  226.  
  227.         ' allocate buffer
  228.         Dim DataBuffer As IntPtr = Runtime.InteropServices.Marshal.AllocHGlobal(iStructLen)
  229.         ' copy our byte array to buffer
  230.         Runtime.InteropServices.Marshal.Copy(StructData, 0, DataBuffer, iStructLen)
  231.         ' get a pointer to our buffer and redirect our function pointer to there (casting it)
  232.         ReadStructure = Runtime.InteropServices.Marshal.PtrToStructure(DataBuffer, StructType)
  233.         ' free memory
  234.         Runtime.InteropServices.Marshal.FreeHGlobal(DataBuffer)
  235.     End Function
  236.  
  237.     Public Sub New(ByVal FilePath As String)
  238.         fsFile = New IO.FileStream(FilePath, IO.FileMode.Open, IO.FileAccess.ReadWrite, IO.FileShare.ReadWrite)
  239.         brFile = New IO.BinaryReader(fsFile)
  240.         bwFile = New IO.BinaryWriter(fsFile)
  241.     End Sub
  242.  
  243.     Protected Overrides Sub Finalize()
  244.         brFile.Close()
  245.         MyBase.Finalize()
  246.     End Sub
  247.  
  248. End Class
[/vbnet:9hwh66el]
_________________
-MrU
Back to top
_aLfa_
Site Admin


Joined: 21 Sep 2002
Posts: 233
Location: Aveiro, Portugal

Posted: Thu Aug 12, 2004 8:04 pm     Post subject:

Why do you pretend writing to the file?
(in this decompiling context)
_________________
One thing only I know, and that is that I know nothing. (Socrates)
Back to top
MrUnleaded
Site Admin


Joined: 21 Sep 2002
Posts: 385
Location: California

Posted: Thu Aug 12, 2004 8:24 pm     Post subject:

well....some values i the exe could potentially be hexed to the value you want...

say a forms width is too small... just update that value in the file.

in theory at least?

But also...i was testing some stuff...and i needed to be able to write binary data....so yea. file dumps....etc.
_________________
-MrU
Back to top
MrUnleaded
Site Admin


Joined: 21 Sep 2002
Posts: 385
Location: California

Posted: Tue Aug 24, 2004 3:49 am     Post subject: to 4 or not to 4

[="_aLfa_":328qwttp]Bug Fix:
SizeOf operator on ReadStructure was returning wrong size for structure (for some weird reason it was returning the real value plus 4 bytes)[/:328qwttp]

Thats weird because it is returning the correct value for me. ie 64 bytes for IMAGE_DOS_HEADER.....it was messing me up for a while....because it was getting me weird values for e_lfanew since it was reading from some random area in memory....

anyways i changed my copy:
Code:

Public Function ReadStructure(ByRef StructType As Type, Optional ByVal Offset As Long = -1) As Object
' get structure size

Dim iStructLen As Integer = Runtime.InteropServices.Marshal.SizeOf(StructType) - 4



to

Code:

Public Function ReadStructure(ByRef StructType As Type, Optional ByVal Offset As Long = -1) As Object
' get structure size

Dim iStructLen As Integer = Runtime.InteropServices.Marshal.SizeOf(StructType)


_________________
-MrU
Back to top
napalm
Site Admin


Joined: 11 Dec 2003
Posts: 18
Location: UK

Posted: Mon Sep 20, 2004 3:01 am     Post subject:

Hello again all.. Just a note that i had problems with.. some compilers can align structures to word or dwords this could account for the 4 bytes.

I dont program .NET so I have no idea if there are any packing attributes to structures. But its worth a look, to sort that problem.

This would also explain why its different for MrU and for _aLfa_ as on some compilers this attribute can be set in the IDEs options.

Nice to see the forums still alive.. and im getting back into the scene.

Nice to see you all again (MrU, Moogman, _aLfa_, and the rest)

Speak soon..
_________________
~Napalm~

"With insomnia, you're never really asleep;
you're never really awake."
Fight Club - 1999 - [url:hdzph3dx]http://www.imdb.com/title/tt0137523/[/url:hdzph3dx]
Back to top
MrUnleaded
Site Admin


Joined: 21 Sep 2002
Posts: 385
Location: California

Posted: Mon Sep 20, 2004 3:10 pm     Post subject:

you signed up for the message board on my birthday
_________________
-MrU
Back to top
   VB Decompiler Forum Index -> Utilities & User Contributions All times are GMT
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group