简体   繁体   English

从 uglifyjs 源映射中恢复 javascript 源代码

[英]Recover javascript source code from uglifyjs source map

I am reverse engineering one magical script.我正在逆向工程一个神奇的脚本。 I have an ug lified source code and source map generated by uglifyjs .我有一个由uglifyjs生成的 ug lified 源代码和源映射

Does anybody know any straightforward way how to achieve at least partly readable source code from that?有没有人知道如何从中获得至少部分可读的源代码的任何直接方法 I have found some obscure ways including conversions through multiple languages, but I hope something better exists.我发现了一些晦涩的方法,包括通过多种语言进行转换,但我希望存在更好的方法。

Thank you!谢谢!

There is an npm library called maximize that purports to do this, but I couldn't get it to work.有一个名为maximize的 npm 库旨在执行此操作,但我无法使其正常工作。 I rewrote it as unsourcemap but I am not a Node JS developer, so I'm sure it's awful.我将它重写为unsourcemap但我不是Node JS 开发人员,所以我确定它很糟糕。 Anyway, it was a pretty trivial application of the source-map npm package.无论如何,这是source-map npm 包的一个非常简单的应用程序。

Once you go through all the rigamarole of installing node-js and nvm and whatnot, you can clone that repo and say:一旦您完成了安装 node-js 和 nvm 等所有繁琐的工作,您就可以克隆该 repo 并说:

npm install . -g
unsourcemap path/to/packed.js path/to/packed.js.map path/to/output-dir/

I don't want to maintain this thing, so if someone wants to improve it, please just fork it and point people to that.我不想维护这个东西,所以如果有人想改进它,请把它分叉并指出它。 :-) :-)

If the project is bundled using webpack / browserify, you can use Debundle package to do reverse engineering.如果项目是使用 webpack/ browserify 打包的,可以使用Debundle 包做逆向工程。 Imho, the result will be either good or bad depending on some project.恕我直言,根据某些项目,结果或好或坏。

And because it de-transpiles JS only (the uglifying webpack pipeline), so if you're using Vue SFC, the Debundle cannot produces your original .vue file, it's JS file instead.而且因为它只对 JS 进行反编译(丑陋的 webpack 管道),所以如果您使用 Vue SFC,Debundle 无法生成您的原始 .vue 文件,而是 JS 文件。

Anw, in case of reading source code, if your web page doesn't hide the source map files, you can use Chrome DevTool > Source pannel to read the beautiful source easily: Anw,在阅读源代码的情况下,如果您的网页没有隐藏源地图文件,您可以使用Chrome DevTool > Source面板轻松阅读精美的源代码:

Chrome DevTool - 源面板

Check this simple NodeJS implementation: It takes the compiled file with embedded source map and parses it.检查这个简单的 NodeJS 实现:它获取带有嵌入源映射的编译文件并解析它。 It's easy to modify and feed only the map file.仅修改和提供地图文件很容易。

const fs = require('fs');
const { SourceMapConsumer } = require("source-map");

fs.readFile('./example.js', 'utf8' , (err, data) => {
  if (err) return console.error(err);

  const sourceMapData = data.split('//# sourceMappingURL=data:application/json;base64,')[1];
  let buff = new Buffer.from(sourceMapData, 'base64');
  let rawSourceMap = buff.toString('ascii');

  const parsed = SourceMapConsumer(rawSourceMap);

  fs.writeFile('example.ts', parsed.sourcesContent, function (err) {
    if (err) return console.log(err);
  });
});

There are a few tools out that that can help with this.有一些工具可以帮助解决这个问题。 I've used http://www.cleancss.com/javascript-beautify/ and http://jsbeautifier.org/ before and had great results.我之前使用过http://www.cleancss.com/javascript-beautify/http://jsbeautifier.org/并且取得了很好的效果。 If you do a quick search in Google for a JavaScript Beautifier, you will find lots of similar results.如果您在 Google 中快速搜索 JavaScript Beautifier,您会发现很多类似的结果。 Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM