python检测ipa证书过期时间

我们打出的ipa包,对应的证书和mobileprovision文件是会过期的
过期了之后,ipa就会无法运行或安装
为了方便知道打出的ipa的过期时间,下面提供一个python脚本,方便检测
需要在mac上运行,把你的ipa放到pyhton脚本所在的目录
在这里插入图片描述

python脚本如下

# check_timeout.py
import os
import shutil
import re
import subprocess
import re
import time
import calendar

def popen_cmd(cmd):
    GBK = 'gbk'
    UTF8 = 'utf-8'

    current_encoding = GBK
    popen = subprocess.Popen(cmd, shell = True,
                             stdout = subprocess.PIPE,
                             stderr = subprocess.PIPE,
                             bufsize=1)
    out,err = popen.communicate()
    #print(out)
    #print(err)
    #print('returncode:' + str(popen.returncode))
    return out

def unzip_ipa(ipa_name):
    os.system('unzip -q ./' + ipa_name)
    os.system("codesign -d --extract-certificates Payload/*.app")

def popen_openssl():
    out = popen_cmd('openssl x509 -inform DER -in codesign0 -noout -nameopt -oneline -dates')
    match = re.search('notAfter=(.*)', out)
    # Jan  9 02:31:00 2020 GMT
    result = match.group(1)
    match = re.search('(\w*)\s*(\w*)\s*(\w*):(\w*):(\w*)\s*(\w*)\s*GMT', result) 
    m = month_str_to_num(match.group(1))
    d = match.group(2)
    H = match.group(3)
    M = match.group(4)
    S = match.group(5)
    Y = match.group(6)
    t = '%s-%s-%s %s:%s:%s'%(Y,m,d,H,M,S)
    #print(t)
    return str_to_timestamp(t), t
    
def month_str_to_num(month_str):
    return list(calendar.month_abbr).index(month_str)
    
# %Y-%m-%d %H:%M:%S
def str_to_timestamp(str):
   time_array = time.strptime(str, "%Y-%m-%d %H:%M:%S")
   timeStamp = int(time.mktime(time_array))
   return timeStamp
    
   
def delete_other_file():
    os.remove("codesign0")
    os.remove("codesign1")
    os.remove("codesign2")
    shutil.rmtree("Payload") 

def cat_mobileprovision(app_name):
    out = popen_cmd('cat Payload/%s/embedded.mobileprovision'%app_name)
    match = re.search('<key>ExpirationDate</key>\\n\s*<date>(.*)</date>', out)
    result = match.group(1)
    result = result.replace('T', ' ')
    result = result.replace('Z', '')
    #print(result)
    return str_to_timestamp(result), result

def get_ipa():
    for f in os.listdir('.'):
        if f.endswith('.ipa'):
            return f

def get_payload_app():
    for d in os.listdir('./Payload'):
        if d.endswith('.app'):
            return d
			
if __name__ == '__main__':
    unzip_ipa(get_ipa())
    stamp1, t1 = popen_openssl()
    stamp2, t2 = cat_mobileprovision(get_payload_app())
    delete_other_file()
    if stamp1 < stamp2:
        print('timeout:' + t1)
    else:
        print('timeout: ' + t2)

运行效果

在这里插入图片描述


补充

将GMT时间格式的字符串转换成datetime类型

dd = "Fri Nov 09 2018 14:41:35 GMT+0800 (CST)"
GMT_FORMAT = '%a %b %d %Y %H:%M:%S GMT+0800 (CST)'
print(datetime.strptime(dd, GMT_FORMAT))
 
output:
2018-11-09 14:41:35

注意:GMT_FORMAT的格式要与索要转化的字符串相对应。
扩展:python的格式转化

%a 本地的星期缩写
%A 本地的星期全称
%b 本地的月份缩写
%B 本地的月份全称
%c 本地的合适的日期和时间表示形式
%d 月份中的第几天,类型为decimal number(10进制数字),范围[01,31]
%f 微秒,类型为decimal number,范围[0,999999],Python 2.6新增
%H 小时(24进制),类型为decimal number,范围[00,23]
%I 小时(12进制),类型为decimal number,范围[01,12]
%j 一年中的第几天,类型为decimal number,范围[001,366]
%m 月份,类型为decimal number,范围[01,12]
%M 分钟,类型为decimal number,范围[00,59]
%p 本地的上午或下午的表示(AM或PM),只当设置为%I(12进制)时才有效
%S 秒钟,类型为decimal number,范围[00,61](60和61是为了处理闰秒)
%U 一年中的第几周(以星期日为一周的开始),类型为decimal number,范围[00,53]。在度过新年时,直到一周的全部7天都在该年中时,才计算为第0周。只当指定了年份才有效。
%w 星期,类型为decimal number,范围[0,6],0为星期日
%W 一年中的第几周(以星期一为一周的开始),类型为decimal number,范围[00,53]。在度过新年时,直到一周的全部7天都在该年中时,才计算为第0周。只当指定了年份才有效。
%x 本地的合适的日期表示形式
%X 本地的合适的时间表示形式
%y 去掉世纪的年份数,类型为decimal number,范围[00,99]
%Y 带有世纪的年份数,类型为decimal number
%Z 时区名字(不存在时区时为空)
%% 代表转义的"%"字符
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林新发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值