[工具-006] C#如何模拟发包登录

  最近接到一个任务,就是模拟某个贴吧的登录发帖功能,我的思路是通过IE浏览器工具登陆操作进行抓包记录登录时候请求的URL,请求方式,请求正文等信息进行模拟的发包。

1.首先我们要到登陆页面,以摇篮网为例子,用IE打开。http://user.yaolan.com/Login.aspx,我们按F12,然后选择网络点击三角形进行开始监控,然后我们在登陆页面开始操作。接着我们就可以获得到下图的信息。

2.我们抓取登录的链接,然后点击详细信息,我们可以得到下图

3.有了标头,我们也需要请求的正文

4.根据以上的信息,我们可以进行编码实现模拟发包

 /// <summary>
        /// 自动登录
        /// </summary>
        public static bool MaliciousLogin( string username, string password)
        {
            // 线程停止,放置切换账号导致错误
            System.Threading.Thread.Sleep(5000);
            // 登陆的URL
            string loginUrl = LOGIN_URL;
            // 是否登陆成功
            bool result = false;
            // 返回的HTML
            string responseHTML = string.Empty;
            // post的正文
            string loginstr = string.Format("__EVENTTARGET=btnLogin&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUJNDQ0NjMxMDM4DxYCHgx1cmxfcmVmZXJyZXIoKVVTeXN0ZW0uVXJpLCBTeXN0ZW0sIFZlcnNpb249Mi4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5Fmh0dHA6Ly93d3cueWFvbGFuLmNvbS9kGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQtjaGtSZW1lbWJlcv8bMzMumb%2FnRN0yh9bsCJyzxPq8&__EVENTVALIDATION=%2FwEWBQKuscygCwKl1bKzCQK1qbSRCwLR55GJDgKC3IeGDP1i0PaxIyWOZCrxXwv1DyNWzvS5&txtUserName={0}&txtPassword={1}", username, Util.GetMd5Str(password));
            // 转成指定的UTF-8编码
            byte[] replybyte = Encoding.UTF8.GetBytes(loginstr);
            try
            {
                // 空的cookie
                m_Cookie = new CookieContainer();
                // 创建request
                HTTPRequest = (HttpWebRequest)WebRequest.Create(loginUrl);
                HTTPRequest.CookieContainer = m_Cookie;
                HTTPRequest.ContentType = "application/x-www-form-urlencoded";
                HTTPRequest.Method = "POST";
                //post 开始
                HTTPRequest.ContentLength = replybyte.Length;
                Stream newStream = HTTPRequest.GetRequestStream();
                newStream.Write(replybyte, 0, replybyte.Length);
                newStream.Close();
                //post 结束

                //返回HTML
                HTTPResponse = (HttpWebResponse)HTTPRequest.GetResponse();
                Stream dataStream = HTTPResponse.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("utf-8"));
                responseHTML = reader.ReadToEnd();

                loginCookie = HTTPRequest.CookieContainer.GetCookies(new Uri(HOMEPAGEURL));
                m_Cookie = HTTPRequest.CookieContainer;
                if (!String.IsNullOrEmpty(loginCookie["user_name"].ToString()))
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            return result;
        }

 根据以上几步,我们就可以轻松模拟论坛登陆了,其实发帖也是一个道理,就是要填充cookie

结语

  • 受益,掌握了模拟发包

 

本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 

转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4625511.html

原文地址:https://www.cnblogs.com/superdo/p/4625511.html