Jump to content
Tuts 4 You

NET Native AOT


whoknows

Recommended Posts

C# implementation 

    {
        public Form1()
        {
            InitializeComponent();
        }

        #region TextBox DragDrop
        private void textBox1_DragDrop(object sender, DragEventArgs e)
        {
            string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);

            if (FileList[0].ToLower().EndsWith(".dll") || FileList[0].ToLower().EndsWith(".exe"))
                textBox1.Text = FileList[0];
        }

        private void textBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;

        }

        #endregion

        private void btn_Click(object sender, EventArgs e)
        {
            if (!File.Exists(textBox1.Text))
            {
                MessageBox.Show("File does not exist");
                return;
            }

            string rootDir = Path.GetDirectoryName(textBox1.Text);
            string rootDirExtracted = Path.Combine(rootDir, "_extracted");

            byte[] mz = new byte[] { 77, 90, 144, 0, 3 }; //4D5A900003

            List<byte[]> d = SplitByteArray(File.ReadAllBytes(textBox1.Text), mz);

            int j = 0;
            foreach (byte[] item in d)
            {
                if (item.Length == 0)
                    continue;

                string fl = string.Format("{0}\\part{1}.exe", rootDirExtracted, j.ToString());

                Directory.CreateDirectory(rootDirExtracted);

                //dump file
                File.WriteAllBytes(fl, mz.Concat(item).ToArray());

                //read original filename from PE properties
                string originalFileName = System.Diagnostics.FileVersionInfo.GetVersionInfo(fl).OriginalFilename;

                if (!string.IsNullOrEmpty(originalFileName))
                {
                    string fullPathOriginalFileName = Path.Combine(rootDirExtracted, originalFileName);

                    if (File.Exists(fullPathOriginalFileName))
                        fullPathOriginalFileName = Path.Combine(rootDirExtracted, string.Format("{0}_{1}{2}", Path.GetFileNameWithoutExtension(originalFileName), j, Path.GetExtension(originalFileName)));

                    File.Move(fl, fullPathOriginalFileName);
                }
                
                j++;
            }
        }

        ///

        private static List<byte[]> SplitByteArray(byte[] source, byte[] split)
        {
            List<byte[]> segments = new List<byte[]>();
            int startIndex = 0;

            while (startIndex < source.Length)
            {
                int foundIndex = FindSplitIndex(source, startIndex, split);
                if (foundIndex == -1)
                {
                    // If the split sequence is not found, add the remaining bytes.
                    foundIndex = source.Length;
                }

                int length = foundIndex - startIndex;
                byte[] segment = new byte[length];
                Buffer.BlockCopy(source, startIndex, segment, 0, length);
                segments.Add(segment);

                startIndex = foundIndex + split.Length;
            }

            return segments;
        }

        private static int FindSplitIndex(byte[] source, int startIndex, byte[] split)
        {
            for (int i = startIndex; i <= source.Length - split.Length; i++)
            {
                bool found = true;
                for (int j = 0; j < split.Length; j++)
                {
                    if (source[i + j] != split[j])
                    {
                        found = false;
                        break;
                    }
                }

                if (found)
                {
                    return i;
                }
            }

            return -1; // Split not found
        }
    }

 

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...