# coding:utf-8
# @Time : 2021/4/1 21:59 
# @Author : R0A1NG
# @File : bugku自动签到.py 
# @Software: PyCharm
import requests
import urllib3
import re
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time


def mailfs(nr):
    from_addr = '[email protected]'  # 邮件发送账号
    to_addrs = '[email protected]'  # 接收邮件账号
    qqCode = 'xxxxxxxxxxx'  # 授权码(这个要填自己获取到的)
    smtp_server = 'smtp.qq.com'  # 固定写死
    smtp_port = 465  # 固定端口
    
    # 配置服务器
    stmp = smtplib.SMTP_SSL(smtp_server, smtp_port)
    stmp.login(from_addr, qqCode)

    # 组装发送内容
    message = MIMEText(nr, 'plain', 'utf-8')  # 发送的内容
    message['From'] = Header("R0A1NG", 'utf-8')  # 发件人名字
    message['To'] = Header("R0A1NG", 'utf-8')  # 收件人名字
    message['Subject'] = Header('bugku签到任务', 'utf-8')  # 邮件标题

    try:
        stmp.sendmail(from_addr, to_addrs, message.as_string())
    except Exception as e:
        print('邮件发送失败--' + str(e))
    print('邮件发送成功')


urllib3.disable_warnings()

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/87.0.4280.141 Safari/537.36 "
}


def checkin(s, bugkucookie):
    bugkucookie = bugkucookie.strip()
    global headers
    headers['cookie'] = bugkucookie
    url_qd = 'https://ctf.bugku.com/user/checkin'  # 签到页面
    req = requests.get(url=url_qd, headers=headers)
    if '签到成功' in req.text:
        with open('log/' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '.txt', 'w') as f:
            f.write('签到成功' + req.text)
        aa = '签到成功'
    elif '已经签到' in req.text:
        with open('log/' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '.txt', 'w') as f:
            f.write('你已签到' + req.text)
        aa = '你已签到'
    else:
        with open('log/' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '.txt', 'w') as f:
            f.write('签到失败' + req.text)
        aa = '签到失败'

    r = requests.get('https://ctf.bugku.com/user/info/id/538.html', headers=headers)  # 个人信息页面
    r = r.text.replace('\n', '').replace('\r', '').replace(' ', '')

    mz = r'class="rounded-circle"alt="image"></figure><h5class="d-flexalign-items-centerjustify-content-center">(' \
         r'.*?)</h5><div> '
    mingz = re.compile(mz, re.S).findall(r)
    mingz = ''.join(mingz)

    zd = r'class="link-1">(.*?)</a></dd></dl><dl><dt>注册时间:</dt><dd>'
    zhand = re.compile(zd, re.S).findall(r)
    zhand = ''.join(zhand)

    jf = r'<h6>积分</h6><h4class="mb-0font-weight-bold">(.*?)</h4></div><divclass="col-6text-center"><h6>金币</h6>'
    jif = re.compile(jf, re.S).findall(r)
    jif = ''.join(jif)

    pm = r'<span>积分</span></div><divclass="col-4text-success"><h4class="font-weight-bold">(.*?)</h4><span>排名</span>'
    paim = re.compile(pm, re.S).findall(r)
    paim = ''.join(paim)

    jb = r'<span>排名</span></div><divclass="col-4text-warning"><h4class="font-weight-bold">(' \
         r'.*?)</h4><span>金币</span></div></div></div></div> '
    jinb = re.compile(jb, re.S).findall(r)
    jinb = ''.join(jinb)

    slnr = '用户名:' + mingz + '\r\n所属战队:' + zhand + '\r\n积分:' + jif + '\r\n排名:' + paim + '\r\n金币:' + jinb + '\r\n签到:' + aa
    print(slnr)
    # mailfs(slnr)


def main(git_cookie):
    s = requests.Session()
    global headers
    githubloginurl = "https://github.com" + \
                     s.get("https://ctf.bugku.com/login", headers=headers, verify=False, timeout=40).text.split(
                         'https://github.com')[1].rsplit('"')[0]
    # 填写你的github cookie 理论只需以下两段就行,并且以下两段的值是相同的。
    githubcookie = "user_session="+git_cookie+";" \
                   "__Host-user_session_same_site="+git_cookie+";"
    headers['cookie'] = githubcookie
    if (s.get("https://github.com/settings/profile", headers=headers, verify=False, timeout=30,
              allow_redirects=False).status_code != 200):
        print("github Cookie 失效")
    res = s.get(githubloginurl, headers=headers, verify=False, timeout=30)
    if "登录成功" in res.text:
        print("成功获取到Cookie")
        for bugkucookie in res.headers['Set-Cookie'].split(','):
            # print(bugkucookie)
            if ('PHPSESSID' in bugkucookie):
                checkin(s, bugkucookie)
                break
    elif "github.githubassets.com" in res.text:
        print("github 二次点击校验")
        form = res.text.split('<form action="')[1].split('<input type="hidden" name="scope"')[0]
        p = re.compile('name="(.*?)".*?value="(.*?)"')
        formdata = p.findall(form)
        data1 = {}
        for x in formdata:
            data1[x[0]] = x[1]
        data1['authorize'] = 1
        formurl = "https://github.com" + form.split('"')[0]
        headers = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                          "Chrome/87.0.4280.141 Safari/537.36",
            "cookie": githubcookie
        }

        res = s.post(formurl, data=data1, headers=headers, verify=False, timeout=30)
        if "登录成功" in res.text:
            print("成功获取到Cookie")
            for bugkucookie in res.headers['Set-Cookie'].split(','):
                print(bugkucookie)
                if 'PHPSESSID' in bugkucookie:
                    checkin(s, bugkucookie)
                    break
        else:
            print("签到失败!")
            # mailfs('签到失败!')
    else:
        print("签到失败!")
        # mailfs('签到失败!)


if __name__ == '__main__':
    git_cookie = 'github的cookie'
    main(git_cookie)

建议脚本运行在国内,因为bugku貌似把国外屏蔽了。但是GitHub在国内经常被墙,需要自定义一下host,或者用github的镜像站,

脚本中需要设置github的cookie,如果需要邮箱提醒的话,需要设置收件人和发件人(默认已注释)。

  • 获取GitHub的cookie方法:

image-20210423214747492.png

  • qq邮箱获取授权码:

image-20210423215214743.png
进入设置-账户,开启POP3/SMTP服务,输入手机令牌或手机验证码,获得授权码,填入到代码中

最后修改:2021 年 06 月 04 日 09 : 44 AM
如果觉得我的文章对你有用,请随意赞赏