From 64fc2a5e2a239ec588359897672fc49c0a4cab47 Mon Sep 17 00:00:00 2001 From: "James, Zonghan JI" Date: Fri, 20 Nov 2026 02:25:11 +0800 Subject: [PATCH] =?UTF-8?q?init=20=E6=A0=87=E9=A2=98=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E7=BB=84=E4=BB=B6=20aimedb=20=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GetUserPreviewApi.py | 28 +++++++++++++++++++++ UserLoginApi.py | 29 ++++++++++++++++++++++ config.py | 12 +++++++++ src/aimedb.py | 51 ++++++++++++++++++++++++++++++++++++++ src/cipher.py | 58 ++++++++++++++++++++++++++++++++++++++++++++ src/payload.py | 40 ++++++++++++++++++++++++++++++ src/title.py | 36 +++++++++++++++++++++++++++ 7 files changed, 254 insertions(+) create mode 100644 GetUserPreviewApi.py create mode 100644 UserLoginApi.py create mode 100644 config.py create mode 100644 src/aimedb.py create mode 100644 src/cipher.py create mode 100644 src/payload.py create mode 100644 src/title.py diff --git a/GetUserPreviewApi.py b/GetUserPreviewApi.py new file mode 100644 index 0000000..fb3ffde --- /dev/null +++ b/GetUserPreviewApi.py @@ -0,0 +1,28 @@ +import argparse + +from src.title import Title +from src import aimedb, payload + +def GetUserPreviewApi(): + parser = argparse.ArgumentParser(description="Title GetUserPreviewApi") + + parser.add_argument("--userId", type=int, required=True, help="userId") + parser.add_argument("--accessCode", type=str, required=True, help="AiME Access Code") + parser.add_argument("--serial", type=str, required=True, help="AiME Serial Mifare Only now") + + args = parser.parse_args() + + data = payload.GetUserPreview(args.userId) + + aimedb.CMD4(args.accessCode, args.serial) + + res = Title( + userId=args.userId, + ApiType="GetUserPreviewApi", + data = data + ) + + return res + +if __name__ == "__main__": + print(GetUserPreviewApi()) diff --git a/UserLoginApi.py b/UserLoginApi.py new file mode 100644 index 0000000..388c2fd --- /dev/null +++ b/UserLoginApi.py @@ -0,0 +1,29 @@ +import argparse + +from src.title import Title +from src import aimedb, payload + +def UserLoginApi(): + parser = argparse.ArgumentParser(description="Title UserLoginApi") + + parser.add_argument("--userId", type=int, required=True, help="userId") + parser.add_argument("--accessCode", type=str, required=True, help="AiME Access Code") + parser.add_argument("--serial", type=str, required=True, help="AiME Serial Mifare Only now") + parser.add_argument("--dateTime", type=int, required=True, help="时间戳") + + args = parser.parse_args() + + aimedb.CMD4(args.accessCode, args.serial) + + data = payload.UserLogin(args.userId, args.accessCode, args.dateTime) + + res = Title( + userId=args.userId, + ApiType="UserLoginApi", + data = data + ) + + return res + +if __name__ == "__main__": + print(UserLoginApi()) \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..7ba645a --- /dev/null +++ b/config.py @@ -0,0 +1,12 @@ +AesKey = "kO2MK]93m4bVTrB0AJBfW314EWKb22ca" +AesIV = "NNCJ]cRObKSMUI:U" +ObfuscateParam = "wB99DfTb" + +MaiEncoding = "1.65" +Endpoint = "mai2exp.sic-rd1.jp:42081" + +regionId = 534 +regionName = "Aomen" +placeId = 7850 +placeName = "YURAKUCHO" +clientId = "A63E01E0264" diff --git a/src/aimedb.py b/src/aimedb.py new file mode 100644 index 0000000..de3f10a --- /dev/null +++ b/src/aimedb.py @@ -0,0 +1,51 @@ +import socket +import re +from binascii import unhexlify, hexlify +from Crypto.Cipher import AES + +def CMD4(accessCode, serial): + key = b'Copyright(C)SEGA' + # https://sega.bsnk.me/allnet/aimedb/common/ + magic = "3ea1" + version = "2140" + command_id = "0400" # ID = 4 + length = "3000" # 48 + gameId = "534447410000" # SDGA + storeId = "bc310000" + keychip_ID = "413633453031453032363400" # A63E01E0264 + header = magic + version + command_id + length + "0000" + gameId + storeId + keychip_ID + + access_code = str(accessCode) + serial = str(serial) + company_code = "00" + firmware_version = "02" + + + plaintext_hex_stream = header + access_code + company_code + firmware_version + serial + + plaintext = unhexlify(plaintext_hex_stream) + + cipher = AES.new(key, AES.MODE_ECB) + encrypted_message = cipher.encrypt(plaintext) + + encrypted_hex_stream = hexlify(encrypted_message).decode('utf-8') + + server_address = ('aime.naominet.jp', 22345) + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + sock.connect(server_address) + sock.sendall(encrypted_message) + + response = sock.recv(1024) + + decrypted_response = cipher.decrypt(response) + + decrypted_hex_stream = hexlify(decrypted_response).decode('utf-8') + + match = re.search(r'[0-9a-f]{64}([0-9a-f]{6})', decrypted_hex_stream) + if match: + six_digit_code = match.group(1) + + rearranged_hex = six_digit_code[4:6] + six_digit_code[2:4] + six_digit_code[0:2] + + decimal_value = int(rearranged_hex, 16) + return decimal_value diff --git a/src/cipher.py b/src/cipher.py new file mode 100644 index 0000000..d98937d --- /dev/null +++ b/src/cipher.py @@ -0,0 +1,58 @@ +import base64 +import hashlib +import random + +from Crypto.Cipher import AES +from Crypto.Util.Padding import pad, unpad + +from config import AesKey, AesIV, ObfuscateParam + +class aes_pkcs7(object): + def __init__(self, key: str, iv: str): + self.key = key.encode('utf-8') + self.iv = iv.encode('utf-8') + self.mode = AES.MODE_CBC + + def encrypt(self, content: bytes) -> bytes: + cipher = AES.new(self.key, self.mode, self.iv) + content_padded = pad(content, AES.block_size) + encrypted_bytes = cipher.encrypt(content_padded) + return encrypted_bytes + + def decrypt(self, content): + cipher = AES.new(self.key, self.mode, self.iv) + decrypted_padded = cipher.decrypt(content) + decrypted = unpad(decrypted_padded, AES.block_size) + return decrypted + + def pkcs7unpadding(self, text): + length = len(text) + unpadding = ord(text[length - 1]) + return text[0:length - unpadding] + + def pkcs7padding(self, text): + bs = 16 + length = len(text) + bytes_length = len(text.encode('utf-8')) + padding_size = length if (bytes_length == length) else bytes_length + padding = bs - padding_size % bs + padding_text = chr(padding) * padding + return text + padding_text + +aes = aes_pkcs7(AesKey, AesIV) + +def ApiHashEncode(ApiType: str): + return hashlib.md5((ApiType+"MaimaiExp"+ObfuscateParam).encode()).hexdigest() + +def CalcRandom(): + max = 1037933 + num2 = random.randint(1, max) * 2069 + + num2 += 1024 # specialnum + num3 = 0 + for i in range(0, 32): + num3 <<= 1 + num3 += num2 % 2 + num2 >>= 1 + + return num3 \ No newline at end of file diff --git a/src/payload.py b/src/payload.py new file mode 100644 index 0000000..3ce3622 --- /dev/null +++ b/src/payload.py @@ -0,0 +1,40 @@ +from config import * + +def GetUserPreview(userId): + + data = { + "userId": userId, + "segaIdAuthKey":"" + } + + return data + +def UserLogin(userId, accessCode, dateTime): + + data = { + "userId": userId, + "accessCode": accessCode, + "regionId": regionId, + "placeId": placeId, + "clientId": clientId, + "dateTime": dateTime, + "loginDateTime": dateTime, + "isContinue": False, + "genericFlag": 0 + } + + return data + +def UserLogout(userId, accessCode, dateTime): + + data = { + "userId": userId, + "accessCode": accessCode, + "regionId": regionId, + "placeId": placeId, + "clientId": clientId, + "loginDateTime": dateTime, + "type": 4 + } + + return data \ No newline at end of file diff --git a/src/title.py b/src/title.py new file mode 100644 index 0000000..d3f3948 --- /dev/null +++ b/src/title.py @@ -0,0 +1,36 @@ +import httpx +import zlib +import json + +from .cipher import ApiHashEncode, aes + +from config import MaiEncoding, Endpoint + +def Title(userId: int, ApiType: str, data: dict): + + ApiTypeHash = ApiHashEncode(ApiType) + + headers = { + "User-Agent": f"{ApiTypeHash}#{userId}", + "Content-Type": "application/json", + "Mai-Encoding": MaiEncoding, + "Accept-Encoding": "", + "Charset": "UTF-8", + "Content-Encoding": "deflate" + } + + data = bytes(json.dumps(data, ensure_ascii=False), encoding="utf-8") + CompressedData = zlib.compress(data) + AESEncrptedData = aes.encrypt(CompressedData) + + TitleResponse = httpx.post( + f"https://{Endpoint}/Maimai2Servlet/{ApiTypeHash}", + headers = headers, + data = AESEncrptedData, + verify = True + ) + + CompressedData = aes.decrypt(TitleResponse.content) + data = zlib.decompress(CompressedData).decode('utf-8') + + return data \ No newline at end of file