众所周知,为了保障商用密码的安全性,国家商用密码管理办公室制定了一系列密码标准,包括SM1(SCB2)、SM2、SM3、SM4、SM7、SM9、祖冲之密码算法(ZUC)那等等。
SM1、SM4、SM7、祖冲之密码(ZUC)是对称算法;SM2、SM9是非对称算法;SM3是哈希算法。其中SM1、SM7算法不公开,调用该算法时,需要通过加密芯片的接口进行调用;
声明
本文章中所有内容仅供学习交流使用,不用于其他任何目的,不提供完整代码,抓包内容、敏感网址、数据接口等均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关!
本文章未经许可禁止转载,禁止任何修改后二次传播,擅自使用本文讲解的技术而导致的任何意外,作者均不负责,若有侵权,请联系作者立即删除,请各位自觉遵守相关法律法规。
事实上从 2010 年开始,我国国家密码管理局就已经开始陆续发布了一系列国产加密算法,这其中就包括 SM1、SM2、SM3 、SM4、SM7、SM9、ZUC(祖冲之加密算法)等,SM 代表商密,即商业密码,是指用于商业的、不涉及国家秘密的密码技术。SM1 和 SM7的算法不公开,其余算法都已成为ISO/IEC 国际标准。
在这些国产加密算法中,SM2、SM3、SM4 三种加密算法是比较常见的,在爬取部分网站时,也可能会遇到这些算法,所以作为爬虫工程师是有必要了解一下这些算法的,如下图所示某网站就使用了 SM2 和 SM4 加密算法:
算法名称 | 算法类别 | 应用领域 | 特点 |
---|---|---|---|
SM1 | 对称(分组)加密算法 | 芯片 | 分组长度、密钥长度均为 128 比特 |
SM2 | 非对称(基于椭圆曲线 ECC)加密算法 | 数据加密 | ECC 椭圆曲线密码机制 256 位,相比 RSA 处理速度快,消耗更少 |
SM3 | 散列(hash)函数算法 | 完整性校验 | 安全性及效率与 SHA-256 相当,压缩函数更复杂 |
SM4 | 对称(分组)加密算法 | 数据加密和局域网产品 | 分组长度、密钥长度均为 128 比特,计算轮数多 |
SM7 | 对称(分组)加密算法 | 非接触式 IC 卡 | 分组长度、密钥长度均为 128 比特 |
SM9 | 标识加密算法(IBE) | 端对端离线安全通讯 | 加密强度等同于 3072 位密钥的 RSA 加密算法 |
ZUC | 对称(序列)加密算法 | 移动通信 4G 网络 | 流密码 |
SM4 | DES | AES | ||
---|---|---|---|---|
计算轮数 | 32 | 16(3DES 为 16*3) | 10/12/14 | |
密码部件 | S 盒、非线性变换、线性变换、合成变换 | 标准算术和逻辑运算、先替换后置换,不含线性变换 | S 盒、行移位变换、列混合变换、圈密钥加变换(AddRoundKey) |
以 SM2算法为例,实现如下(其他算法和详细用法可参考其官方文档):https://openstd.samr.gov.cn/bzgk/gb/
SM2加密(encrypt)和解密(decrypt):
// npm install sm-crypto --save
const sm2 = require('sm-crypto').sm2
// 1 - C1C3C2,0 - C1C2C3,默认为1 算法模式
const cipherMode = 1
// 获取密钥对
let keypair = sm2.generateKeyPairHex()
let publicKey = keypair.publicKey // 公钥
let privateKey = keypair.privateKey // 私钥
let msgString = "this is the data to be encrypted"
let encryptData = sm2.doEncrypt(msgString, publicKey, cipherMode) // 加密结果
let decryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode) // 解密结果
console.log("encryptData: ", encryptData)
console.log("decryptData: ", decryptData)
若要使用 SM9 算法,可下载 gmssl-python 源码手动安装。
pip install gmssl
示例如下:
from gmssl import sm2
# 16 进制的公钥和私钥
private_key = '00B9AB0B828FF68872F21A837FC303668428DEA11DCD1B24429D0C99E24EED83D5'
public_key = 'B9C9A6E04E9C91F7BA880429273747D7EF5DDEB0BB2FF6317EB00BEF331A83081A6994B8993F3F5D6EADDDB81872266C87C018FB4162F5AF347B483E24620207'
sm2_crypt = sm2.CryptSM2(public_key=public_key, private_key=private_key)
# 待加密数据和加密后数据为 bytes 类型
data = b"this is the data to be encrypted"
enc_data = sm2_crypt.encrypt(data)
dec_data = sm2_crypt.decrypt(enc_data)
print('enc_data: ', enc_data.hex())
print('dec_data: ', dec_data)
先发包,对各类参数进行分析
请求头参数中,初步推测有变化的是以下几个
X-Tif-Nonce: k3WPg33U
X-Tif-Paasid: undefined 不需要进行分析
X-Tif-Signature: 0cd52866692085e798ef0ae26c05f79a8761589e08fa0cd719787803ce1f64c2
X-Tif-Timestamp: 1691201261 初步推测为时间戳
X-Tingyun: c=B|4Nl_NnGbjwY;x=0b6f424553e94bc2
即请求标头部分分析三个参数
载荷部分
encData: "3DFBCA4667B978F639BB23B95DCE4CC7DCE52C499A10A562377DE69F5F324190CCD20943B4DAE96380B41164D761DE9742C84A985FE3BABC31CB352556BB87C9C1495DB24A29AB6BC3A85AB7FCA00F338EE714ACFC4C924F01CF575098AEF16755EE6C2B00989F3CBDACE061021CBD579720F6A86E9EFCBA77082625D345D5DFD130FEBBE62DBFF03225CA796232EA15C36959880C2647559E3C97B56FD4F10F"
encType: "SM4"
signData: "C9MFlzW3KIjkULauoIGe7ogtospAMs3qODpvT1ZuLDzL36ldwW7nlHSnB4ZFabX3thjoGaNQV/nMJ5gHnOXDMg=="
signType: "SM2"
timestamp: 1691201261 时间戳
即载荷部分有两个参数进行分析
预览部分(返回的参数部分)
encData: "943195DDA5E5337E08E0A527071CC637E0074CBD73D293A3643B2199C79561872E19ABE7BB2600D23E9CDB14C1A75194C1D3C735D2DCC50E297311A423B72BF0AE4781F770874C1AC923AD26618311F1E72934E0FFBB639208EEADCEC171768689F9BB657ADD7ABB9CA217AA1764EDF845FCD975D48DDA6ADBCA561CDB3629A9C813752E572A9CA0464E7D0E608A18559D3F9A6A7761254178DAB215456B459C6CDC054BD1257D8B5533060F9D258878D617219CA22161E40C854E96EBDC1BA718EC6F9A44A974D9B23C10556F766BB7200162303EC72ABC55B1B0A9F40A4ECB254AE970E8A34298B214FAF895E5E814E226A5F27791FBF0973885F9F4DD107B99525EE7B77CF1E63FB487A3C65AC27A7AB5F7D52905E2F1B63C146974BFA06D5051342E44DB7D4356D17EC48FFAEA937C4BFD5C94F18DA762BDF9D072FB629728D71B26E974C1908D83047A8BB39D3061191527ADF51DF0C45D6F56511907C5EE94D7C6989531A8243821EDECB172C8FF05AAACC321250E30F2C1E52F2FB440C5199CC288802FE0FCCA1984C8D5EF2D132B2D260D528BDF619D4E0C88028761C5985746F20552902911B4C33ABFC61A50182FEA36036B5B0FEE63175041C34054D9A236571521938B7DC02F16CBCC08C4F25C37D9B6BF97F0104C091469FCAD181C27AF83702DCB2B3B1917B9260122B63EF8EAE80098E69E6F04084372720608B0244EBCEB0A69F00579D6E7EDD32064A84F33C441433EE8456447F01BC86AD8808027665DC09B81DD86C4FBAF26D4AD0D3D99C54A971331C1310095F1798AEE62D559BB248E6D7E02C53CC4DABFD007A7BA5418EFA266D2DFFF0EBE2B8A4117B8F04489B3F4B9DCBECA674EF2B1DE5E4A4EB152907743EE47A7D669003FC12316FA22C614F2DC747F1240F9DBEC14D6CFD028283C5A867FE15B743B7F6C50C2FCE4AAF07C7990A16220238DC937EA2EBEAA2C6940513330B4D301FCD01E4636977C64E66FFD66898ADBDB99D5C1D0E13453881245615C917D409D2D01E9595152E684489A5B7A8D4201F7FCC61030212B0D8FFD69979FA03284896CE2A732DCB35A83E4C9E4F33E9681D786F3935E031BC9317C526090AD813BFB5FBA35EC57DBBBB445A45B34974D9E91BAEC7A480FA236BDD4190908405241EB99B4000E31449262E04001C8E2B493DD51254AFE65D87627E132D6E8281823416F1B469003EC8091E37D96697A8F1D788B7BC16CE3BE88F71C33C5A5C61E041276C94CD9B7A00E4011E692BB95F2C02CD49FF2EED410029204A995BE1850CF211035F8D2AC67FB3B38DC76925B4A1E72442195B34F1ED065348B94C16C969E67D13AACE45E7ABF406AA2EA9881B6EB91F042E50E7C50687C9B138508F2B75025FB0E428D267AD15424E7F533655EE31A46563815F30A7E354C6355D0F30A4C3BFDE087403310E50966DCEDB32A748E6D6232E473CFD101F63B32702D90214315424F207A8C6D176BD27E3C089E3D50E3B3C0E04C43EBB86FE6068C4E67691F287713EC7ADC5CBB57ED51669FB4BA566C0AE68E3FF046122897DC282D3A64AA2D9FBB6C7FA4384186A41C31C94BAE0029B2C932B824E540918807252F0FF6D4D741C14BF47CE0B648858ADC465332E5D5D024525EC2F9191071BED9CE58C3B46B0259015C9D3F9A6A7761254178DAB215456B459CBBDB05FA904534ABB9D5A1B30F507C24DE6B58EFA9FA869D79F267FA8D2454E199A6A93A947845803634A2E3B060BF92200162303EC72ABC55B1B0A9F40A4ECB254AE970E8A34298B214FAF895E5E814E226A5F27791FBF0973885F9F4DD107B99525EE7B77CF1E63FB487A3C65AC27A441A05D21BB957FFA38A265FD050118D5BA29B4C4F49709B4A07A4001D758B734E5D35B3CEB6E62A39FC81F9E0D199B0999AC4DE8F9057EB4E13F481EE2F4C58B68E1B07E417C353AEA6DCA44F107F016299000C504CAD202DC09DCECAB8A0558343573EB5CC99C8C6614D123130042CD968ECA31897485877E1DB628267D3ECB37A12ADB00652DCC798C8838C60FBEB4240D64C8784217F395FA129834F0383226679D62BF8FC7277047EF88561BAA5602C1EEC6B17E9F8F057757A8A9804DDF2429A5D0D32C0660DCF84D6E1817F98A7500384500B8ACC926C254DE9D455707813DA9D8644C332964B12448909602269D360821F227BCD0AC25404C1763DF0E22EB1A862C8B1FB066492C9413448D1444900A7F1DC60803B5D392B0054FC756724475215C9965949E0030942A349C7EC529D0E0FEB988B0683CDDFD3344F1D87056E641E4FC48A95148E2FF83DD8ED175918CCBA3EEBE058C994E75E2C19151C62A19050CB7FA27CDFE2DDCE2777174FDC2458C4772E772BD27888E1CE37DA1B0F536E1BDCA8B668766E0ACFF222B2B9C6CF6D56AFE390332D7A848A37FF9156CD1D7B0EAC2627586E5717014DCE9F6E34E90C6FAB05D1D543C848732A0AB4448C11F1ACBB5B21F37AEA683E0A3B62907228575692F06822201465AEF3F8448C061A83BE8A5BA89EEA3D2CB66EC4090D84CF6CC19165511DC5165D9C8CA80226D94DF9A9A8DED38E5BD02F833FE880778B8091EED9308E28D7C31571DF912E57CEF50FD58D38AC57E5DB8AA0DCD358E4C0D26BA9BA72CD82924468E424E68275A5EBF8ADDD0B19C73ED56A810011A868D20673C76CBC012D1AF4D1DB0E385D9A230E7AA083FAF5A98E6A8793F5D09CA6857200B5FD24C93A841F6A343420EB441DC0FBA6953D0EF058369B2D929C23A1BDE4308E381CFD2F1BD4FEA2EADED0C6AF3FA4C82FB7DF54A83389D6958A0EAB58431A227CFB94A9BF32D50688980B27B31CC1288AF4C0BDFC030700EF7F8269C418CE15D92C9ADCCDBB1D4168C22565D0EF9ED3C4600562BCC3B3C3725566F5D339E303626DE9533FE12A3830C04C5A8EDC869F4740AC6215C363714AFD3F5EA62FFE81A56DB7B4FCE6D08A383A01A9352F8960F0E06F7B44F5CE476B3D3B8C4B64F8A6DA990F25DF95D73CA2A226B9F526302D44A82CC44957644E7C0282AB01663B2E9C35650E74AD893EF04D2D08B0244EBCEB0A69F00579D6E7EDD32047FF559EA862DD8C62FF724FFE6975EDD8808027665DC09B81DD86C4FBAF26D44AC0FD61AD5503F744E927D6D9483E51EE62D559BB248E6D7E02C53CC4DABFD007A7BA5418EFA266D2DFFF0EBE2B8A4117B8F04489B3F4B9DCBECA674EF2B1DE5E4A4EB152907743EE47A7D669003FC1F4957FDD25D560C7B5A96DC8C23BBABCF261DEBF41C8A5A497B8060B8D7AFAA89B04B78C1661BC8FC97D36D892D828B05754A1AE8D9E21951C8596B58B32780178469869C57834E0D22BB36FCB3747D38B1278ACB5974C457AB3C04A2D1EF4E162B6587A4CC293BEE09981FF5EFD99D78717239EDBF3BB023F3D7BCB6DBDE643E9E9DFCC3555453A28D11FD63B559614FDAB509CE5C10DB60B44C94C6C982EFE4061CADCA3D2B15BC97FB1190936E8FD156D903ADB5FA39E28CAC25D9DAE38B602F379B328BD13C76C9EC57205E6F7879084AC0681A2FAA432B0428504BB98361E5F69DB0DEE26C9AEC9CE8B583F2482CE1EE612EB5DE8D219DB07E307F521C2D03D1D4E2FB7DF972493679EF395559B3C244A128BF389B9EBD3AAEEE4592D1DBCFDCF8622ABD4CD5CC8BE9BBF5F5AA30A0A0F9D54A0B9AF7CDE2719B43D519AD36A6958936295584A289B2EF360529761191527ADF51DF0C45D6F56511907C50DD44A58A9A6DD0857FA972B48A6CCA64685B49547648EC889C7CE957407118BC988D80A53F32EC8DCD180248DEDD1052C43A8E9C39CF03050091BD7402B719786FAB524D9F3A915102D44F9BB819B05B6CB8D87F0DF2413E05EA4E0186D0E43ED56BCCD50D34398A06A7B7325741DF09D746F9B0D04CADB2E448FB48352670839EF7B27C000252E87510FAA194D7C5F5FAC709C20EDE563E9C04289771F62A42300FBE8F7FD527D9D837359909CE27365D87627E132D6E8281823416F1B469003EC8091E37D96697A8F1D788B7BC16C871F8E7468139D6062EA440CC252088605EA8E085579280DF3487A09FF4DBEB092A1F65FE0A0CFC022375844965A7702878F859940DE7A27A835494EF11EC9D75612416D0E2DC30810864C72F26F5EF2EAFF8B361C01D88C567D31F6F59D061F9750ECF166F074EB96827E9B4B041F2B3EEEB99FE4D8BC7442B41EB7E499DC25080AEFC3C08CE9FF8FC3628D464344404AE6BBDCEEAECB4DBDA362DEEB32A3CCA01BE34B0DAD5821A3F5210904F9F8FE54D07FAC968D6782F8FE10048B927CE156CD1D7B0EAC2627586E5717014DCE9F875BA9029009D8331F26052FFA09B79C67F0B991E727760A80D644D12BEE40B7720E6812453B72DCA4AE6443703CDCF77841004B6FEE50D2B9FE2494A3A1DFAF0786770A98ACE50A2CF691C33B2B23BBD8D0D510C6DFB84041B866632ABDEBCD1027D6D0C97B1C8CF52C7B1B1CDC45873F8B94CCCB77E78D20CBDCC073BECEFCE7D07504538689E1BDF8290A84AB10972BA8645F994EBF0B8209BD1DA6CFFD86024C2704853828C84D551E814D02EAFC52D48F2BFE9C488CBF934A35AEDD2D1A5397AC08489FD76B7354E81B3920D61BB539CE43CE13DB232D25B569C10BA0AB030A19E7BE0C16ECEEECB12E6733A23DAF110E81BFA43C863BA0A7746AC784E8BBB0BD43D7346B3470DFF5CC6577D5401265E8A5D55546C52583F9ADC687D4437FFF364A6228E46B33EB1CDE5D0929D7148C48485E4039CDE485A42B59B2C9D175FE097F6379DCCD13683BA4AA2E5902148D63B4DC305165BA9B77F51EC7D555BB72D37C5ED9D8742CE4D7CF591B9E9552F3CC8279B00745D00C693A7EE67B365D43E4FD8395662C7308E062C3665A073772AD76CDFA0B9A98D5D0E8B5D18F5B4C3E6B164405B0362A5E9957CAAB3AD321FB16136F2203F215AE8229839386B9568C59CC7056D7356495817E88240889D56386979338E9FB8A0FA2D15695AAEC1B6CB9105C9313915FC0B47D8E71BD21EAA8B2EF513A7C5997935BF0605A474375FC4046A89F2E042B48CA0998EB28C5E46E4FA0F86E08E19BE804BC3FF9380A7FF7166605BC98DEA1FA5C0F523C866C"
encType: "SM4"
signData: "iLyf85PTOuV4RJFUqsgZq/Jk2fw1goR11F/nzdhnx8jZS1d/PBEIfA0DRkMZENZnLMYL4CruXBLl8C6ZWHr0Xg=="
signType: "SM2"
timestamp: "1691201260062" 时间戳
即预览返回部分也许需要分析两个参数加密
那就先对请求标头参数进行逐步分析
直接全局搜索 encData 或 signData,搜索结果仅在 app.1691****.js 有,非常明显,上面还有设置 header的地方,所有参数都在这里,埋下断点,可以看到这里就是加密的地方,如下图所示:
头部参数处理
解决x-tif-nonce 和 x-tif-signature
encData参数处理
signdata处理
好吧我承认,因为感觉太复杂了,所以偷懒了,最后放一张逆向结果图
最后结果
写在最后:
本人写作水平有限,如有讲解不到位或者讲解错误的地方,还请各位大佬在评论区多多指教,共同进步.如有需要代码和讲解交流,可以加本人微信18847868809