您现在的位置是:网站首页> 编程资料编程资料

简单的Python解密rsa案例_python_

2023-05-26 459人已围观

简介 简单的Python解密rsa案例_python_

⛳️ 本次反反爬实战案例背景

本篇博客选择的案例是由 VX 好友提出,他希望有一篇博客能简单的介绍清楚下面这个问题。

快速定位加密参数逻辑,快速扣取 JS,使用 JS 文件在 Python 中复现逻辑。

为此我翻找了一下橡皮擦的历史案例库(还没有写过的站点),发现如下一个目标站点,当乐

一看就是一个老平台了,看人家域名 d.cn。

通过点击登录按钮,定位到如下数据请求。

可以看到其请求方式是 GET,相关参数都在 URL 中进行了加密。

进行简单的定位之后,找到加密逻辑所在位置。

堆栈里面找到如下函数名 pwdFormLogin,点击进入代码逻辑处。

在代码段中找到了加密位置,其代码如下所示:

rsaPwd = rsa(passwordVal);

⛳️ JS 代码扣取

进入到 rsa() 函数内部,找到如下代码:

 //密码加密 var rsa = function (arg) { setMaxDigits(130); var PublicExponent = "10001"; var modulus = "be44aec4d73408f6b60e6fe9e3dc55d0e1dc53a1e171e071b547e2e8e0b7da01c56e8c9bcf0521568eb111adccef4e40124b76e33e7ad75607c227af8f8e0b759c30ef283be8ab17a84b19a051df5f94c07e6e7be5f77866376322aac944f45f3ab532bb6efc70c1efa524d821d16cafb580c5a901f0defddea3692a4e68e6cd"; var key = new RSAKeyPair(PublicExponent, "", modulus); return encryptedString(key, arg); }; 

打开 JS 工具箱,复制本段代码,然后进行补齐。上述代码仅包含 rsa 部分,RSAKeyPair 对象所在的代码不存在,需要补齐。

优先扣取 rsa 函数所在代码文件,可以假设一个网页不断进行测试,缺少哪个函数,就扣取对应 JS 文件,最终该案例得到如下结果。

  • RSA.js 文件包含核心加密逻辑
  • BigInt.js 文件包含数字处理函数,例如 setMaxDigits(130) 就在其中;
  • Barrett.js 文件包含了 RSAKeyPair 调用的部分文件,例如 BarrettMu。

  • 将上述所有内容组合成一个独立的 JS 文件,这里我们将该文件存放到 gitcode 中,大家可以进行获取。

登录加密逻辑.js

 /* * Copyright (c) 2015 Eric Wilde. * Copyright 1998-2015 David Shapiro. * * RSA.js is a suite of routines for performing RSA public-key computations * in JavaScript. The cryptographic functions herein are used for encoding * and decoding strings to be sent over unsecure channels. * * To use these routines, a pair of public/private keys is created through a * number of means (OpenSSL tools on Linux/Unix, Dave Shapiro's * RSAKeyGenerator program on Windows). These keys are passed to RSAKeyPair * as hexadecimal strings to create an encryption key object. This key object * is then used with encryptedString to encrypt blocks of plaintext using the * public key. The resulting cyphertext blocks can be decrypted with * decryptedString. * * Note that the cryptographic functions herein are complementary to those * found in CryptoFuncs.php and CryptoFuncs.pm. Hence, encrypted messages may * be sent between programs written in any of those languages. The most * useful, of course is to send messages encrypted by a Web page using RSA.js * to a PHP or Perl script running on a Web servitron. * * Also, the optional padding flag may be specified on the call to * encryptedString, in which case blocks of cyphertext that are compatible * with real crypto libraries such as OpenSSL or Microsoft will be created. * These blocks of cyphertext can then be sent to Web servitron that uses one * of these crypto libraries for decryption. This allows messages encrypted * with longer keys to be decrypted quickly on the Web server as well as * making for more secure communications when a padding algorithm such as * PKCS1v1.5 is used. * * These routines require BigInt.js and Barrett.js. */ /*****************************************************************************/ /* * Modifications * ------------- * * 2014 Jan 11 E. Wilde Add optional padding flag to encryptedString * for compatibility with real crypto libraries * such as OpenSSL or Microsoft. Add PKCS1v1.5 * padding. * * 2015 Jan 5 D. Shapiro Add optional encoding flag for encryptedString * and encapsulate padding and encoding constants * in RSAAPP object. * * Original Code * ------------- * * Copyright 1998-2005 David Shapiro. * * You may use, re-use, abuse, copy, and modify this code to your liking, but * please keep this header. * * Thanks! * * Dave Shapiro * dave@ohdave.com */ /*****************************************************************************/ var RSAAPP = {}; RSAAPP.NoPadding = "NoPadding"; RSAAPP.PKCS1Padding = "PKCS1Padding"; RSAAPP.RawEncoding = "RawEncoding"; RSAAPP.NumericEncoding = "NumericEncoding" /*****************************************************************************/ function RSAKeyPair(encryptionExponent, decryptionExponent, modulus, keylen) /* * encryptionExponent The encryption exponent (i.e. public * encryption key) to be used for * encrypting messages. If you aren't * doing any encrypting, a dummy * exponent such as "10001" can be * passed. * * decryptionExponent The decryption exponent (i.e. private * decryption key) to be used for * decrypting messages. If you aren't * doing any decrypting, a dummy * exponent such as "10001" can be * passed. * * modulus The modulus to be used both for * encrypting and decrypting messages. * * keylen The optional length of the key, in * bits. If omitted, RSAKeyPair will * attempt to derive a key length (but, * see the notes below). * * returns The "new" object creator returns an * instance of a key object that can be * used to encrypt/decrypt messages. * * This routine is invoked as the first step in the encryption or decryption * process to take the three numbers (expressed as hexadecimal strings) that * are used for RSA asymmetric encryption/decryption and turn them into a key * object that can be used for encrypting and decrypting. * * The key object is created thusly: * * RSAKey = new RSAKeyPair("ABC12345", 10001, "987654FE"); * * or: * * RSAKey = new RSAKeyPair("ABC12345", 10001, "987654FE", 64); * * Note that RSAKeyPair will try to derive the length of the key that is being * used, from the key itself. The key length is especially useful when one of * the padding options is used and/or when the encrypted messages created by * the routine encryptedString are exchanged with a real crypto library such * as OpenSSL or Microsoft, as it determines how many padding characters are * appended. * * Usually, RSAKeyPair can determine the key length from the modulus of the * key but this doesn't always work properly, depending on the actual value of * the modulus. If you are exchanging messages with a real crypto library, * such as OpenSSL or Microsoft, that depends on the fact that the blocks * being passed to it are properly padded, you'll want the key length to be * set properly. If that's the case, of if you just want to be sure, you * should specify the key length that you used to generated the key, in bits * when this routine is invoked. */ { /* * Convert from hexadecimal and save the encryption/decryption exponents and * modulus as big integers in the key object. */ this.e = biFromHex(encryptionExponent); this.d = biFromHex(decryptionExponent); this.m = biFromHex(modulus); /* * Using big integers, we can represent two bytes per element in the big * integer array, so we calculate the chunk size as: * * chunkSize = 2 * (number of digits in modulus - 1) * * Since biHighIndex returns the high index, not the number of digits, the * number 1 has already been subtracted from its answer. * * However, having said all this, "User Knows Best". If our caller passes us * a key length (in bits), we'll treat it as gospel truth. */ if (typeof (keylen) != 'number') { this.chunkSize = 2 * biHighIndex(this.m); } else { this.chunkSize = keylen / 8; } this.radix = 16; /* * Precalculate the stuff used for Barrett modular reductions. */ this.barrett = new BarrettMu(this.m); } /*****************************************************************************/ function encryptedString(key, s, pad, encoding) /* * key The previously-built RSA key whose * public key component is to be used to * encrypt the plaintext string. * * s The plaintext string that is to be * encrypted, using the RSA assymmetric * encryption method. * * pad The optional padding method to use * when extending the plaintext to the * full chunk size required by the RSA * algorithm. To maintain compatibility * with other crypto libraries, the * padding method is described by a * string. The default, if not * specified is "OHDave". Here are the * choices: * * OHDave - this is the original * padding method employed by Dave * Shapiro and Rob Saunders. If * this method is chosen, the * plaintext can be of any length. * It will be padded to the correct * length with zeros and then broken * up into chunks of the correct * length before being encrypted. * The resultant cyphertext blocks * will be separated by blanks. * * Note that the original code by * Dave Shapiro reverses the byte * order to little-endian, as the * plaintext is encrypted. If * either these JavaScript routines * or one of the complementary * PHP/Perl routines derived from * this code is used for decryption, * the byte order will be reversed * again upon decryption so as to * come out correctly. * * Also note that this padding * method is claimed to be less * secure than PKCS1Padding. * * NoPadding - this method truncates * the plaintext to the length of * the RSA key, if it is longer. If * its length is shorter, it is * padded with zeros. In either * case, the plaintext string is * reversed to preserve big-endian * order before it is encrypted to * maintain compatibility with real * crypto 
                
                

-六神源码网