新增准则:提供特定字段检测是否使用VirtualLock()函数锁定存放敏感信息的内存

wuhaoyang
wuhaoyang 10 months ago
parent 87e3c41f2b
commit e6fb96d32c
  1. 5
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/EncryptionAlgorithmChecker.java
  2. 88
      sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/VirtualLockUsageChecker.java
  3. 35
      sonar-keyware-plugins-cxx/src/test/java/com/keyware/sonar/cxx/rules/checkers/VirtualLockUsageCheckerTest.java
  4. 32
      sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/VirtualLockUsageChecker.cc

@ -59,14 +59,13 @@ public class EncryptionAlgorithmChecker extends SquidCheck<Grammar> {
return name != null && name.contains("password"); return name != null && name.contains("password");
})){ })){
cache.remove(varName); cache.remove(varName);
break;
} }
} }
next = next.getNextSibling(); next = next.getNextSibling();
} }
cache.values().forEach(item->{ cache.values().forEach(item->{
System.out.println("特定字段"+item.getFirstDescendant(CxxGrammarImpl.declaratorId).getTokenOriginalValue()+"未使用VirtualLock()函数锁定存放敏感信息的内存"); System.out.println("特定字段"+item.getFirstDescendant(CxxGrammarImpl.declaratorId).getTokenOriginalValue()+"未使用单向加密算法对口令进行加密并存储");
getContext().createLineViolation(this, "特定字段未使用VirtualLock()函数锁定存放敏感信息的内存", item); getContext().createLineViolation(this, "单向加密算法对口令进行加密并存储", item);
}); });
} }
} }

@ -0,0 +1,88 @@
/*
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved.
* 项目名称C++ 信息安全性设计准则
* 项目描述用于检查C++源代码的安全性设计准则的Sonarqube插件
* 版权说明本软件属北京关键科技股份有限公司所有在未获得北京关键科技股份有限公司正式授权情况下任何企业和个人不能获取阅读安装传播本软件涉及的任何受知识产权保护的内容
*/
package com.keyware.sonar.cxx.rules.checkers;
import com.sonar.cxx.sslr.api.AstNode;
import com.sonar.cxx.sslr.api.Grammar;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault;
import org.sonar.cxx.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.cxx.squidbridge.checks.SquidCheck;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 使用VirtualLock()函数锁定存放敏感信息的内存
*
* @author WuHaoYang
* @date 2024/1/6
*/
@Rule(key = "VirtualLockUsageChecker", name = "使用VirtualLock()函数锁定存放敏感信息的内存", description = "使用VirtualLock()函数锁定存放敏感信息的内存", priority = Priority.INFO, tags = {"28suo"})
@ActivatedByDefault
@SqaleConstantRemediation("5min")
public class VirtualLockUsageChecker extends SquidCheck<Grammar> {
private List<String> keywords = Arrays.asList("add", "keyword2", "keyword3");
private Map<String, Map<String, AstNode>> caches = new HashMap<>();
@Override
public void init() {
subscribeTo(CxxGrammarImpl.declarationStatement);
}
@Override
public void visitNode(AstNode astNode) {
String varName = astNode.getFirstDescendant(CxxGrammarImpl.declaratorId).getTokenOriginalValue();
for (String keyword : keywords) {
if (varName.contains(keyword) && (!caches.containsKey(keyword) || !caches.get(keyword).containsKey(varName))) {
caches.putIfAbsent(keyword, new HashMap<>());
caches.get(keyword).put(varName, astNode);
processNode(astNode, keyword);
}
}
}
private void processNode(AstNode astNode, String keyword) {
String varName = astNode.getFirstDescendant(CxxGrammarImpl.declaratorId).getTokenOriginalValue();
AstNode currentNode = astNode.getNextAstNode();
while (currentNode != null) {
AstNode callNode = currentNode.getFirstDescendant(CxxGrammarImpl.postfixExpression);
if (callNode != null && callNode.getTokenOriginalValue().equalsIgnoreCase("VirtualLock")) {
List<AstNode> paramList = callNode.getDescendants(CxxGrammarImpl.expressionList);
if (paramList.stream().anyMatch(item -> item.getTokenOriginalValue().contains(keyword))) {
caches.get(keyword).remove(varName);
break;
}
}
currentNode = currentNode.getNextSibling();
}
}
@Override
public void leaveFile(AstNode astNode) {
reportViolations();
}
private void reportViolations() {
caches.values().forEach(cache ->
cache.values().forEach(item -> {
System.out.println("特定字段"+item.getFirstDescendant(CxxGrammarImpl.declaratorId).getTokenOriginalValue()+"未使用VirtualLock()函数锁定存放敏感信息的内存");
getContext().createLineViolation(this, "特定字段未使用VirtualLock()函数锁定存放敏感信息的内存", item);
})
);
caches.clear();
}
}

@ -0,0 +1,35 @@
/*
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved.
* 项目名称C++ 信息安全性设计准则
* 项目描述用于检查C++源代码的安全性设计准则的Sonarqube插件
* 版权说明本软件属北京关键科技股份有限公司所有在未获得北京关键科技股份有限公司正式授权情况下任何企业和个人不能获取阅读安装传播本软件涉及的任何受知识产权保护的内容
*/
package com.keyware.sonar.cxx.rules.checkers;
import com.keyware.sonar.cxx.CxxFileTesterHelper;
import org.junit.jupiter.api.Test;
import org.sonar.cxx.CxxAstScanner;
import org.sonar.cxx.squidbridge.api.SourceFile;
import org.sonar.cxx.squidbridge.checks.CheckMessagesVerifier;
import java.io.IOException;
/**
* TODO VirtualLockUsageCheckerTest
*
* @author WuHaoYang
* @date 2024/1/15
*/
public class VirtualLockUsageCheckerTest {
@Test
public void checkTest() throws IOException {
var checker = new VirtualLockUsageChecker();
var tester = CxxFileTesterHelper.create("VirtualLockUsageChecker.cc");
SourceFile file = CxxAstScanner.scanSingleInputFile(tester.asInputFile(), checker);
CheckMessagesVerifier.verify(file.getCheckMessages())
.next().atLine(8).withMessage("特定字段未使用VirtualLock()函数锁定存放敏感信息的内存")
.noMore();
}
}

@ -0,0 +1,32 @@
#include <windows.h>
#include <iostream>
#include <vector>
using namespace std;
int main() {
string add = "北京市"; //error
// string keyword2 = "北京市"; //error
//
// string keyword3 = "北京市"; //error
// 利用vector<char>管理内存
// vector<char> addressBuffer(add.begin(), add.end());
//
// addressBuffer.push_back('\0');
//
// BOOL bResult = VirtualLock(addressBuffer.data(), addressBuffer.size());
// if (bResult == FALSE) {
// return 1;
// }
//
//
// // 解除锁定
// bResult = VirtualUnlock(addressBuffer.data(), addressBuffer.size());
// if (bResult == FALSE) {
// return 1;
// }
//
// return 0;
}
Loading…
Cancel
Save