NEWS

[C#] Hướng dẫn xem mật khẩu wifi đã lưu trữ trong máy tính

[C#] Hướng dẫn xem mật khẩu wifi đã lưu trữ trong máy tính
Đăng bởi: Thảo Meo - Lượt xem: 5310 08:23:05, 29/09/2018DEVEXPRESS   In bài viết

Bài viết hôm nay, mình sẽ hướng dẫn các bạn cách xem mật khẩu Wifi trong lập trình C#. Thông thường, khi các bạn kết nối với một wifi và chọn nhớ mật khẩu, thì hệ thống sẽ lưu trữ thông tin của một profile wifi đó.

+ Cách thực hiện viết ứng dụng xem wifi, đầu tiên mình sẽ hướng dẫn các bạn xem mật khẩu bằng CMD (MS-DOS), và từ câu lệnh command này mình sẽ lấy dữ liệu output xuất ra và hiển thị chúng lên phần mềm.

1. Đầu tiên, các bạn mở MS-DOS Command và chạy với quyền Run as Administrator.

Thực hiện lệnh đọc các wifi của windows đã lưu trữ.

Gõ lệnh: netsh wlan show profile

=> Kết quả trả về như hình bên dưới:

xem mật khẩu wifi đã lưu c#

 

- Ở hình ảnh, trên đã hiển thị cho chúng ta thấy danh sách tất cả các profile.

Và bây giờ nếu mình muốn xem mật khẩu của wifi "Agribank" mình thực hiện lệnh sau:

netsh wlan show profile agribank key=clear

 

xem password wifi đã lưu

 

Và dưới đây là hình ảnh mật khẩu của Wifi Agribank:

 

xem password wifi đã lưu trên windows

 

- Và bây giờ mình sẽ lập trình ứng dụng, các bạn chỉ cần tạo vòng for duyệt qua các profile của wifi để lấy thông tin và hiển thị lên DataGridView là xong.

Giao diện ứng dụng:

 

xem password csharp

Source code show profile wifi C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;

namespace WifiPass
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int count = 0;
        int count_names = 0;
        DataTable table = new DataTable();

        #region console functions
        private string wifilist()
        {

            Process processWifi = new Process();
            processWifi.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            processWifi.StartInfo.FileName = "netsh";
            processWifi.StartInfo.Arguments = "wlan show profile";


            processWifi.StartInfo.UseShellExecute = false;
            processWifi.StartInfo.RedirectStandardError = true;
            processWifi.StartInfo.RedirectStandardInput = true;
            processWifi.StartInfo.RedirectStandardOutput = true;
            processWifi.StartInfo.CreateNoWindow = true;
            processWifi.Start();

            string output = processWifi.StandardOutput.ReadToEnd();

            string err = processWifi.StandardError.ReadToEnd();

            processWifi.WaitForExit();
            return output;
        }
        private string wifipassword(string wifiname)
        {

            string argument = "wlan show profile name="" + wifiname + "" key=clear";
            Process processWifi = new Process();
            processWifi.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            processWifi.StartInfo.FileName = "netsh";
            processWifi.StartInfo.Arguments = argument;


            processWifi.StartInfo.UseShellExecute = false;
            processWifi.StartInfo.RedirectStandardError = true;
            processWifi.StartInfo.RedirectStandardInput = true;
            processWifi.StartInfo.RedirectStandardOutput = true;
            processWifi.StartInfo.CreateNoWindow = true;
            processWifi.Start();

            string output = processWifi.StandardOutput.ReadToEnd();

            string err = processWifi.StandardError.ReadToEnd();

            processWifi.WaitForExit();
            return output;
        }
        private string wifipassword_single(string wifiname)
        {
            string get_password = wifipassword(wifiname);
            using (StringReader reader = new StringReader(get_password))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    Regex regex2 = new Regex(@"Key Content * : (?.*)");
                    Match match2 = regex2.Match(line);

                    if (match2.Success)
                    {
                        string current_password = match2.Groups["after"].Value;
                        return current_password;
                    }
                }
            }
            return "Open Network";
        }
        #endregion
        #region process data for wifi names
        private void parse_lines(string input)
        {

            using (StringReader reader = new StringReader(input))
            {

                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    count++;
                    regex_lines(line);
                }
            }
        }
        private void command_dump(string input_raw)
        {
            textBox1_pass.Text += input_raw + "
";
        }
        #endregion
        #region regex
        private void regex_lines(string input2)
        {
            Regex regex1 = new Regex(@"All User Profile * : (?.*)");
            Match match1 = regex1.Match(input2);

            if (match1.Success)
            {
                count_names++;
                string current_name = match1.Groups["after"].Value;
                string password = wifipassword_single(current_name);

                table.Rows.Add(count_names, current_name, password);
                textBox1_pass.Text += string.Format("{0}{1}{2}", count_names.ToString().PadRight(7), current_name.PadRight(20), password) + "
";
            }
        }
        #endregion
        #region table
        private void init_table()
        {
            table.Columns.Add("Number", typeof(int));
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Password", typeof(string));
        }
        private void reset_all()
        {
            count = 0;
            count_names = 0;
            table.Rows.Clear();
            textBox1_pass.Text = "";
        }
        #endregion

        private void get_passwords()
        {
            string wifidata = wifilist();
            parse_lines(wifidata);
        }

        private void colorOpenWifi()
        {
            int rowscount = dataGridView1.Rows.Count;
            for (int i = 0; i < rowscount; i++)
            {
                if (Convert.ToString(dataGridView1.Rows[i].Cells[2].Value).StartsWith("Open Network") == true)
                {
                    dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.LawnGreen;
                }
                if (Convert.ToString(dataGridView1.Rows[i].Cells[2].Value).Length < 9)
                {
                    dataGridView1.Rows[i].Cells[2].Style.BackColor = Color.Yellow;
                }
            }
            dataGridView1.Refresh();
        }

        private void export_CSV()
        {

            DateTime theDate = DateTime.Now;
            string dateString = theDate.ToString("dd-MM-yy-HH.mm.ss");
            string filename = "wifiPass-" + dateString + ".csv";


            var lines = new List();

            string[] columnNames = table.Columns.Cast().
                                              Select(column => column.ColumnName).
                                              ToArray();

            var header = string.Join(",", columnNames);
            lines.Add(header);

            var valueLines = table.AsEnumerable()
                       .Select(row => string.Join(",", row.ItemArray));
            lines.AddRange(valueLines);

            File.WriteAllLines(filename, lines);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            init_table();
        }


        private void button_getPasswords_Click(object sender, EventArgs e)
        {
            button_getPasswords.Enabled = false;
            button2_exportCSV.Enabled = false;
            reset_all();
            get_passwords();
            dataGridView1.DataSource = table;
            colorOpenWifi();
            button_getPasswords.Enabled = true;
            button2_exportCSV.Enabled = true;
        }

        private void button2_exportCSV_Click(object sender, EventArgs e)
        {
            button_getPasswords.Enabled = false;
            button2_exportCSV.Enabled = false;
            reset_all();
            get_passwords();
            dataGridView1.DataSource = table;
            colorOpenWifi();
            export_CSV();
            button_getPasswords.Enabled = true;
            button2_exportCSV.Enabled = true;
            MessageBox.Show("CSV Exported", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

HAPPY CODING heart

DOWNLOAD SOURCE

 

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn xem mật khẩu wifi đã lưu trữ trong máy tính
Đăng bởi: Thảo Meo - Lượt xem: 5310 08:23:05, 29/09/2018DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.