摘要:前几天无意间有个小伙伴问我要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/liweibin123/fortify
部分引用来自:https://nosec/home/detail/4470.html
更多推荐
fortify SCA内置规则破解到简单工具开发使用
发布评论