天天看点

Ladon插件实例内网Web扫描C#源码

Ladon插件实例内网Web扫描C#源码

鑾峰彇缃戦〉鏍囬銆佹湇鍔″櫒Banner锛屼緥瀛愪粎鎺㈡祴80绔彛

澶у鍙嚜琛屼慨鏀规坊鍔犲叾瀹冪鍙h瘑鍒畾鍒禬eb鎵弿鍔熻兘

濡傝瘑鍒嚭鐗瑰畾涓棿浠舵垨WEB鍚庡啀妫€娴嬫槸鍚﹀瓨鍦ㄦ紡娲炵瓑

缂栬瘧鍚庣殑netscan.dll鍙敼鎴愪换鎰忓悕绉癓adon鍧囧彲鍔犺浇

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;

namespace LadonDLL
{
    public class scan
    {
        public static string run(string ip)
        {
            if (string.IsNullOrEmpty(ip))
                return "";
            else
            {

                //192.11.22.10    Microsoft-IIS/10.0      IIS Windows
                //192.11.22.1     H3C-Miniware-Webs       ER3200G2绯荤粺绠$悊
                return ip + "\t" + getURLbanner(ip) + "\t" + GetTitle(getHtml("http://" + ip,2));
            
            }

        }

        private static string getURLbanner(string url)
        {
            HttpWebResponse res;
            if (!url.ToLower().Contains("https://") && !url.ToLower().Contains("http://"))
                url = "http://" + url;

            try
            {
                var req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
                req.Method = "HEAD";
                req.Timeout = 1000;
                var res = (HttpWebResponse)req.GetResponse();

                if (res.StatusCode == HttpStatusCode.OK || res.StatusCode == HttpStatusCode.Forbidden || res.StatusCode == HttpStatusCode.Redirect || res.StatusCode == HttpStatusCode.MovedPermanently)
                {
                    return res.Server;
                }

                //res.Close();

                return res.Server;
            }
            catch (WebException ex)
            {
                return "";
            }
        }

        private static string GetTitle(string html)
        {
            if (html.Contains("<hTmlKErRor>"))
            {
                //return html.Replace("<hTmlKErRor>", "");
                return "";
            }

            html = html.Replace("<br>", "");
            html = html.Replace("<BR>", "");
            html = html.Replace("\r\n", "");
            html = html.Replace("&nbsp;", " ");
            html = html.Replace("\n", "").Trim();

            String regex = @"<title.+</title>";

            String title = Regex.Match(html, regex).ToString();
            title = Regex.Replace(title, @"[\""]+", "");

            title = title.TrimStart('<');

            string regex2 = @">.+</title>";

            string title2 = Regex.Match(title, regex2).ToString();
            title2 = title2.TrimStart('>').Replace("</title>", "").Trim();

            if (title2.Length > 50)
                return title2.Substring(0, 50);

            return title2;

        }

        private static string getHtml(string url, int codingType)
        {

            try
            {
                if (!url.ToLower().Contains("https://") && !url.ToLower().Contains("http://"))
                    url = "http://" + url;
                WebClient myWebClient = new WebClient();
                if (url.ToLower().Contains("https://"))
                {
                    System.Net.ServicePointManager.ServerCertificateValidationCallback +=
    delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
             System.Security.Cryptography.X509Certificates.X509Chain chain,
             System.Net.Security.SslPolicyErrors sslPolicyErrors)
    {
        return true; // **** Always accept
    };

                }

                byte[] myDataBuffer = myWebClient.DownloadData(url);
                //return Encoding.Default.GetString(myDataBuffer);
                string strWebData = System.Text.Encoding.Default.GetString(myDataBuffer);

                //鑷姩璇嗗埆缂栫爜  涓嶄竴瀹氭湁<meta  姣斿 鐧惧害寮€鏀惧钩鍙?content="text/html; charset=gbk">
                //Match charSetMatch = Regex.Match(strWebData, "<meta([^>]*)charset=(\")?(.*)?\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                Match charSetMatch = Regex.Match(strWebData, "(.*)charset=(\")?(.*)?\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);

                string webCharSet = charSetMatch.Groups[3].Value.Trim().ToLower();

                if (webCharSet != "gb2312" && webCharSet != "gbk")
                {
                    webCharSet = "utf-8";
                }

                if (System.Text.Encoding.GetEncoding(webCharSet) != System.Text.Encoding.Default)
                {
                    strWebData = System.Text.Encoding.GetEncoding(webCharSet).GetString(myDataBuffer);
                }



                //if (codingType == 1)
                //    return Encoding.Unicode.GetString(myDataBuffer);
                //else if (codingType == 2)
                //    return Encoding.Default.GetString(myDataBuffer);//GBK 936
                //else if (codingType == 3)
                //    return Encoding.UTF8.GetString(myDataBuffer);//65501

                return strWebData;

            }
            catch (Exception ex)
            {
                //Console.WriteLine(url + " " + ex.Message);
                return "<hTmlKErRor>" + ex.Message;
            }

            return "";
        }

    }
}


           

继续阅读