惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
Google Developers Blog
S
SegmentFault 最新的问题
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
P
Proofpoint News Feed
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
L
LangChain Blog
F
Fortinet All Blogs
C
Check Point Blog
博客园_首页
I
InfoQ
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
Engineering at Meta
Engineering at Meta
美团技术团队
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 燕闪硕

EF Core 批量操作实战:3 种方案对比与性能测试 Python 开发 Windows 客户端:Web 技术做界面的 5 款核心工具 Python描述器(Descriptor)深度解析:OOP底层核心机制实操指南 使用pyenv-win管理多版本Python Python字符串编码及正则表达式使用 利用canvas在手机页面实现景区导航的一点思路 重装系统后遇到Git Authentication failed 错误 [转帖]如何准确高效的获取数据库新插入数据的主键id [转帖]SQL中partition关键字的使用 利用分布类防止EF更新模型丢失验证信息 列表样式切换 CSS3 简易照片墙 HTML5表单增强 HTML5 元素拖放 HTML 5 全局属性 微软build 2015 工厂方法 简单工厂 System.Data.SQLite兼容32位和64位问题
写个程序登陆58同城
燕闪硕 · 2015-03-31 · via 博客园 - 燕闪硕

代码 http://pan.baidu.com/s/1i3kqYjV

主要代码,还是比较复杂的,58验证比较难破

        /// <summary>
        /// 获取城市编码
        /// </summary>
        /// <param name="unumber">石家庄:sjz,北京:bj</param>
        /// <returns></returns>
        public string GetCityCode()
        {
            string citycode = "";
            //从网站上取
            //site = CommunBase.Core.GetCityCodeByWeb(siteid, cityname);
            string siteURL = "http://www.58.com/changecity.aspx";
            citycode = "";//默认是无
            string text = GetPageData_UTF8(siteURL);//html文本
            Regex reg = new Regex("<a href=\"http://(.{1,20}).58.com/\" onclick=\"co\\('(.{1,20})'\\)\">" + cityName + "</a>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            Match mc = reg.Match(text);
            if (mc.Success)
                citycode = mc.Groups[1].Value.Trim();
            return citycode;
        }

        /// <summary>
        /// 获得城市ID 
        /// </summary>
        /// <param name="cityname"></param>
        /// <returns></returns>
        public string GetCityID()
        {
            string html = GetPageData("http://post.58.com/postindex.htm", "utf-8");
            return GetDizhi_MultiLine(html, "58.com/(\\d*?)/\" onclick=\"\\S*?>" + cityName, 1);
        }
        /// <summary>
        /// 检测是否登陆成功
        /// </summary>
        /// <returns></returns>
        private bool IsLogin()
        {
            string ss = GetPageData("http://my.58.com/", "", "utf-8");
            if (ss.IndexOf("<title>用户中心</title>") > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 登陆主方法
        /// </summary>
        /// <returns></returns>
        public bool DoLogin()
        {
            try
            {
                if (IsLogin()) return true;
                //Caiji caiji = new Caiji();
                city = GetCityCode();
                cityID = GetCityID();
                string user = siteAccount; //用户名
                string pass = sitePassword; //密码
                string html = GetPageData("http://" + Core.city + ".58.com/", "http://www.58.com/changecity.aspx", "utf-8");
                html = GetPageData("http://passport.58.com/login", "http://" + Core.city + ".58.com/", "utf-8");
                string str2 = GetDizhi_MultiLine(html, "timespan[\\s\\S]*?(\\d{13})", 1);
                long timespan = 0;
                try
                {
                    timespan = Convert.ToInt64(str2);
                }
                catch { timespan = 1411093327735; }
                string str3 = md5(pass);
                string str4 = md5(str3 + str2.Substring(5, 6));//P1
                string str5 = str3.Substring(8, 0x10);
                string str6 = string.Empty;
                for (int i = str5.Length - 1; i > -1; i--)
                {
                    str6 = str6 + str5[i];
                }
                string str7 = md5(str6 + str2.Substring(5, 6));//P2
                //这个也需要取
                string subResult = GetDizhi_MultiLine(html, "\"010001\",\\s*\"\\s*([\\w]*?)\"\\)\\);", 1);
                string str9 = "encryptString('" + str2 + HttpUtility.UrlEncode(pass) + "', '010001', '" + subResult + "')";
                string str10 = RsaUtils.EncryptString(str2 + HttpUtility.UrlEncode(pass), "010001", subResult, 130);
                string ptk = GetDizhi_MultiLine(html, "id=\"ptk\" value=\"(\\w*?)\"", 1);
                string cd = GetDizhi_MultiLine(html, "id=\"cd\" value=\"(\\d{4})\"", 1);
                //string isweak = "0";
                data =
                    "isweak=0&username=" + user +
                    "&password=password" +
                    "&p1=" + str4 + //正确
                    "&p2=" + str7 + //正确
                    "&p3=" + str10 + //正确
                    "&timesign=" + timespan +
                    "&ptk=" + ptk + //正确
                    "&cd=" + cd + //正确
                    "&mcresult=undefined";
                Cookie ck = new Cookie();
                ck.Name = "58home";
                ck.Value = city;
                ck.Domain = ".58.com";
                ck.Path = "/";
                cc.Add(ck);
                ck = new Cookie();
                ck.Name = "city";
                ck.Value = city;
                ck.Domain = ".58.com";
                ck.Path = "/";
                cc.Add(ck);
                Encoding Ed = Encoding.GetEncoding("utf-8");
                string url = "http://passport.58.com/dounionlogin";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.Referer = "http://passport.58.com/login";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept =
                    "text/html, application/xhtml+xml, */*";
                request.UserAgent =
                    "Mozilla/5.0 (Windows NT 6.1; WOW64;Trident/7.0; rv:11.0) like Gecko";
                request.Headers.Add("Cache-Control", "no-cache");
                request.AllowAutoRedirect = false;//是否重定向
                request.CookieContainer = cc;
                request.KeepAlive = true;
                request.ServicePoint.Expect100Continue = false;
                byte[] bbb = Ed.GetBytes(data);
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bbb, 0, bbb.Length);
                requestStream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8"; //默认编码
                }
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                data = reader.ReadToEnd();


                if (data.IndexOf("my.58.com") == -1)
                {
                    return false;
                }
                else
                {
                    string strCookie = response.Headers["Set-Cookie"];
                    CookieCollection cl = GetCookiesFromReponseHead(strCookie, ".58.com");
                    foreach (Cookie oneck in cl)
                    {
                        Cookie newhk = new Cookie();
                        newhk.Domain = ".58.com";
                        newhk.Name = oneck.Name;
                        newhk.Value = oneck.Value;
                        Cookie ck2 = FindCookie("post.58.com", oneck.Name);
                        if (ck2 == null)
                        {
                            cc.Add(newhk);
                        }
                        else
                        {
                            ck2 = newhk;
                        }
                    }
                    response.Close();
                    return IsLogin();
                }
            }
            catch (Exception ex) { return false; }
        }