Stupid C# Question

Put your programming skills to the test in these challenges.

Stupid C# Question

Post by ingenium on Thu Aug 21, 2008 1:36 pm
([msg=10199]see Stupid C# Question[/msg])

I am trying to get the html code of a webpage (http://www.hackthissite.org/missions/prog/11/), here it is the code i am using:
C#
Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

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

        private void button1_Click(object sender, EventArgs e)
        {
           
            string formkey = "";
            string html = "";
            int indexformkey = 0;
           
           
            Post post = new Post(textBox1);
            post.nick = "ingenium";
            post.url = "http://www.hackthissite.org";
            html = post.go();
            for (indexformkey = 0; indexformkey < html.Length; indexformkey++)
            {
                if (html[indexformkey + 1] == 'f' && html[indexformkey + 2] == 'o' && html[indexformkey + 3] == 'r' && html[indexformkey + 4] == 'm' && html[indexformkey + 5] == 'k' && html[indexformkey + 6] == 'e' && html[indexformkey + 7] == 'y')
                {
                    indexformkey += 17;
                    break;
                }

            }
            for (; html[indexformkey] != '"'; indexformkey++)
            {
                formkey += html[indexformkey];
            }
            post.log(textBox1,"Chiave del form: " + Environment.NewLine + formkey);
            post.url = "http://www.hackthissite.org/pages/index/index2.php";
            post.content = "formkey="+formkey+"%3D%3D&txt_username=ingenium&txt_password=*****&btn_submit=Login";
            post.login();
            post.url = "http://www.hackthissite.org/missions/prog/11/";
            html = post.go();
            post.log(textBox1, html);
           
         

        }


    }

    public class Post
    {
        #region varaibles
        private string _url = "";
        private string _content = "";
        private string _nick = "";
        private CookieContainer _cookie = new CookieContainer();
        private HttpWebRequest _request;
        private HttpWebResponse _response;
        private TextBox textbox;
        #endregion

        #region get&set
        public string url
        {
            get
            {
                return _url;
            }
            set
            {
                _url = value;
            }
        }
        public string content
        {
            get
            {
                return _content;
            }
            set
            {
                _content = value;
            }
        }
        public string nick
        {
            get
            {
                return _nick;
            }
            set
            {
                _nick = nick;
            }
        }
        public HttpWebRequest request
        {
            get
            {
                return _request;
            }
            set
            {
                _request = value;
            }
        }
        public HttpWebResponse response
        {
            get
            {
                return _response;
            }
            set
            {
                _response = value;
            }
        }
        public CookieContainer cookie
        {
            get
            {
                return _cookie;
            }
            set
            {
                _cookie = value;
            }
        }
        #endregion

        /// <summary>
        /// costruttore
        /// </summary>
        /// <param name="textbox"></param>
        public Post(TextBox textbox)
        {
            this.textbox = textbox;
            log(textbox, "Avviata procedura di log in");
        }

        /// <summary>
        /// logga in un sito web
        /// </summary>
        public void login()
        {
            try
            {
                _request = (HttpWebRequest)WebRequest.Create(_url);
            }
            catch (Exception ex)
            {
                log(textbox, "Sito web non trovato");
            }
            _request.CookieContainer = new CookieContainer();
            byte[] buffer = new ASCIIEncoding().GetBytes(_content);
            _request.ContentType = "application/x-www-form-urlencoded";
            _request.Method = "POST";
            _request.ContentLength = content.Length;
            try
            {
                Stream strContent = _request.GetRequestStream();
                strContent.Write(buffer, 0, buffer.Length);
                strContent.Close();
                _response = (HttpWebResponse)_request.GetResponse();
                _cookie.Add(_response.ResponseUri, _response.Cookies);
                StreamReader strResponse = new StreamReader(_response.GetResponseStream());
                string output = strResponse.ReadToEnd();
                strResponse.Close();
                if (output.Contains(nick))
                {
                    log(textbox, "Log In effettuato");
                    log(textbox, "Cookie: " + Environment.NewLine + _cookie.GetCookieHeader(response.ResponseUri));

                }
                else
                {
                    log(textbox, "Log In fallito");
                    log(textbox, output);
                }
                _request.Abort();
                _response.Close();

            }
            catch (Exception ex)
            {
                log(textbox, "Il tentativo di connessione al sito web è fallito. Controllare l'indirizzo immesso.");
            }
        }

        /// <summary>
        /// va ad una pagina web, da usare dopo essersi loggati
        /// </summary>
        public string go()
        {
            _request = (HttpWebRequest)WebRequest.Create(_url);
           
            _request.CookieContainer = cookie;
            try
            {
                HttpWebResponse _response = (HttpWebResponse)_request.GetResponse();
                StreamReader strResponse = new StreamReader(_response.GetResponseStream());
                string output = strResponse.ReadToEnd();
                log(textbox, "Connessione al sito web riuscita");
                return output;
            }
            catch (Exception ex)
            {
                log(textbox, "Il tentativo di connessione al sito web è fallito. Controllare l'indirizzo immesso");
                return "fail";
            }
        }
       
        /// <summary>
        /// è solo una funzione che permette di capire
        /// cosa sta accadendo
        /// </summary>
        /// <param name="target"> textbox dove viene scritto il log </param>
        /// <param name="output"> contenuto del log </param>
        public void log(TextBox target, string output)
        {
            target.Text = target.Text + output + Environment.NewLine + "--------------" + Environment.NewLine;
        }
    }

}


Using this code i can log in, and it is ok, but when i try to get the html code from http://www.hackthissite.org/missions/prog/11/ with post.go() hackthissite.org replies that i need to be authed to access... why? i am quite sure that i am passing the right cookie :?


P.s. It is coded in Visual C#!!!
ingenium
New User
New User
 
Posts: 2
Joined: Thu Aug 21, 2008 1:29 pm
Blog: View Blog (0)


Re: Stupid C# Question

Post by AbaxHacker on Thu Aug 21, 2008 6:33 pm
([msg=10216]see Re: Stupid C# Question[/msg])

Sorry that i can't really fix the code, because I am not huge in c++, and site-ing but: Try looking for a cookie with a passing vars' that are use to send/recive the cookie also try after catching the cookie sending it directly, thats all I can say, but by your theory it seems to be fine. So try making the actual loading page the problem one and load the cookie straigth from their, that is all I can say.
AbaxHacker
New User
New User
 
Posts: 17
Joined: Tue Aug 19, 2008 6:31 pm
Blog: View Blog (0)


Re: Stupid C# Question

Post by ingenium on Fri Aug 22, 2008 2:14 am
([msg=10240]see Re: Stupid C# Question[/msg])

AbaxHacker wrote:Sorry that i can't really fix the code, because I am not huge in c++, and site-ing but: Try looking for a cookie with a passing vars' that are use to send/recive the cookie also try after catching the cookie sending it directly, thats all I can say, but by your theory it seems to be fine. So try making the actual loading page the problem one and load the cookie straigth from their, that is all I can say.

It's C#, not C++ ;)
ingenium
New User
New User
 
Posts: 2
Joined: Thu Aug 21, 2008 1:29 pm
Blog: View Blog (0)


Re: Stupid C# Question

Post by BhaaL on Fri Aug 22, 2008 9:13 am
([msg=10257]see Re: Stupid C# Question[/msg])

Probably look into WebClient and CookieContainer - Creating a derived class from WebClient that sets a CookieContainer permanently might help.

Code: Select all
    public class CookieWebClient : WebClient
    {
        public CookieContainer CookieContainer { get; set; }
        public CookieWebClient()
            : this(new CookieContainer())
        {
        }
        public CookieWebClient(CookieContainer cc)
            : base()
        {
            CookieContainer = cc;
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            var baseRequest = base.GetWebRequest(address);
            var httpRequest = baseRequest as HttpWebRequest;

            if (httpRequest != null)
            {
                httpRequest.CookieContainer = CookieContainer;
            }

            return baseRequest;
        }
    }


Using webClient.DownloadString, webClient.UploadString you can do that quite easily (Download* for GET, Upload* for POST).
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: Stupid C# Question

Post by AbaxHacker on Sat Aug 23, 2008 12:28 am
([msg=10294]see Re: Stupid C# Question[/msg])

I liked making that game emu n fortran...
AbaxHacker
New User
New User
 
Posts: 17
Joined: Tue Aug 19, 2008 6:31 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests