From a75c98b78a0c31b4506c4ba34f869f53b9a2016d Mon Sep 17 00:00:00 2001 From: dhfhfk Date: Wed, 6 Mar 2024 14:40:44 +0900 Subject: [PATCH 1/2] fix: fix KTX login problem --- ktrains/korail/korail.py | 34 ++++++++++++++++++++++++++++++++-- requirements.txt | 3 ++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/ktrains/korail/korail.py b/ktrains/korail/korail.py index 882d0f5..69bd3d7 100644 --- a/ktrains/korail/korail.py +++ b/ktrains/korail/korail.py @@ -9,11 +9,14 @@ import itertools import re import sys +import base64 from datetime import datetime, timedelta, timezone from pprint import pprint import requests from six import with_metaclass +from Crypto.Util.Padding import pad +from Crypto.Cipher import AES try: # noinspection PyPackageRequirements @@ -56,6 +59,8 @@ def _python3(): KORAIL_PAYMENT = "%s/ebizmw/PrdPkgMainList.do" % KORAIL_DOMAIN KORAIL_PAYMENT_VOUCHER = "%s/ebizmw/PrdPkgBoucherView.do" % KORAIL_DOMAIN +KORAIL_CODE = "%s.common.code.do" % KORAIL_MOBILE + DEFAULT_USER_AGENT = "Dalvik/2.1.0 (Linux; U; Android 5.1.1; Nexus 4 Build/LMY48T)" @@ -564,6 +569,30 @@ def __init__(self, korail_id, korail_pw, auto_login=True, want_feedback=False): if auto_login: self.login(korail_id, korail_pw) + def __enc_password(self, password): + url = KORAIL_CODE + data = { + 'code': "app.login.cphd" + } + + r = self._session.post(url, data=data) + j = json.loads(r.text) + + if j['strResult'] == 'SUCC' and j.get('app.login.cphd') is not None: + self._idx = j['app.login.cphd']['idx'] + key = j['app.login.cphd']['key'] + + encrypt_key = key.encode(encoding='utf-8', errors='strict') + iv = key[:16].encode(encoding='utf-8', errors='strict') + cipher = AES.new(encrypt_key, AES.MODE_CBC, iv) + + padded_data = pad(password.encode("utf-8"), AES.block_size) + + return base64.b64encode(base64.b64encode(cipher.encrypt(padded_data))).decode("utf-8") + else: + return False + + def login(self, korail_id=None, korail_pw=None): """Login to Korail server. :param korail_id : `Korail membership number` or `phone number` or `email` @@ -611,14 +640,15 @@ def login(self, korail_id=None, korail_pw=None): url = KORAIL_LOGIN data = { "Device": self._device, - "Version": "150718001", # HACK + "Version": "231231001", # HACK #'Version': self._version, # 2 : for membership number, # 4 : for phone number, # 5 : for email, "txtInputFlg": txt_input_flg, "txtMemberNo": korail_id, - "txtPwd": korail_pw, + 'txtPwd': self.__enc_password(korail_pw), + 'idx': self._idx } r = self._session.post(url, data=data) diff --git a/requirements.txt b/requirements.txt index ad0701e..040a423 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ streamlit-aggrid pandas chime datetime -pytz \ No newline at end of file +pytz +PyCryptodome \ No newline at end of file From f6c46e030b2525a81beda535d7bbf986903a290e Mon Sep 17 00:00:00 2001 From: dhfhfk Date: Thu, 7 Mar 2024 10:55:45 +0900 Subject: [PATCH 2/2] fix: missing idx variable declaration --- ktrains/korail/korail.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ktrains/korail/korail.py b/ktrains/korail/korail.py index 69bd3d7..8b72495 100644 --- a/ktrains/korail/korail.py +++ b/ktrains/korail/korail.py @@ -556,6 +556,8 @@ class Korail(object): _version = "190617001" _key = "korail1234567890" + _idx = None + membership_number = None name = None email = None