Jun 20, 2014 03:11 PM
(This post was last modified: Jun 20, 2014 11:45 PM by LordCoder.)
Hello,
As I see some people are confused creating a builder for stub files I decided to create a complete tutorial.
If you don't know what does builder or stub mean let me show you an example.
You have an application that shows a message. That message can be anything. The user who creates the application needs to set a custom message as he's trying to do, for example, an application saying "Hello".
There are 3 ways (I think there aren't any other). The first one is using Mono.Cecil, an open source library to edit already compiled assemblies for .NET. It will edit us whatever we want inside the application using MSIL. The problem is to know how to use it (so this method could be more advanced if you are not into reversing).
The second method is using a part from the compiled .EXE and then get it. The options should be inside the application and you can't change the code. That's useful if you're going to make similar stubs and not very customized.
The last method is using a stub code and edit it as we want. Then compile it using some classes from .NET. This might be a good method if you're planning to do an easy application without many classes or references (as you will have it on a .txt file basically).
1st way: Mono.Cecil
Download Mono.Cecil.dll (already compiled for you).
I'm not going to show the basic of Mono.Cecil as there are a lot of tutorials about Mono.Cecil (or remember Google is your friend always).
Creating a basic "MsgBox Creator" application
-Create 2 projects: Builder and Stub.
-Add Mono.Cecil to your project.
On the Stub you should use, for example, a ConsoleApplication. On MainModule1 put:
Now save it to "YourBuilderProject\bin\debug\stub_compiled.exe" and add this to the Builder:
Finally run "Builder" and call "Compile" sub with any message. It will save "stub_edited.exe" with your new message.
2nd way: Editing options from an EXE
This method doesn't require any extra library. I know my code is very old and I can use IO functions but I like it. I'm not going to make this thread more long to explain how does it work, it just works.
Creating a basic "MsgBox Creator" application
As we did with the Mono.Cecil example, create both projects "Stub" and "Builder". The stub can be a ConsoleApplication like before with this method:
Compile it and do the same (copy the file to bin\debug as "stub_compiled.exe"). Then add this code:
Remember that "WHATEVERHEREWITHOUTSPACES" should be the same for builder and stub and without spaces.
3rd way: Compiling a new assembly
You'll need System.Reflection namespaces so add them with Imports.
Creating a basic "MsgBox Creator" application
The trick to do this is editing the stub but as a text file. So let's say we have this:
We'll have to edit the stub to:
And then compile it:
That's all! I didn't explain everything step by step as I suppose you know about VB.NET.
Any comments are welcomed (I expect the bad ones too, please be bad with me because I can learn from those comments :D).
As I see some people are confused creating a builder for stub files I decided to create a complete tutorial.
If you don't know what does builder or stub mean let me show you an example.
You have an application that shows a message. That message can be anything. The user who creates the application needs to set a custom message as he's trying to do, for example, an application saying "Hello".
There are 3 ways (I think there aren't any other). The first one is using Mono.Cecil, an open source library to edit already compiled assemblies for .NET. It will edit us whatever we want inside the application using MSIL. The problem is to know how to use it (so this method could be more advanced if you are not into reversing).
The second method is using a part from the compiled .EXE and then get it. The options should be inside the application and you can't change the code. That's useful if you're going to make similar stubs and not very customized.
The last method is using a stub code and edit it as we want. Then compile it using some classes from .NET. This might be a good method if you're planning to do an easy application without many classes or references (as you will have it on a .txt file basically).
1st way: Mono.Cecil
Download Mono.Cecil.dll (already compiled for you).
I'm not going to show the basic of Mono.Cecil as there are a lot of tutorials about Mono.Cecil (or remember Google is your friend always).
Creating a basic "MsgBox Creator" application
-Create 2 projects: Builder and Stub.
-Add Mono.Cecil to your project.
On the Stub you should use, for example, a ConsoleApplication. On MainModule1 put:
Code
Function GetMessage() as String
Return "Not Set - Error" ' Builder will do the job
End Function
Sub Main()
Dim msg as String = GetMessage
If msg = "Not Set - Error" Then ' Checks if Builder did it
MsgBox("No message was set. Closing now.")
Else
MsgBox(msg)
End If
End SubNow save it to "YourBuilderProject\bin\debug\stub_compiled.exe" and add this to the Builder:
Code
Sub Compile(ByVal message As String)
Dim m As AssemblyDefinition = AssemblyDefinition.ReadAssembly(Application.StartupPath & "\stub_compiled.exe") ' Opens our stub
For Each i In m.MainModule.Types
If i.Name = "MainModule1" Then ' Finds the MainModule of the ConsoleApplication
For Each Me2 In i.Methods
If Me2.Name = "GetMessage" Then ' Finds the GetMessage function
Me2.Body.Instructions(1).Operand = message ' I put 1 instead of 0 because compilers should put always a 'Nop' instruction before the code
End If
Next
End If
Next
m.Write(Application.StartupPath & "\stub_edited.exe") ' Saves the assembly
End SubFinally run "Builder" and call "Compile" sub with any message. It will save "stub_edited.exe" with your new message.
2nd way: Editing options from an EXE
This method doesn't require any extra library. I know my code is very old and I can use IO functions but I like it. I'm not going to make this thread more long to explain how does it work, it just works.
Creating a basic "MsgBox Creator" application
As we did with the Mono.Cecil example, create both projects "Stub" and "Builder". The stub can be a ConsoleApplication like before with this method:
Code
Sub Main()
Dim options as String()
Dim text1 as String
FileSystem.FileOpen(1, Application.ExecutablePath, OpenMode.Binary, OpenAccess.Read, OpenShare.Shared, -1) ' Opens the file and saves to a local variable 1.
Me.text1 = Strings.Space(CInt(FileSystem.LOF(1))) ' Gets the variable
FileSystem.FileGet(1, Me.text1, -1, False)
FileSystem.FileClose(1)
Try
Me.options = Strings.Split(Me.text1, "WHATEVERHEREWITHOUTSPACES", -1, CompareMethod.Binary)
Catch ex as Exception
Msgbox("Error. No message was set")
End
End Try
MsgBox(options(1))
End SubCompile it and do the same (copy the file to bin\debug as "stub_compiled.exe"). Then add this code:
Code
Sub Compile(message as String)
FileSystem.FileOpen(1, "stub_compiled.exe", OpenMode.Binary, OpenAccess.Read, OpenShare.Default, -1)
Dim str As String = Strings.Space(CInt(FileSystem.LOF(1)))
FileSystem.FileGet(1, str, -1, False)
FileSystem.FileClose(New Integer() { 1 })
FileSystem.FileOpen(1, Me.SaveFileDialog1.FileName, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.Default, -1)
FileSystem.FilePut(1, (str & "WHATEVERHEREWITHOUTSPACES" & "YOURMESSAGEHEREIFUWANT"), -1, False)
FileSystem.FileClose(New Integer() { 1 })
End SubRemember that "WHATEVERHEREWITHOUTSPACES" should be the same for builder and stub and without spaces.
3rd way: Compiling a new assembly
You'll need System.Reflection namespaces so add them with Imports.
Creating a basic "MsgBox Creator" application
The trick to do this is editing the stub but as a text file. So let's say we have this:
Code
Module MainModule1
Sub Main()
MsgBox("WHATTOSAY")
End Sub
End ModuleWe'll have to edit the stub to:
Code
mystubsaved.Replace("WHATTOSAY", "Hey this is the new message")And then compile it:
Code
Public Sub CompileCode(ByVal is_vb As Boolean, sourceFile As String, _
exeFile As String)
Dim provider
If Not is_vb Then
provider = New CSharpCodeProvider()
Else
provider = New VBCodeProvider()
End If
Dim cp As New CompilerParameters()
cp.ReferencedAssemblies.Add("System.dll")
' NOTE: If your stub has more references you need to add them
cp.GenerateExecutable = True
cp.OutputAssembly = exeFile
cp.GenerateInMemory = False
Dim cr As CompilerResults = provider.CompileAssemblyFromFile(cp, sourceFile)
End SubThat's all! I didn't explain everything step by step as I suppose you know about VB.NET.
Any comments are welcomed (I expect the bad ones too, please be bad with me because I can learn from those comments :D).



![[Image: 0I49JoS.png]](https://i.imgur.com/0I49JoS.png)
![[Image: C8Ssokx.png]](https://i.imgur.com/C8Ssokx.png)
![[Image: 2YkFHUe.png]](https://i.imgur.com/2YkFHUe.png)
![[Image: 1zL3pDG.jpg]](https://i.imgur.com/1zL3pDG.jpg)

![[Image: PVoDiqS.png]](https://i.imgur.com/PVoDiqS.png)