using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static CSharpGuiMemScanner.MemoryExploiter;

namespace CSharpGuiMemScanner
{
    public partial class MemExploit : Form
    {

        private static string MethodToCall = string.Empty;

        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
        (
            int nLeftRect,     
            int nTopRect,      
            int nRightRect,    
            int nBottomRect,   
            int nWidthEllipse, 
            int nHeightEllipse 
        );
        public MemExploit()
        {
            InitializeComponent();
            InitiateProgessBar();

            //Below code is for just rounded corners of the Program
            this.FormBorderStyle = FormBorderStyle.None;
            Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));

        }

        public void WriteLog(string text)
        {
            label11.Text = text;
        }
        private void MemExploit_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
        }

        private void PatScan()
        {
            int progressPercentage = 0;
            backgroundWorker1.ReportProgress(progressPercentage);
            string proc = textBox1.Text;
            string addy = textBox2.Text;
            List<IntPtr> addr = new List<IntPtr>();
            uint getID = 0;
            string[] hexValues = addy.Split(' ');
            byte[] bytess = hexValues.Select(s => Convert.ToByte(s,16)).ToArray();

            if (checkBox1.Checked)
            {
                getID = Convert.ToUInt32(textBox4.Text);
                addr = MemoryExploiter.FindPatternInProcess(proc,bytess,getID,true);
            }
            else
            {
                addr = MemoryExploiter.FindPatternInProcess(proc, bytess, 0, false);
            }
            if (addr.Count > 0)
            {
                WriteLog("Scan Success");
                textBox3.ForeColor = Color.Green;
                foreach (var item in addr)
                {
                    textBox3.AppendText("0x" + item.ToString("X"));
                    if(addr.Count > 1)
                    {
                        textBox3.AppendText(Environment.NewLine);
                    }
                }
                progressPercentage = 100;
                backgroundWorker1.ReportProgress(progressPercentage);
            }
            else
            {
                WriteLog("Scan Fail");
                textBox3.ForeColor = Color.Red;
                textBox3.Text = "None";
                progressPercentage = 100;
                backgroundWorker1.ReportProgress(progressPercentage);
            }
        }
        private void WriteMem()
        {
            int progressPercentage = 0;
            backgroundWorker1.ReportProgress(progressPercentage);
            uint getID = 0;
            bool ok = false;
            if (checkBox1.Checked)
            {
                ok = true;
                getID = Convert.ToUInt32(textBox4.Text);
            }

            long put = 0;
            string getAddy = textBox5.Text;
            string finaldata = getAddy.Replace("0x", "");
            if (long.TryParse(finaldata, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out put))
            {
                //Write mem
                if (ok)
                {
                    WPM(put, textBox1.Text, Convert.ToInt32(textBox6.Text), getID, true);

                }
                else if (!ok)
                {
                    WPM(put, textBox1.Text, Convert.ToInt32(textBox6.Text), 0, false);
                }
                if (isWriteDone)
                {
                    WriteLog("Write Success");
                }
                progressPercentage = 100;
                backgroundWorker1.ReportProgress(progressPercentage);
            }
        }
        private void ReadMem()
        {
            int progressPercentage = 0;
            backgroundWorker1.ReportProgress(progressPercentage);
            bool ok = false;
            uint getID = 0;
            if (checkBox1.Checked)
            {
                ok = true;
                getID = Convert.ToUInt32(textBox4.Text);
            }
            long put = 0;
            string getAddy = textBox7.Text;
            string finaldata = getAddy.Replace("0x", "");
            if (long.TryParse(finaldata, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out put))
            {
                //Read mem
                if (ok)
                {
                    textBox8.Text = RPM(put, textBox1.Text, getID, true).ToString();
                }
                else if (!ok)
                {
                    textBox8.Text = RPM(put, textBox1.Text, 0, false).ToString();
                }
                if (isReadDone)
                {
                    WriteLog("Read Success");
                }
                progressPercentage = 100;
                backgroundWorker1.ReportProgress(progressPercentage);
            }
        }
        private void WriteByte()
        {
            int progressPercentage = 0;
            backgroundWorker1.ReportProgress(progressPercentage);
            bool ok = false;
            uint getID = 0;
            if (checkBox1.Checked)
            {
                ok = true;
                getID = Convert.ToUInt32(textBox4.Text);
            }
            int put = 0;
            string address = textBox9.Text;
            string shellcode = textBox10.Text;
            string[] hexValues = shellcode.Split(' ');
            byte[] bytess = hexValues.Select(s => Convert.ToByte(s, 16)).ToArray();
            string finaldata = address.Replace("0x", "");
            if (int.TryParse(finaldata, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out put))
            {
                //Write Bytes
                if (ok)
                {
                    WBYTE(put, textBox1.Text, bytess, getID, true);
                }
                else if (!ok)
                {
                    WBYTE(put, textBox1.Text, bytess, 0, false);

                }
                WriteLog("Write Success");
                progressPercentage = 100;
                backgroundWorker1.ReportProgress(progressPercentage);
            }
        }

        private static string tmpBytes = string.Empty;
        private  void GenPattern()
        {
            int progressPercentage = 0;
            backgroundWorker1.ReportProgress(progressPercentage);
            bool ok = false;
            uint getID = 0;
            if (checkBox1.Checked)
            {
                ok = true;
                getID = Convert.ToUInt32(textBox4.Text);
            }
            long put = 0;
            string getAddy = gunaTextBox1.Text;
            string finaldata = getAddy.Replace("0x", "");
            if (long.TryParse(finaldata, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out put))
            {
                string pika = string.Empty;
                //Write mem

                if (gunaCheckBox2.Checked)
                {
                    if (ok)
                    {
                        pika = PMAKER(put, textBox1.Text, Convert.ToInt32(gunaTextBox2.Text), 0, false,true,true);
                    }
                    else
                    {
                        pika = PMAKER(put, textBox1.Text, Convert.ToInt32(gunaTextBox2.Text), 0, false,false,true);
                    }
                }
                else if (gunaCheckBox3.Checked)
                {
                    if (ok)
                    {
                        pika = PMAKER(put, textBox1.Text, Convert.ToInt32(gunaTextBox2.Text), 0, false, true, false, true);
                    }
                    else
                    {
                        pika = PMAKER(put, textBox1.Text, Convert.ToInt32(gunaTextBox2.Text), 0, false,false,false,true);
                    }
                }

                else
                {
                    if (ok)
                    {
                        pika = PMAKER(put, textBox1.Text, Convert.ToInt32(gunaTextBox2.Text), 0, true);
                    }
                    else
                    {
                        pika = PMAKER(put, textBox1.Text, Convert.ToInt32(gunaTextBox2.Text), 0);
                    }
                }
                tmpBytes = pika;
                textBox12.Text = pika;
                if (isAOBDone)
                {
                    WriteLog("Pattern Made..");
                    progressPercentage = 100;
                    backgroundWorker1.ReportProgress(progressPercentage);
                }
            }
        }

        private  void gunaGradientButton4_Click(object sender, EventArgs e)
        {
            MethodToCall = "PatScan";
            ToBeCalledOnClick();
            textBox3.Clear();
            skipLoad = true;
            WriteLog("AoB Scanning");
            //PatScan();
            label19.Text = totalPatAddresses.ToString() + " Results";
        }

        private void ToBeCalledOnClick()
        {
            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;
            progressBar1.Step = 1;
            backgroundWorker1.RunWorkerAsync();
        }

        private void InitiateProgessBar()
        {
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            switch (MethodToCall)
            {
                case "ReadMem":
                    ReadMem();
                    break;
                case "WriteMem":
                    WriteMem();
                    break;
                case "WriteByte":
                    WriteByte();
                    break;
                case "GenPattern":
                    GenPattern();
                    break;
                case "PatScan":
                    PatScan();
                    break;
                default:
                    return;

            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

       

        private  void gunaGradientButton1_Click(object sender, EventArgs e)
        {
            MethodToCall = "ReadMem";
            ToBeCalledOnClick();
            skipLoad = true;
            WriteLog("Attempting Read");
            //ReadMem();
        }

        private  void gunaGradientButton2_Click(object sender, EventArgs e)
        {
            MethodToCall = "WriteMem";
            ToBeCalledOnClick();
            skipLoad = true;
            WriteLog("Attempting Write");
             //WriteMem();
            //cacheProcess();
        }

        private  void gunaGradientButton3_Click(object sender, EventArgs e)
        {
            MethodToCall = "WriteByte";
            ToBeCalledOnClick();
            skipLoad = true;
            WriteLog("Attempting Write");
            //WriteByte();
        }

        private void gunaLabel1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

        private void gunaButton1_Click(object sender, EventArgs e)
        {
            MethodToCall = "GenPattern";
            ToBeCalledOnClick();
        }

        private void gunaButton2_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(tmpBytes.Trim());
        }

        private void gunaGroupBox1_Click(object sender, EventArgs e)
        {

        }
    }
}
