예제 #1
0
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            //try
            //{
            if (DebugForm != null)
            {
                MessageBox.Show("Already debugging", "ASM.net", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            TextboxHighlighting textbox = GetCurrentTextbox();

            if (textbox != null)
            {
                AssemblerCompiler compiler = Build(textbox.Text, false, false);
                if (compiler.errors.Count == 0)
                {
                    AssemblerExecute executor = new AssemblerExecute();
                    DebugForm = new frmEmulate(executor);
                    executor.Execute(compiler.AssemblerBytes.ToArray());
                    DebugForm.Show();
                }
            }

            /*}
             * catch(Exception ex)
             * {
             *  MessageBox.Show(ex.StackTrace + "\r\n" + ex.Message);
             * }*/
        }
예제 #2
0
 private void executeToolStripMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
         TextboxHighlighting textbox = GetCurrentTextbox();
         if (textbox != null)
         {
             AssemblerCompiler compiler = Build(textbox.Text, true, true);
         }
     }
     catch { }
 }
예제 #3
0
        public AssemblerCompiler Build(string SourceCode, bool save, bool execute)
        {
            Stopwatch sw = Stopwatch.StartNew();

            textBox1.Text = "--------- Build started at " + DateTime.Now + "---------\r\n";
            AssemblerCompiler compiler = new AssemblerCompiler(SourceCode);

            compiler.Build();

            if (compiler.errors.Count > 0)
            {
                textBox1.Text += "Build unsuccessful, " + compiler.errors.Count + " errors\r\n";
                for (int i = 0; i < compiler.errors.Count; i++)
                {
                    textBox1.Text += "line:" + compiler.errors[i].line + ", " + compiler.errors[i].Details + "\r\n";
                }
                return(compiler);
            }

            sw.Stop();

            if (sw.Elapsed.Seconds == 0)
            {
                textBox1.Text += "Build complete, It took " + sw.ElapsedMilliseconds + " Milliseconds\r\n";
            }
            else
            {
                textBox1.Text += "Build complete, It took " + sw.Elapsed.Seconds + ":" + sw.ElapsedMilliseconds + " second(s)\r\n";
            }

            if (save)
            {
                using (SaveFileDialog dialog = new SaveFileDialog())
                {
                    dialog.AddExtension    = true;
                    dialog.CheckFileExists = false;
                    dialog.Filter          = "Executable|*.exe";
                    dialog.Title           = "Save the Assembler.net to a executable";

                    if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("using System;");
                        sb.AppendLine("using AsmEngine;");
                        sb.AppendLine("using System.Reflection;");
                        sb.AppendLine("using System.Diagnostics;");

                        if (Settings.MergeEngine && Settings.CompressEngine)
                        {
                            sb.AppendLine("using System.IO;");
                            sb.AppendLine("using System.IO.Compression;");
                        }

                        sb.AppendLine("namespace AsmNetConsoleCodedom");
                        sb.AppendLine("{");
                        sb.AppendLine("class Program");
                        sb.AppendLine("{");
                        sb.AppendLine("static void Main(string[] args)");
                        sb.AppendLine("{");

                        //bytes to execute
                        string bytes = BitConverter.ToString(compiler.AssemblerBytes.ToArray());
                        bytes = "0x" + bytes.Replace("-", ", 0x");

                        byte[] Engine = File.ReadAllBytes("AsmEngine.dll");
                        if (Settings.CompressEngine)
                        {
                            Engine = Compressor.Compress(Engine);
                        }

                        string EngineBytes = BitConverter.ToString(Engine);
                        EngineBytes = "0x" + EngineBytes.Replace("-", ", 0x");

                        if (Settings.MergeEngine)
                        {
                            if (Settings.CompressEngine)
                            {
                                sb.AppendLine("byte[] engine = Decompress(new byte[] { " + EngineBytes + " });");
                            }
                            else
                            {
                                sb.AppendLine("byte[] engine = new byte[] { " + EngineBytes + " };");
                            }

                            sb.AppendLine("Assembly asm = Assembly.Load(engine);");
                            sb.AppendLine("Type executor = asm.GetType(\"AsmEngine.AssemblerExecute\");");
                            sb.AppendLine("Object initialized = Activator.CreateInstance(executor);");
                            sb.AppendLine("byte[] arrayzor = new byte[] { " + bytes + " };");
                            sb.AppendLine("executor.InvokeMember(\"Execute\", BindingFlags.Default | BindingFlags.InvokeMethod, null, initialized, new object[] { arrayzor });");
                            sb.AppendLine("PropertyInfo property = executor.GetProperty(\"HALT\");");
                            sb.AppendLine("bool isHalt = false;");
                            sb.AppendLine("do");
                            sb.AppendLine("{");
                            sb.AppendLine("    isHalt = Convert.ToBoolean(property.GetValue(initialized, null));");
                            sb.AppendLine("    if (!isHalt)");
                            sb.AppendLine("        executor.InvokeMember(\"NextStep\", BindingFlags.Default | BindingFlags.InvokeMethod, null, initialized, new object[] { });");
                            sb.AppendLine("}");
                            sb.AppendLine("while (!isHalt);");
                        }
                        else
                        {
                            sb.AppendLine("AssemblerExecute a = new AssemblerExecute();");
                            sb.AppendLine("a.Execute(new byte[]{" + bytes + "});");
                            sb.AppendLine("while(!a.HALT){ a.NextStep();}");
                        }

                        #region "Dynamic calls"

                        /*
                         * //WORKING DYNAMIC CALLS
                         * //load the AsmEngine
                         * Assembly asm = Assembly.Load(File.ReadAllBytes("AsmEngine.dll"));
                         * Type executor = asm.GetType("AsmEngine.AssemblerExecute");
                         * //Call constructor
                         * Object initialized = Activator.CreateInstance(executor);
                         * //Call Execute function
                         * executor.InvokeMember("Execute", BindingFlags.Default | BindingFlags.InvokeMethod, null, initialized, new object[] { compiler.AssemblerBytes.ToArray() });
                         *
                         * PropertyInfo property = executor.GetProperty("HALT");
                         * bool isHalt = false;
                         * do
                         * {
                         *  isHalt = Convert.ToBoolean(property.GetValue(initialized, null));
                         *  if(!isHalt)
                         *      executor.InvokeMember("NextStep", BindingFlags.Default | BindingFlags.InvokeMethod, null, initialized, new object[] { });
                         * }
                         * while (!isHalt);*/
                        #endregion

                        //sb.AppendLine("System.Diagnostics.Process.GetCurrentProcess().WaitForExit();");
                        sb.AppendLine("}");

                        if (Settings.MergeEngine && Settings.CompressEngine)
                        {
                            sb.AppendLine("public static byte[] Decompress(byte[] gzBuffer)");
                            sb.AppendLine("{");
                            sb.AppendLine("    MemoryStream ms = new MemoryStream();");
                            sb.AppendLine("    int msgLength = BitConverter.ToInt32(gzBuffer, 0);");
                            sb.AppendLine("    ms.Write(gzBuffer, 4, gzBuffer.Length - 4);");
                            sb.AppendLine("    byte[] buffer = new byte[msgLength];");
                            sb.AppendLine("    ms.Position = 0;");
                            sb.AppendLine("    GZipStream zip = new GZipStream(ms, CompressionMode.Decompress);");
                            sb.AppendLine("    zip.Read(buffer, 0, buffer.Length);");
                            sb.AppendLine("    return buffer;");
                            sb.AppendLine("}");
                        }

                        sb.AppendLine("}");
                        sb.AppendLine("}");


                        CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");

                        System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
                        parameters.ReferencedAssemblies.Add(Environment.CurrentDirectory + "\\AsmEngine.dll");
                        parameters.ReferencedAssemblies.Add("System.dll");
                        parameters.CompilerOptions = "/unsafe /optimize";
                        if (Settings.buildOutput == BuildOutput.WindowsApp)
                        {
                            parameters.CompilerOptions += " /t:winexe";
                        }

                        //Make sure we generate an EXE, not a DLL
                        parameters.GenerateExecutable = true;
                        parameters.OutputAssembly     = dialog.FileName;

                        try
                        {
                            if (File.Exists(dialog.FileName))
                            {
                                File.Delete(dialog.FileName);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                            return(compiler);
                        }

                        CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, sb.ToString());

                        /*
                         * FileStream filestream = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Write);
                         * filestream.Seek(0xF4, SeekOrigin.Begin);
                         * filestream.WriteByte(11);
                         * filestream.Flush();
                         * filestream.Close();*/
                        textBox1.Text += "Saved file at " + dialog.FileName + "\r\n";

                        if (Settings.Obfuscate)
                        {
                            textBox1.Text += "NetFuscator - Obfuscating assembly\r\n";
                            NetFuscator obfuscator = new NetFuscator();
                            obfuscator.Obfuscate(dialog.FileName);
                        }

                        if (execute)
                        {
                            Process.Start(dialog.FileName);
                        }
                    }
                }
            }
            return(compiler);
        }
예제 #4
0
        public override AssemblerCompiler GetAssociatedCompiler()
        {
            AssemblerCompiler compiler = new AssemblerCompiler(this);

            compiler.LoadInstructionFromSet();

            #region Basic arithmetic operations
            compiler.SetInstruction("add", 0x1, 2, AssemblerParameters.Register, AssemblerParameters.Register);
            compiler.SetInstruction("add", 0x2, 2, AssemblerParameters.Register, AssemblerParameters.Value);
            compiler.SetInstruction("mov", 0x3, 2, AssemblerParameters.Register, AssemblerParameters.Register);
            compiler.SetInstruction("sub", 0x4, 2, AssemblerParameters.Register, AssemblerParameters.Register);
            compiler.SetInstruction("sub", 0x5, 2, AssemblerParameters.Register, AssemblerParameters.Value);
            compiler.SetInstruction("mov", 0x6, 2, AssemblerParameters.Register, AssemblerParameters.Value);
            compiler.SetInstruction("mul", 0x7, 2, AssemblerParameters.Register, AssemblerParameters.Register);
            compiler.SetInstruction("mul", 0x8, 2, AssemblerParameters.Register, AssemblerParameters.Value);
            compiler.SetInstruction("div", 0x9, 2, AssemblerParameters.Register, AssemblerParameters.Register);
            compiler.SetInstruction("div", 0xA, 2, AssemblerParameters.Register, AssemblerParameters.Value);
            compiler.SetInstruction("mod", 0x27, 2, AssemblerParameters.Register, AssemblerParameters.Value);
            compiler.SetInstruction("mod", 0x28, 2, AssemblerParameters.Register, AssemblerParameters.Register);
            compiler.SetInstruction("inc", 0x29, 1, AssemblerParameters.Register);
            compiler.SetInstruction("dec", 0x2A, 1, AssemblerParameters.Register);
            #endregion

            #region Bit operations
            compiler.SetInstruction("or", 0xB, 2, AssemblerParameters.Register, AssemblerParameters.Register);
            compiler.SetInstruction("or", 0xC, 2, AssemblerParameters.Register, AssemblerParameters.Value);
            compiler.SetInstruction("and", 0xE, 2, AssemblerParameters.Register, AssemblerParameters.Register);
            compiler.SetInstruction("and", 0xF, 2, AssemblerParameters.Register, AssemblerParameters.Value);
            compiler.SetInstruction("xor", 0x10, 2, AssemblerParameters.Register, AssemblerParameters.Register);
            compiler.SetInstruction("xor", 0x11, 2, AssemblerParameters.Register, AssemblerParameters.Value);
            compiler.SetInstruction("not", 0x12, 2, AssemblerParameters.Register);
            compiler.SetInstruction("shr", 0x13, 2, AssemblerParameters.Register, AssemblerParameters.Register);
            compiler.SetInstruction("shl", 0x14, 2, AssemblerParameters.Register, AssemblerParameters.Register);
            compiler.SetInstruction("shr", 0x15, 2, AssemblerParameters.Register, AssemblerParameters.Value);
            compiler.SetInstruction("shl", 0x16, 2, AssemblerParameters.Register, AssemblerParameters.Value);
            #endregion

            #region Basic jumps
            compiler.SetInstruction("br", 0x17, 1, AssemblerParameters.Address);
            compiler.SetInstruction("jmp", 0x18, 1, AssemblerParameters.Address);
            #endregion

            #region Stack operations
            compiler.SetInstruction("push", 0x19, 2, AssemblerParameters.Register);
            compiler.SetInstruction("pop", 0x1A, 2, AssemblerParameters.Register);
            #endregion

            #region Conditional jumps, conditions
            compiler.SetInstruction("cmp", 0x1B, 2, AssemblerParameters.Register, AssemblerParameters.Value);
            compiler.SetInstruction("je", 0x1C, 1, AssemblerParameters.Address);
            compiler.SetInstruction("jz", 0x1D, 1, AssemblerParameters.Address);
            compiler.SetInstruction("jne", 0x1E, 1, AssemblerParameters.Address);
            compiler.SetInstruction("jnz", 0x1F, 1, AssemblerParameters.Address);
            compiler.SetInstruction("jgr", 0x20, 1, AssemblerParameters.Address);
            compiler.SetInstruction("jlo", 0x21, 1, AssemblerParameters.Address);
            compiler.SetInstruction("jeg", 0x22, 1, AssemblerParameters.Address);
            compiler.SetInstruction("jel", 0x23, 1, AssemblerParameters.Address);
            #endregion

            #region Additional operations
            compiler.SetInstruction("lidt", 0xD, 1, AssemblerParameters.Register);
            compiler.SetInstruction("call", 0x24, 1, AssemblerParameters.Address);
            compiler.SetInstruction("ret", 0x25, 1, AssemblerParameters.Value);
            compiler.SetInstruction("halt", 0x26, 0);
            compiler.SetInstruction("int", 0x31, 1, AssemblerParameters.Register);
            #endregion

            #region Memory operations
            //compiler.SetInstruction("mov", 0x2C, 2,  AssemblerParameters.Register, AssemblerParameters.Pointer);
            //compiler.SetInstruction("mov", 0x2D, 2,  AssemblerParameters.Pointer, AssemblerParameters.Register);
            //compiler.SetInstruction("mov", 0x2E, 2,  AssemblerParameters.Pointer, AssemblerParameters.Value);
            #endregion

            #region I/O Operations
            compiler.SetInstruction("out", 0x32, 2, AssemblerParameters.Value, AssemblerParameters.Value);
            //compiler.SetInstruction("out", 0x33, 2, AssemblerParameters.Value, AssemblerParameters.Register);
            compiler.SetInstruction("in", 0x34, 2, AssemblerParameters.Value, AssemblerParameters.Register);
            #endregion

            #region Float-point operations
            compiler.SetInstruction("fadd", 0x84, 0);
            compiler.SetInstruction("fsub", 0x86, 0);
            compiler.SetInstruction("fmul", 0x88, 0);
            compiler.SetInstruction("fdiv", 0x8A, 0);
            compiler.SetInstruction("fcmp", 0x8E, 0);
            compiler.SetInstruction("fdst", 0x90, 0);
            #endregion

            for (byte i = 0; i < RegistersCount - 2; i++)
            {
                compiler.SetRegister("r" + (i + 1), i);
            }

            compiler.SetRegister("sp", StackPointerRegisterNumber);
            compiler.SetRegister("pc", ProgramCountRegisterNumber);

            for (byte i = RegistersCount; i < RegistersCount + 8; i++)
            {
                compiler.SetRegister("f" + (i - 10 + 1), i);
            }

            compiler.SetConstant("qword", 8);
            compiler.SetConstant("dword", 4);
            compiler.SetConstant("word", 2);
            compiler.SetConstant("byte", 1);

            return(compiler);
        }