Post: [VB.net Source] - Portable .NET Compiler and a "new" way to Write Settings to a "stub
05-10-2011, 12:03 AM #1
(adsbygoogle = window.adsbygoogle || []).push({});
Originally posted by kingdeath360 View Post
You must login or register to view this content.

Hey guys, I'm new to this site and I do see that there are already some tutorials on the usage of the CodeDom compiler class in .net; however I haven't seen any sources for a portable compiler yet, or at least not one that stresses the idea I'm trying to get across. So without further delay, let's get started Happy!

Alright so let's start off with a brand new Windows Application project.
At this time you should also be sure to add the following import to your application:
    Imports System.CodeDom.Compiler


You should also take this time to mess around with the GUI and make it look all pretty n' shit, but before you get carried away with that, add the following controls:

x2 Textbox (Textbox1.text & Textbox2.text)
x1 Button (Button1)

Next thing you'll want to do is declare a global variable (string) called 'CompilePath'.
    Dim CompilePath As String


Now we're going to make a simple SaveFileDialog function for our button1_click() event.
    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FileSave As New SaveFileDialog 'Awesome faceeclares a save file dialog so the user can specify where to save the compiled file.
With FileSave
.InitialDirectory = Application.StartupPath 'Sets initial directory to the startup path of the application.
.FileName = "*.exe"
.DefaultExt = ".exe|*.exe"
End With

If FileSave.ShowDialog = Windows.Forms.DialogResult.OK Then
CompilePath = FileSave.FileName
Else : Exit Sub 'If the user hits cancel/'X', we exit the sub.
End If
End Sub


Before we go any further; I want to give a brief explanation of what "CodeDom" really is, for those who don't know. CodeDOM is used to refer to a Code Document Object Model; now rather than me try to explain what the **** that means; let's read a snippet from Microsoft together.

So now that we have been educated; onward and upward!
I'm the kind of coder who uses WAY too many subroutines in an attempt to make my code look cleaner; bellow is an example of this. (This is part of the program, put it in your code window.)
    Sub CompileCode()
Dim TheCode As String = TextBox1.Text 'Stores the written code in the 'TheCode' variable.
CreateEXE(CompilePath, TheCode) 'Calls the function to Compile the code, with "CompilePath" as the location for the code to be compiled to, and "TheCode" as the code to be compiled.
End Sub


Now go back to were we made that nice Button1_click() event, and add a call to the subroutine we just created right after we set CompilePath to equal the location the user choose to save the file.
    
Call CompileCode()


Now comes the heavy duty shit; our CreateEXE subroutine Smile.
    
Sub CreateEXE(ByVal SaveLocation As String, ByVal Code As String)
On Error Resume Next 'To Avoid exceptions being thrown.

Dim Settings As New CompilerParameters() 'To store the settings of our compiler in.
Dim Compiler As ICodeCompiler = (New VBCodeProvider).CreateCompiler 'Awesome faceeclaring our compiler.

With Settings 'Setting some Settings for the compiler.
.GenerateExecutable = True
.OutputAssembly = SaveLocation 'Sets save location to the place the user inputed in the FileSave.sfd
.CompilerOptions = "/target:winexe" 'Tells the compiler to compile the code as a winexe file.
.ReferencedAssemblies.Add("System.dll") 'Imports System
End With

Code = Code.Replace("[*message*]", TextBox2.Text) 'Replaces the place holder for the message box's message with the message that was entered in textbox2.text
Compiler.CompileAssemblyFromSource(Settings, Code) 'Compiles the code with all the settings stored in the "Settings" string.
End Sub


Alright, now the code is half-commentated but there are a few things I want to briefly explain. First of all, if you look back at your CompileCode() sub, you'll notice that this subroutine is called in it as follows:
    
CreateEXE(CompilePath, TheCode)


For those who don't know, this sets "CompilePath" and "TheCode" as the variables filling in for "SaveLocation" and "Code" respectively.
    Sub CreateEXE(ByVal SaveLocation As String, ByVal Code As String)


Now the code is pretty straight forward for the CreateEXE "function"; it declares the variable Settings to store some basic compiling options, and the variable Compiler as the actual, well, compiler! All the code between "With Settings" and "End With" are just options we're using to tell the program how we want our .exe file to be compiled.

What I want to talk about a little bit more is this line of code:
     Code = Code.Replace("[*message*]", TextBox2.Text)


Now it may look innocent enough, but just think of this in a big picture way. You're taking the string value "[*message*]" that we see in textbox1 and replacing it with the contents you have of textbox2. Meaning that the compiled program will have whatever the hell textbox2.text had in it in its code, not "[*message*]". Now, you can use this to write any settings you want really. Let's say you're making a basic keylogger, and you need to write the SMTP information you're using to send logs to yourself to the server; well just make a few "variable marks" in the code like [*user*], [*pass*], etc. Then all you have to do is replace them with whatever the information is set to on compile-time. You can use this same idea to make a basic USG system as pr0t0typ3 explained in his tutorial.

This way, the settings aren't being written to the EOF or the Resources; they're being written right snap dab in the middle of the source code, which will avoid detections.
Anyway that about does it for this tutorial; if you have any questions feel free to ask them bellow or shoot me a PM. Also, bellow is a download link for an example project using these source codes.

Source Code Download: You must login or register to view this content.

source You must login or register to view this content.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo