fortify SCA内置规则破解到简单工具开发使用

摘要:前几天无意间有个小伙伴问我要fortify 内置最新规则,突然觉得内置规则既然能扫描代码缺陷,而他本质上是使用xml语言和内置源语言来编写的,心想能不能将其还原为xml文件,这样自定义规则的时候可以进行参考,少走弯路。说干就干,下面贴上具体步骤。

1. 既然猜测是通过解析xml规则来对代码进行静态分析的,所以,fortify内部肯定对加密规则进行了解密,将其转化为xml进行静态扫描分析。所以先进行一个初步搜索fortify相关jar包。在everything中输入 fortfiy*.jar,找到和fortify相关的jar包。最可疑的jar应该就是fortify-crypto-1.0.jar(因为已经开发好了jar包,所以后续也会搜到一些重复的内容)

2.将fortify-common-20.1.1.0007.jar   fortify-crypto-1.0.jar   fortify-public-20.1.1.0007.jar导入idea中,然后进行相关加密解密关键字搜索encrypt,decrypt,crypto后发现如下方法比较疑似。分析后,其实CryptoUtil中的方法更像是解密一个文件格式,因为他使用到了inputStream IO操作。

3.直接定位到CryptoUtil工具类中,查下这几个方法,发现调用关系是,先解密然后再解压缩。具体代码可以反编译jar包看源码。就不在赘述。

decryptCompressed(InputStream encrypted, String keyString)----->readHeaders(InputStream encrypted)------->decryptCompressedAfterHeaders(InputStream encrypted, String keyString)------>decryptAfterHeaders(InputStream encrypted, String keyString, boolean compressed)-------->doBlockCipher(InputStream source, OutputStream dest, boolean encrypt, long[] usrKey)------>dec(InputStream source, OutputStream dest, long[] usrKey)-----> uncompressString(byte[] in)

4.测试分析结果是否正确。随便找一个内置规则解析一下,发现结果是对的。

5.直接把他的工具类考进项目使用即可,都省了引入jar包了。核心代码如下所示,里面有swing组件的东西,用的时候可以删除掉

package com.lanju.decry;


import com.lanju.ui.CommonComponent;

import java.io.*;

public class FortifyRuleDecrypter {
    private String ruleDir;
    private String saveDir;
    private String information;
    public FortifyRuleDecrypter(String ruleDir, String saveDir) {
        this.ruleDir = ruleDir;
        this.saveDir = saveDir;
    }

    public void doDecrypt() {
        File encryptRule = new File(ruleDir);
        // 传入的是文件
        if (encryptRule.isFile()) {
            if (encryptRule.getName().endsWith(".bin")) {
                decryptRule(encryptRule, new File(saveDir + File.separator + encryptRule.getName() + ".xml"));
            } else {
                CommonComponent.jTextArea.append("[-] The rule file suffix is.bin!");
                System.out.println("[-] The rule file suffix is.bin!");
                System.exit(0);
            }
        }

        //传入是目录
        if (encryptRule.isDirectory()) {
            File[] listFile = encryptRule.listFiles();
            for (File file : listFile) {
                if (file.getName().endsWith(".bin")) {
                    File saveName = new File(saveDir + File.separator + file.getName().replace(".bin", "") + ".xml");
                    decryptRule(file, saveName);
                }
            }
        }

    }

    public void decryptRule(File encFile, File decFile) {
        try {
            //调用decryptCompressed()对规则库进行解密
            InputStream ruleStream = CryptoUtil.decryptCompressed(new FileInputStream(encFile), null);
            OutputStream outputStream = new FileOutputStream(decFile);
            byte[] b = new byte[1024];
            while ((ruleStream.read(b)) != -1) {
                outputStream.write(b);
            }
            ruleStream.close();
            outputStream.close();
            //System.out.println(String.format("[+] success %s -> %s", encFile.getName(), decFile.getAbsolutePath()));
            information = String.format("[+] success %s -> %s", encFile.getName(), decFile.getAbsolutePath()+"\r\n");
            CommonComponent.jTextArea.append(information);
        } catch (Exception e) {
            //System.out.println(String.format("[-] fail %s -> %s", encFile.getName(), decFile.getAbsolutePath()));
            String infomation2 = String.format("[-] fail %s -> %s", encFile.getName(), decFile.getAbsolutePath());
            CommonComponent.jTextArea.append(infomation2);
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //只接受两个参数
       /* if (args.length != 2) {
            System.out.println("Usage: java -jar FortifyRuleDecrypter.jar [rule_dir|rule_file] <save_dir>");
            System.exit(0);
        }
        FortifyRuleDecrypter decrypter = new FortifyRuleDecrypter(args[0], args[1]);
        decrypter.doDecrypt();*/
    }
}

将其打个jar包可以解密文件了。为了进一步有个友好的操作,想加个界面,看了一下午的swing组件开发,决定写一个。

5,swing组件没啥说的,这个建议大家不用深究了,因为都没人用了,直接贴上代码吧

package com.lanju.ui;

import com.lanju.decry.FortifyRuleDecrypter;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CommonComponent extends JFrame {

    private JPanel jPanel1, jPanel2, jPanel3;
    private JButton jButton1, jButton2, jButton3;
    private JScrollPane scroll;

    private JLabel name1, name2;

    private JTextField field1, field2;
    public static JTextArea jTextArea;
    private static CommonComponent frame;

    public CommonComponent() {


        //初始化四个panel对象
        jPanel1 = new JPanel();
        jPanel2 = new JPanel();
        jPanel3 = new JPanel();

        this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
        // 标签
        name1 = new JLabel("encrypt-rule:");
        // 文本域
        field1 = new JTextField(30);
        //按钮
        jButton1 = new JButton("选择路径");
        jPanel1.add(name1);
        jPanel1.add(field1);
        jPanel1.add(jButton1);
        this.add(jPanel1);


        // 标签
        name2 = new JLabel("decrypt-rule:");
        // 文本域
        field2 = new JTextField(30);
        //按钮
        jButton2 = new JButton("选择路径");
        jPanel2.add(name2);
        jPanel2.add(field2);
        jPanel2.add(jButton2);
        this.add(jPanel2);

        //按钮
        jButton3 = new JButton("开始解密");
        jPanel3.add(jButton3);
        this.add(jPanel3);

        //下方显示信息
        jTextArea = new JTextArea(1, 1);
        //把定义的JTextArea放到JScrollPane里面去
        scroll = new JScrollPane(jTextArea);
        //分别设置水平和垂直滚动条自动出现
        scroll.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        jTextArea.setLineWrap(true);
        this.add(scroll);

        init();
    }

    public void init() {

        //选择加密规则路径
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                chooiceDirPath(field1);
            }
        });
        //选择解密路径
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                chooiceDirPath(field2);
            }
        });

        jButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String encrytPath = field1.getText();
                String decrytPath = field2.getText();
                //开启一个子线程提高执行效率
                new Thread(){
                    @Override
                    public void run() {
                        FortifyRuleDecrypter fortifyRuleDecrypter = new FortifyRuleDecrypter(encrytPath, decrytPath);
                        fortifyRuleDecrypter.doDecrypt();
                    }
                }.start();

            }
        });
    }

    public void chooiceDirPath(JTextField field) {
        JFileChooser fileChooser = new JFileChooser("D:\\");

        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        int returnVal = fileChooser.showOpenDialog(fileChooser);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String filePath = fileChooser.getSelectedFile().getAbsolutePath();
            field.setText(filePath);
        }
    }

   /* public void outputUI() {//捕获控制台输出到GUI界面上
        OutputStream textAreaStream = new OutputStream() {
            public void write(int b) throws IOException {
                jTextArea.append(String.valueOf((char) b));
            }

            public void write(byte b[]) throws IOException {
                jTextArea.append(new String(b));
            }

            public void write(byte b[], int off, int len) throws IOException {
                jTextArea.append(new String(b, off, len));
            }
        };
        PrintStream myOut = new PrintStream(textAreaStream);
        System.setOut(myOut);
        System.setErr(myOut);
    }
*/
    public static void main(String[] args) {
        frame = new CommonComponent();
        frame.setTitle("规则解密");
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 自适应
        // frame.pack();
        frame.setVisible(true);
    }

}

6.最后将其打成jar包即可运行,运行效果如图所示。看起来比较low,就凑合用吧,O(∩_∩)O哈哈~。如有需要可直接上Github下载,下载地址https://github.com/liweibin123/fortify

部分引用来自:https://nosec.org/home/detail/4470.html

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Fortify SCA使用手册》是一本介绍如何使用Fortify SCA(静态代码扫描工具)的手册。Fortify SCA是一种用于识别和修复软件代码中漏洞和安全隐患的工具。这本手册旨在帮助开发人员了解如何正确地使用Fortify SCA来提高软件安全性。 手册首先介绍了Fortify SCA的基本原理和工作流程。它解释了代码扫描的过程,包括如何配置扫描规则、扫描参数和扫描范围等。通过从源代码中提取语义和构建数据流图,Fortify SCA可以分析代码中的潜在漏洞和安全风险。 接下来,手册详细介绍了Fortify SCA的功能和工具。它解释了如何使用Fortify SCA的界面来管理和执行扫描任务,包括如何创建项目、导入代码、配置扫描规则等。此外,手册还提供了关于如何解读扫描结果和报告的指导,以及如何通过查看漏洞详情和代码路径来定位和修复问题。 此外,手册还包括一些使用Fortify SCA的最佳实践和建议。它强调了需在开发过程中进行持续扫描和自动化集成,以便及早发现和修复安全漏洞。手册还提供了一些常见漏洞类型的示例和建议,以帮助开发人员更好地理解Fortify SCA的扫描结果,并采取适当的修复措施。 总之,《Fortify SCA使用手册》是一本全面而详细的指导手册,旨在帮助开发人员学会如何正确地使用Fortify SCA来提高软件安全性。通过充分理解和应用手册中的指导,开发人员可以更好地利用Fortify SCA的功能,确保软件代码的安全性和可靠性。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值