1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
|
import requests import ddddocr from bs4 import BeautifulSoup import hashlib import base64 from io import BytesIO from PIL import Image from Crypto.Cipher import DES from Crypto.Util.Padding import pad import time import log from config import *
def md5_encrypt(text: str) -> str: """ MD5加密 :param text: 待加密字符串 :return: 加密后字符串 """ md5 = hashlib.md5() md5.update(text.encode('utf-8')) encrypted_text = md5.hexdigest() return encrypted_text
def des_ecb_encrypt(data: bytes, key: bytes) -> str: """ DES ECB加密 :param data: 待加密字节串 :param key: 密钥字节串 :return: 加密后hex编码的字符串 """ cipher = DES.new(key, DES.MODE_ECB) padded_data = pad(data, DES.block_size) encrypted_data = cipher.encrypt(padded_data).hex() return encrypted_data
def base64_to_image(base64_str: str) -> bytes: """ 将base64字符串转换为图像字节串 :param base64_str: base64字符串 :return: """ image_data = base64.b64decode(base64_str.split(',')[1])
image = Image.open(BytesIO(image_data)) image = image.convert('L')
img_byte_arr = BytesIO() image.save(img_byte_arr, format='PNG') img_byte_arr = img_byte_arr.getvalue() return img_byte_arr
def img_to_code(img: bytes) -> str: """ 使用ddddocr识别图片验证码 :param img: :return: """ ocr = ddddocr.DdddOcr(show_ad=False) ocr.set_ranges(6) code = ocr.classification(img) return code
class NSSCTF: def login(self, username: str, password: str) -> str: """ NSSCTF登录 :param username: 用户名 :param password: 密码 :return: Token """ if not username or not password: log.error("NSSCTF: 账户或密码不能为空") return ""
url = "https://www.nssctf.cn/api/user/login/"
post_data = { "username": username, "password": password }
try: res = requests.post(url, json=post_data) log.debug(f"NSSCTF: 登录完成,响应:{res.text}") except Exception as err: log.error(f"NSSCTF: 网络链接出错:{err}") return ""
code = res.json()["code"] if code == 200: token = res.json()["data"]["token"] log.info(f"NSSCTF: 登录成功!Token:{token}") return token else: if code == 201: log.error("NSSCTF: 登录失败!账户或密码错误!") else: log.error(f"NSSCTF: 登录失败!错误码:{code}") return ""
def sign_in(self, token: str) -> bool: """ NSSCTF签到 :param token: Token :return: 是否签到成功 """ if token == "": log.error("NSSCTF: Token不能为空") return False
url = "https://www.nssctf.cn/" headers = { "Cookie": f"token={token}" }
requests.get(url, headers=headers)
res = self.get_person_information(token) if res["code"] != 200: log.error("NSSCTF: 未登录,签到失败!") return False
coin = res["data"]["coin"] log.info(f"NSSCTF: 今日已签到,金币余额: {coin}")
return True
def get_person_information(self, token: str) -> dict: """ 获取个人信息 :param token: Token :return: 个人信息 """ url = "https://www.nssctf.cn/api/user/info/opt/setting/" headers = { "Cookie": f"token={token}" }
res = requests.get(url, headers=headers) log.debug(f"NSSCTF: 获取个人信息完成,响应:{res.text}")
code = res.json()["code"] if code == 200: log.info(f"NSSCTF: 获取个人信息成功!") else: if code == 402: log.error("NSSCTF: 获取个人信息失败!无效的Token") else: log.error(f"NSSCTF: 获取个人信息失败!错误码: {code}")
return res.json()
class Bugku: def login(self, username: str, password: str) -> str: """ Bugku登录 :param username: 用户名 :param password: 密码 :return: PHPSESSID """ if not username or not password: log.error("Bugku: 账户或密码不能为空") return ""
url = "https://ctf.bugku.com/login/check.html"
flag = 0 r_session = requests.session()
while flag < retry_limit: flag += 1 post_data = { "username": username, "password": password, "vcode": self.classification(r_session), "autologin": "0" }
headers = { "X-Requested-With": "XMLHttpRequest" }
try: res = r_session.post(url, headers=headers, data=post_data) except Exception as err: log.error(f"Bugku: 网络链接出错:{err}") return ""
log.debug(f"Bugku: 登录返回结果:{res.text}")
if res.json()["code"] == 1: PHPSESSID = r_session.cookies.get('PHPSESSID') log.info(f"Bugku: 【第{flag}次尝试】登录成功,PHPSESSID: {PHPSESSID}") return PHPSESSID else: msg = res.json()['msg'] log.error(f"Bugku: 【第{flag}次尝试】登录失败!{msg}") if "验证码" not in msg: return ""
log.error("Bugku: 超过最大尝试上限,登录失败!") return ""
def classification(self, r_session: requests.Session = None) -> str: """ 获取并识别验证码 :param r_session: :return: 识别的验证码 """ if r_session is None: r_session = requests.session()
url_captcha = "https://ctf.bugku.com/captcha.html"
try: res = r_session.get(url_captcha) except Exception as err: log.error(f"Bugku: 网络链接出错:{err}") return ""
code = img_to_code(res.content) log.debug(f"bugku: 识别登录验证码: {code}") return code
def sign_in(self, PHPSESSID: str) -> bool: """ Bugku签到 :param PHPSESSID: :return: 是否签到成功 """
url = "https://ctf.bugku.com/user/checkin"
headers = { "X-Requested-With": "XMLHttpRequest", "Cookie": f"PHPSESSID={PHPSESSID};" }
start_coin = self.get_coin(PHPSESSID)
try: res = requests.get(url, headers=headers) except Exception as err: log.error(f"Bugku: 网络链接出错:{err}") return False
final_coin = self.get_coin(PHPSESSID)
if res.json()["code"] == 1: log.info(f"Bugku: 签到成功!金币余额: {start_coin}->{final_coin}") return True else: if "签到过" in res.json()['msg']: log.info(f"Bugku: 今日已签到,金币余额: {final_coin}") return True else: log.error(f"Bugku: 签到失败!原因:{res.json()['msg']}") return False
def get_coin(self, PHPSESSID: str) -> int: """ 获取当前金币数 :param PHPSESSID: :return: 当前金币余额 """ url = "https://ctf.bugku.com/user/recharge.html" headers = { "Cookie": f"PHPSESSID={PHPSESSID};" }
try: res = requests.get(url, headers=headers) except Exception as err: log.error(f"Bugku: 网络链接出错:{err}") return -1
try: soup = BeautifulSoup(res.text, "html.parser") coin = int(soup.find("span", class_="alert-link text-warning").text) except Exception as e: log.error(f"Bugku: 数据处理失败: {e}") return -1
return coin
class CTFHub: def login(self, username: str, password: str) -> str: """ CTFHub登录 :param username: 用户名 :param password: 密码 :return: """
if not username or not password: log.error("CTFHub: 账户或密码不能为空") return ""
cookie = self.get_base_cookie()
url = "https://api.ctfhub.com/User_API/User/Login"
flag = 0 while flag < retry_limit: flag += 1
headers = { "Authorization": "ctfhub_sessid="+cookie }
post_json = { "account": username, "captcha": self.classification(cookie), "password": md5_encrypt(password) }
try: res = requests.post(url, headers=headers, json=post_json).json() except Exception as err: log.error(f"CTFHub: 网络链接出错:{err}") return ""
if res.get("status", False): log.info(f"CTFHub: 【第{flag}次尝试】登录成功,Cookie: {cookie}") return cookie else: log.error(f"CTFHub: 【第{flag}次尝试】登录失败,原因:{res.get('msg')}")
log.error("CTFHub: 登录失败,超过最大重试次数!") return ""
def get_base_cookie(self) -> str: """ 获取基础cookie :return: cookie """ url = "https://api.ctfhub.com/User_API/Other/getCookie"
try: res = requests.get(url).json() except Exception as err: log.error(f"CTFHub: 网络链接出错:{err}") return ""
if res.get("status", False): cookie = res.get("data").get("cookie").replace("ctfhub_sessid=","") log.info(f"CTFHub: 获取Cookie成功,Cookie: {cookie}") return cookie else: log.error(f"CTFHub: 获取Cookie失败,原因:{res.get('msg')}") return ""
def classification(self, cookie: str) -> str: """ 验证码识别 :param cookie: :return: 识别出的验证码 """ url = "https://api.ctfhub.com/User_API/User/getCaptcha" headers = { "Authorization": "ctfhub_sessid="+cookie }
code = "" while len(code) != 4: try: res = requests.get(url, headers=headers).json() except Exception as err: log.error(f"CTFHub: 网络链接出错:{err}") return ""
if not res.get("status", False): log.error(f"CTFHub: 获取验证码失败,原因:{res.get('msg')}") return ""
b64_img = res.get("data").get("captcha") img = base64_to_image(b64_img)
code = img_to_code(img) log.debug(f"CTFHub: 识别验证码: {code}") return code
def get_person_information(self, cookie: str) -> dict: """ 获取个人信息 :param cookie: :return: 个人信息 """ url = "https://api.ctfhub.com/User_API/User/getUserinfo"
headers = { "Authorization": "ctfhub_sessid=" + cookie }
post_json = { "target": "self" }
res = requests.post(url, headers=headers, json=post_json).json()
if res.get("status", False): log.info("查询个人信息成功") log.debug(f"个人信息: {res.get('data')}") return res.get("data") else: log.error(f"查询个人信息失败,原因:{res.get('msg')}") return {}
def sign_in(self, cookie: str) -> bool: """ CTFHub签到 :param cookie: :return: 签到是否成功 """ url = "https://api.ctfhub.com/User_API/User/checkIn"
headers = { "Authorization": "ctfhub_sessid="+cookie }
start_coin = self.get_person_information(cookie).get("coin", "-1")
res = requests.get(url, headers=headers).json()
if res.get("status", False): final_coin = self.get_person_information(cookie).get("coin", "-1") log.info(f"CTFHub: 签到成功!金币余额: {start_coin}->{final_coin}") return True else: if "已经签到" in res.get("msg"): log.info(f"CTFHub: 今日已签到,金币余额: {start_coin}") return True log.error(f"CTFHub: 签到失败!原因:{res.get('msg')}") return False
class ADWorld: user_id = -1
def login(self, username: str, password: str) -> (str, str): """ 攻防世界登录 :param username: 用户名 :param password: 密码 :return: 用户ID,登录token """ url = "https://adworld.xctf.org.cn/api/login/"
headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" }
flag = 0 while flag < retry_limit: flag += 1 hashkey = self.get_hash_key() code = self.classification(hashkey)
json_data = { "username": username, "password": des_ecb_encrypt(password.encode("utf-8"), b'B13H016Y'), "hash_key": hashkey, "hash_code": code }
try: res = requests.post(url, headers=headers, json=json_data).json() except Exception as err: log.error(f"攻防世界: 网络链接出错:{err}") return "", ""
if res.get("code") == 0: jwt_token = res.get("data").get("access") user_id = res.get("data").get("id") log.info(f"攻防世界: 【第{flag}次尝试】登录成功, 用户id: {user_id}, jwtToken: {jwt_token}") log.debug(f"攻防世界: 登录信息: {res.get('data')}") return user_id, jwt_token else: log.error(f"攻防世界: 【第{flag}次尝试】登录失败,原因:{res.get('msg')}")
log.error("攻防世界: 登录失败!超过最大重试次数!") return "", ""
def get_hash_key(self) -> str: """ 获取随机验证码图片代码 :return: 验证码图片代码 """ url = "https://adworld.xctf.org.cn/api/images/"
headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" }
try: res = requests.get(url, headers=headers).json() except Exception as err: log.error(f"攻防世界: 网络链接出错:{err}") return ""
if res.get("code") == 0: hashkey = res.get("data").get("hashkey") log.info(f"攻防世界: 成功获取hashkey: {hashkey}") return hashkey else: log.error(f"攻防世界: 获取hash_key失败, 原因: {res.get('msg')}") return ""
def classification(self, hashkey: str) -> str: """ 识别验证码 :param hashkey: 验证码图片代码 :return: 识别出的验证码 """ headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0" }
code = "" while len(code) != 4: url = "https://adworld.xctf.org.cn/api/captcha/images/?image_code_id="+hashkey
try: res = requests.get(url, headers=headers) except Exception as err: log.error(f"攻防世界: 网络链接出错:{err}") return ""
code = img_to_code(res.content) log.debug(f"攻防世界: 识别验证码: {code}")
return code
def sign_in(self, user_id: str, jwt_token: str) -> bool: """ 攻防世界签到 :param user_id: 用户ID :param jwt_token: 登录Token :return: 是否签到成功 """ url = "https://adworld.xctf.org.cn/api/user_center/daily/checkin/create/" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0", "Authorization": f"Bearer {jwt_token}", }
start_coin = self.get_person_information(user_id, jwt_token).get("coin_number", -1)
res = requests.post(url, headers=headers).json() if res.get("code") == 0: final_coin = self.get_person_information(user_id, jwt_token).get("coin_number", -1) log.info(f"攻防世界: 签到成功!金币余额: {start_coin}->{final_coin}") return True else: if "已签到" in res.get("msg"): log.info(f"攻防世界: 今日已签到, 当前金币余额: {start_coin}") return True else: log.error(f"攻防世界: 签到失败!原因:{res.get('msg')}") return False
def get_person_information(self, user_id: str, jwt_token: str) -> dict: """ 获取个人信息 :param user_id: 用户ID :param jwt_token: 登录Token :return: 个人信息 """ url = f"https://adworld.xctf.org.cn/api/user_center/base/info/{user_id}/" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0", "Authorization": f"Bearer {jwt_token}", }
res = requests.get(url, headers=headers).json()
if res.get("code") == 0: log.info(f"攻防世界: 获取个人信息成功") log.debug(f"攻防世界: 个人信息: {res.get('data')}") return res.get("data") else: log.error(f"攻防世界: 获取个人信息失败!原因:{res.get('msg')}") return {}
class QSNCTF: def login(self, username, password) -> str: """ 青少年CTF训练平台登录 :param username: 用户名 :param password: 密码 :return: 登录凭证 """ url = "https://www.qsnctf.com/api/login"
flag = 0 while flag < retry_limit: flag += 1
post_json = { "username": username, "password": password, "captcha": self.classification(), "code": "02c9ad84-d17d-47e8-8a6f-a1228d2b81f9" }
try: res = requests.post(url, json=post_json).json() except Exception as err: log.error(f"青少年CTF练习平台: 网络链接出错:{err}") return ""
access = res.get("access", None) if access is not None: log.info(f"青少年CTF练习平台: 【第{flag}次尝试】登录成功! access: {access}") return access log.error(f"青少年CTF练习平台: 【第{flag}次尝试】登录失败, 原因: {res.get('detail', '未知原因')}")
log.error(f"青少年CTF练习平台: 登录失败,超过最大重试次数!") return ""
def classification(self) -> str: """ 识别验证码 :return: 识别出的验证码 """ code = "" while len(code) < 4: url = "https://www.qsnctf.com/api/captcha/02c9ad84-d17d-47e8-8a6f-a1228d2b81f9"
try: img = requests.get(url).content except Exception as err: log.error(f"青少年CTF练习平台: 网络链接出错:{err}") return ""
code = img_to_code(img) return code
def sign_in(self, access: str) -> bool: """ 青少年CTF练习平台签到 :param access: 登录凭证 :return: 是否签到成功 """ url = "https://www.qsnctf.com/api/api/sign_in"
headers = { "Authorization": f"Bearer {access}" }
start_coin = self.get_person_information(access).get("gold_coins", -1)
try: res = requests.post(url, headers=headers).json() except Exception as err: log.error(f"青少年CTF练习平台: 网络链接出错:{err}") return False
final_coin = self.get_person_information(access).get("gold_coins", -1)
msg = res.get("detail")
if "成功" in msg: log.info(f"青少年CTF练习平台: 签到成功!金币余额: {start_coin}->{final_coin}") return True elif "已经签到" in msg: log.info(f"青少年CTF练习平台: 今日已经签到!当前金币余额: {final_coin}") return True else: log.error(f"青少年CTF练习平台: 签到失败!原因: {msg}") return False
def get_person_information(self, access: str) -> dict: """ 获取个人信息 :param access: 登录凭证 :return: 个人信息 """ url = "https://www.qsnctf.com/api/profile"
headers = { "Authorization": f"Bearer {access}" }
try: res = requests.get(url, headers=headers).json() except Exception as err: log.error(f"青少年CTF练习平台: 网络链接出错:{err}") return {}
msg = res.get("detail", None) log.debug(f"青少年CTF练习平台: 获取到的个人信息: {res}")
if msg is not None: log.error(f"青少年CTF练习平台: 获取个人信息失败!原因: {msg}") return {}
return res
if not onnxruntime_warning: import onnxruntime onnxruntime.set_default_logger_severity(3)
if __name__ == "__main__": log.info("开始执行自动签到任务 (GitHub Actions模式)...")
print("-"*20+"\n"+"-"*20) if nss_username != "" and nss_password != "": log.info("NSSCTF: 开始签到") token = NSSCTF().login(nss_username, nss_password) NSSCTF().sign_in(token) log.info("NSSCTF: 签到操作结束") else: log.info("NSSCTF: 未配置账号密码,跳过")
print("-"*20+"\n"+"-"*20) if bugku_username != "" and bugku_password != "": log.info("Bugku: 开始签到") PHPSESSID = Bugku().login(bugku_username, bugku_password) Bugku().sign_in(PHPSESSID) log.info("Bugku: 签到操作结束") else: log.info("Bugku: 未配置账号密码,跳过")
print("-"*20+"\n"+"-"*20) if ctfhub_username != "" and ctfhub_password != "": log.info("CTFHub: 开始签到") cookie = CTFHub().login(ctfhub_username, ctfhub_password) CTFHub().sign_in(cookie) log.info("CTFHub: 签到操作结束") else: log.info("CTFHub: 未配置账号密码,跳过")
print("-"*20+"\n"+"-"*20) if adworld_username != "" and adworld_password != "": log.info("攻防世界: 开始签到") inf = ADWorld().login(adworld_username, adworld_password) ADWorld().sign_in(inf[0], inf[1]) log.info("攻防世界: 签到操作结束") else: log.info("攻防世界: 未配置账号密码,跳过")
print("-"*20+"\n"+"-"*20) if qsnctf_username != "" and qsnctf_password != "": log.info("青少年CTF练习平台: 开始签到") access = QSNCTF().login(qsnctf_username, qsnctf_password) QSNCTF().sign_in(access) log.info("青少年CTF练习平台: 签到操作结束") else: log.info("青少年CTF练习平台: 未配置账号密码,跳过")
log.info("所有签到任务执行完毕,退出脚本。")
|