Example #1
0
        static void Main(string[] args)
        {
            System.Console.Write("Enter a URL address:");
            string url = System.Console.ReadLine();

            System.Console.WriteLine("Scanning hyperlinks at: " + url);
            string page;

            if (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
            {
                page = GetPage(url);
            }
            else
            {
                page = ReadFile(url);
            }

            if (page == null)
            {
                System.Console.WriteLine("Can't process that type of file,"
                                         +
                                         "please specify an HTML file URL."
                                         );
                return;
            }

            ParseHTML parse = new ParseHTML();

            parse.Source = page;
            while (!parse.Eof())
            {
                char ch = parse.Parse();
                if (ch == 0)
                {
                    AttributeList tag = parse.GetTag();
                    if (tag["href"] != null)
                    {
                        System.Console.WriteLine("Found link: " +
                                                 tag["href"].Value);
                    }
                }
            }
        }
    void IHttpHandler.ProcessRequest(HttpContext Context)
    {
        string ServerURL = "";
        string ext = ".tunnel";
        string protocol = "http://";

        try
        {
            // Parsing incoming URL and extracting original server URL
            char[] URL_Separator = { '/' };
            string[] URL_List = Context.Request.Url.AbsoluteUri.Remove(0, protocol.Length).Split(URL_Separator);
            ServerURL = protocol + URL_List[2].Remove(URL_List[2].Length - ext.Length, ext.Length) + @"/";
            string URLPrefix = @"/" + URL_List[1] + @"/" + URL_List[2]; // Eg. "/handler/stg2web.tunnel";
            for ( int i = 3; i < URL_List.Length; i++ )
            {
                ServerURL += URL_List[i] + @"/";
            }
            ServerURL = ServerURL.Remove(ServerURL.Length -1, 1);
            WriteLog(ServerURL + " (" + Context.Request.Url.ToString() + ")");

            // Extracting POST data from incoming request
            Stream RequestStream = Context.Request.InputStream;
            byte[] PostData = new byte[Context.Request.InputStream.Length];
            RequestStream.Read(PostData, 0, (int) Context.Request.InputStream.Length);

            // Creating proxy web request
            HttpWebRequest ProxyRequest = (HttpWebRequest) WebRequest.Create(ServerURL);
            if (false)
            {
                ProxyRequest.Proxy = new WebProxy("proxy1:80", true);
            }
            ProxyRequest.Method = Context.Request.HttpMethod;
            ProxyRequest.UserAgent = Context.Request.UserAgent;
            CookieContainer ProxyCookieContainer = new CookieContainer();
            ProxyRequest.CookieContainer = new CookieContainer();
            ProxyRequest.CookieContainer.Add(ProxyCookieContainer.GetCookies(new Uri(ServerURL)));
            ProxyRequest.KeepAlive = true;

            // For POST, write the post data extracted from the incoming request
            if (ProxyRequest.Method == "POST")
            {
                ProxyRequest.ContentType = "application/x-www-form-urlencoded";
                ProxyRequest.ContentLength = PostData.Length;
                Stream ProxyRequestStream = ProxyRequest.GetRequestStream();
                ProxyRequestStream.Write(PostData, 0, PostData.Length);
                ProxyRequestStream.Close();
            }

            // Getting response from the proxy request
            HttpWebResponse ProxyResponse = (HttpWebResponse) ProxyRequest.GetResponse();

            if (ProxyRequest.HaveResponse)
            {
                // Handle cookies
                foreach(Cookie ReturnCookie in ProxyResponse.Cookies)
                {
                    bool CookieFound = false;
                    foreach(Cookie OldCookie in ProxyCookieContainer.GetCookies(new Uri(ServerURL)))
                    {
                        if (ReturnCookie.Name.Equals(OldCookie.Name))
                        {
                            OldCookie.Value = ReturnCookie.Value;
                            CookieFound = true;
                        }
                    }
                    if (!CookieFound)
                    {
                        ProxyCookieContainer.Add(ReturnCookie);
                    }
                }
            }

            Stream StreamResponse = ProxyResponse.GetResponseStream();
            int ResponseReadBufferSize = 256;
            byte[] ResponseReadBuffer = new byte[ResponseReadBufferSize];
            MemoryStream MemoryStreamResponse = new MemoryStream();

            int ResponseCount = StreamResponse.Read(ResponseReadBuffer, 0, ResponseReadBufferSize);
            while ( ResponseCount > 0 )
            {
                MemoryStreamResponse.Write(ResponseReadBuffer, 0, ResponseCount);
                ResponseCount = StreamResponse.Read(ResponseReadBuffer, 0, ResponseReadBufferSize);
            }

            byte[] ResponseData = MemoryStreamResponse.ToArray();
            string ResponseDataString = Encoding.ASCII.GetString(ResponseData);

            Context.Response.ContentType = ProxyResponse.ContentType;

            // While rendering HTML, parse and modify the URLs present
            if (ProxyResponse.ContentType.StartsWith("text/html"))
            {
                HTML.ParseHTML Parser = new HTML.ParseHTML();
                Parser.Source = ResponseDataString;
                while (!Parser.Eof())
                {
                    char ch = Parser.Parse();
                    if (ch == 0)
                    {
                        HTML.AttributeList Tag = Parser.GetTag();

                        if (Tag.Name.Equals("img", StringComparison.InvariantCultureIgnoreCase))
                        {
                            // Adjust image tags
                        }

                        if (Tag["href"] != null)
                        {
                            if (Tag["href"].Value.StartsWith(@"/"))
                            {
                                WriteLog("URL " + Tag["href"].Value + " modified to " + URLPrefix + Tag["href"].Value);
                                ResponseDataString = ResponseDataString.Replace(
                                    "\"" + Tag["href"].Value + "\"",
                                    "\"" + URLPrefix + Tag["href"].Value + "\"");
                            }
                        }

                        if (Tag["src"] != null)
                        {
                            if (Tag["src"].Value.StartsWith(@"/"))
                            {
                                WriteLog("URL " + Tag["src"].Value + " modified to " + URLPrefix + Tag["src"].Value);
                                ResponseDataString = ResponseDataString.Replace(
                                    "\"" + Tag["src"].Value + "\"",
                                    "\"" + URLPrefix + Tag["src"].Value + "\"");
                            }
                        }
                    }
                }

                // Replace script/style file url's
                ResponseDataString = ResponseDataString.Replace(
                    "url('/",
                    "url('" + URLPrefix + "/");

                // replace flash file url's
                ResponseDataString = ResponseDataString.Replace(
                    "swf: '/",
                    "swf: '" + URLPrefix + "/");

                Context.Response.Write(ResponseDataString);
            }
            else
            {
                Context.Response.OutputStream.Write(ResponseData, 0, ResponseData.Length);
            }

            MemoryStreamResponse.Close();
            StreamResponse.Close();
            ProxyResponse.Close();
        }
        catch (Exception Ex)
        {
            Context.Response.Write(Ex.Message.ToString());
            WriteLog("An error has occurred while requesting the URL " +
                ServerURL + "(" + Context.Request.Url.ToString() + ")\n" +
                Ex.ToString());
        }
    }
Example #3
0
        private static string ParsePageForImage(string page)
        {
            ParseHTML parse = new ParseHTML();
            parse.Source = page;
            while (!parse.Eof())
            {
                char ch = parse.Parse();
                if (ch == 0)
                {
                    AttributeList tag = parse.GetTag();
                    if (tag["src"] != null && tag.Name == "img" && tag["src"].Value.Contains("scans"))
                    {
                        return tag["src"].Value;
                    }
                }
            }

            return "";
        }