Friday, November 15, 2013

C# Program web based password try.

Scenario: I just work for a new company. I want to get into some network switch and check the configuration to draw the network diagram and backup the switch configuration. But nobody knows the password in our IT team, and no document mention that. It is bad. :(.
I can reset the switches, but we will lost the configuration. And nobody know what is the current switch setting. The switch provide web console. So I use the C#, Selenium, Chrome web driver, and the password dictionary file to find out the password.

Here is the C# code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Support.UI;
using System.Threading;
using System.IO;
namespace myFirstSelenium
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = "PwDict.txt";
            string url = "http://192.168.1.20/";
            string userName = "admin";
            string password = "";
            string returnValue = "";
            StreamReader fileReader = new StreamReader(filePath);
            try
            {
                password = fileReader.ReadLine();
               
                while (password != null)
                {
                    Console.WriteLine();
                    Console.WriteLine("Trying {0} with username: {1} and password: {2}...",url,userName,password);
                    returnValue = Web.Access(url, userName, password);
                    if (returnValue == "Sucessfull")
                    {
                        break;
                    }
                    password = fileReader.ReadLine();
                }

                if (password == null)
                {
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine("***Can not find any password!****");
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine("***Congratulation***");
                    Console.WriteLine("We found username: {0} ,and password: {1} for {2}!", userName, password, url);
                }
            }
            finally
            {
                fileReader.Close();
            }
        }

       
    }

    class Web
    {
        #region Access Web Site
        public static string Access (string url, string loginname, string password)
        {
            ChromeOptions options = new ChromeOptions();
            //ChromeOptions options = new ChromeOptions();
            options.AddArguments("--no-proxy-server");
            ChromeDriverService service = ChromeDriverService.CreateDefaultService();
            service.SuppressInitialDiagnosticInformation = true;
         
           
            IWebDriver driver = new ChromeDriver(service, options);
           
            //driver.Navigate().GoToUrl("http://192.168.1.20/");
            driver.Navigate().GoToUrl(url);
            driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10));

            while (true)
            {
                try
                {
                    driver.SwitchTo().Frame("main");
                    break;
                }
                catch (Exception)
                {
                    int milliseconds = 1000;
                    Thread.Sleep(milliseconds);
                }
            }

            while (true)
            {
                try
                {
                    driver.FindElement(By.Name("Password")).Clear();
                    break;
                }
                catch (Exception)
                {
                    int milliseconds = 1000;
                    Thread.Sleep(milliseconds);
                }
            }

            //driver.SwitchTo().Frame("main");
            driver.FindElement(By.Name("Username")).Clear();
            driver.FindElement(By.Name("Username")).SendKeys(loginname);
            driver.FindElement(By.Name("Password")).Clear();
            driver.FindElement(By.Name("Password")).SendKeys(password);
            driver.FindElement(By.LinkText("OK")).Click();
           
            string page = driver.PageSource;

            if (page.Contains("<title>Error</title>"))
            {
                driver.Close();
                return "Failed";
            }
            else
            {
                return "Sucessfull";
            }
           
        }
        #endregion
    }
}