爬虫 js逆向,调用js 第三方库报错:execjs._exceptions.ProgramError: ReferenceError: crypto-js is not defined

     没学过js,刚刚学习爬虫接触 js逆向,觉得很是复杂,又要懂一点js代码,然后写成 python 处理处理,觉得很麻烦,其实就是懒,上网一找,python 有个 CryptoJS 的 三方库,只要把 js 代码稍微修改一下,就可以直接调用,很是方便。然后把JS 解密的部分全部复制进去,运行-------当 JS 代码需要调用 js的模块的时候,会报错,比如:发现报错execjs._exceptions.ProgramError: ReferenceError: crypto-js is not defined 。

    当时以为直接安装下 crypto-js 模块就可以了,然后在cmd 里面 安装,出现以下错误:

C:\Windows\System32>npm install crypto-js
npm WARN saveError ENOENT: no such file or directory, open 'C:\Windows\System32\package.json'
npm WARN enoent ENOENT: no such file or directory, open 'C:\Windows\System32\package.json'
npm WARN System32 No description
npm WARN System32 No repository field.
npm WARN System32 No README data
npm WARN System32 No license field.

+ [email protected]
updated 1 package and audited 1 package in 1.446s
found 0 vulnerabilities

 一项一项解决吧:

1、上网找,前两项是缺少一个叫 package.json 的文件。在 cmd 输入  npm init -y ,前两项解决。

C:\Windows\System32>npm init -y
Wrote to C:\Windows\System32\package.json:

{
  "name": "System32",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "crypto-js": "^4.0.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

再次运行安装程序:

C:\Windows\System32>npm install crypto-js
npm WARN registry Using stale data from https://registry.npmjs.org/ because the host is inaccessible -- are you offline?
npm WARN registry Using stale data from https://registry.npmjs.org/ due to a request error during revalidation.
npm WARN [email protected] No description
npm WARN [email protected] No repository field.

+ [email protected]
updated 1 package and audited 1 package in 1.953s
found 0 vulnerabilities

提示还剩两项,继续上网找。。。。。

原来是刚刚新建的 package.json 是默认的,需要稍微修改下里面的参数。

路径在刚刚执行的命令:npm init -y 的返回中找到了 :C:\Windows\System32\package.json 

打开之后:

{
  "name": "System32",
  "version": "1.0.0",
  "description": "npm-insall-package",
  "main": "index.js",
  "dependencies": {
    "crypto-js": "^4.0.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "private": true
}

修改了 "description":"npm-insall-package" ,和增加了 "private":true 

保存后再次在cmd 执行 安装命令:

C:\Windows\System32>npm install crypto-js
+ [email protected]
updated 1 package and audited 1 package in 1.299s
found 0 vulnerabilities

解决了。

下图为 python 调用 js的代码:

自己手动定义了crypto-js 库。

然后定义 crypto-js 模块的路径,我用的是绝对路径(当时也不知道刚刚安装的 crypto-js 模块的路径在哪里,我用了最粗暴的方法:直接用  Everything.exe 搜素 crypto-js文件)

爬虫 js逆向,调用js 第三方库报错:execjs._exceptions.ProgramError: ReferenceError: crypto-js is not defined_第1张图片

# *-* coding:utf-8 *-*

import requests
import re,os
from bs4 import BeautifulSoup
import execjs


js = '''
const CryptoJS = require('crypto-js');  //引用AES源码js  -------定义 crypto-js 库
function a(a) {
        var d, e, b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c = "";
        for (d = 0; a > d; d += 1)
            e = Math.random() * b.length,
            e = Math.floor(e),
            c += b.charAt(e);
        return c
    }
function b(a, b) {
	var c = CryptoJS.enc.Utf8.parse(b)
	  , d = CryptoJS.enc.Utf8.parse("0102030405060708")
	  , e = CryptoJS.enc.Utf8.parse(a)
	  , f = CryptoJS.AES.encrypt(e, c, {
		iv: d,
		mode: CryptoJS.mode.CBC
	});
	return f.toString()
}
function c(a, b, c) {
	var d, e;
	return setMaxDigits(131),
	d = new RSAKeyPair(b,"",c),
	e = encryptedString(d, a)
}
function d(d, e, f, g) {
	var h = {}
	  , i = a(16);
	return h.encText = b(d, g),
	h.encText = b(h.encText, i),
	h.encSecKey = c(i, e, f),
	h
}
function e(a, b, d, e) {
	var f = {};
	return f.encText = c(a + e, b, d),
	f
}


'''

if __name__ == '__main__':
	#C:\Windows\System32\node_modules
	ctx = execjs.compile(js,cwd=r"C:/Windows/System32/node_modules")  #定义js 模块的绝对路径
	
	print('execjs.compile().call():', ctx.call('b',10,10))

改好之后运行程序:解决了。

你可能感兴趣的:(python爬虫学习笔记)