codeql常用类型及函数积累(进阶)| 相关自写案例

2024-09-02 20:28

本文主要是介绍codeql常用类型及函数积累(进阶)| 相关自写案例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

  • 前言
  • 一、函数
    • method.hasName("query")
    • method.getDeclaringType()
    • methodAccess.getQualifier
    • class.hasQualifiedName("com.mytest", "User")
  • 二、类型
    • ParamTag
    • MethodAccess
    • PrimitiveType
    • RefType
    • TopLevelType
    • NestedType
    • Call
  • 例子1
  • 例子2(spring提取注解参数名)
  • 例子3 (某堡垒机练手)

前言

写着写着,感觉这篇文章意义不是很大,更加推荐去看官方文档,例子和习题:
https://codeql.github.com/docs/codeql-language-guides/analyzing-data-flow-in-java/#examples
类型学习:
https://codeql.github.com/docs/codeql-language-guides/codeql-library-for-java/#types
各种类型漏洞使用codeql查询官方案例:
https://codeql.github.com/codeql-query-help/java/


本篇继续积累一些自己觉得重点的codeql用法和例子

一、函数

method.hasName(“query”)

用于判断一个方法的名称

method.hasName("query")

method.getDeclaringType()

表示获取一个方法所在的类或接口

import javapredicate isStudent(Method method) {
exists(|method.hasName("getStudent"))
}from Method method
where isStudent(method)
select method.getName(), method.getDeclaringType()

methodAccess.getQualifier

获取被调用方法,例如a.b(),那么methodAccess.getQualifier()获取的是a对象

methodAccess.getQualifier().getType() instanceof TypeString

class.hasQualifiedName(“com.mytest”, “User”)

检查是否是一个具有完全限定名称 com.mytest.User 的类。

二、类型

部分类型可翻阅如下文档:
https://codeql.github.com/docs/codeql-language-guides/codeql-library-for-java/#types

ParamTag

获取注释中的标签,例如param

import javafrom Callable c, ParamTag pt
where c.getDoc().getJavadoc() = pt.getParent()
select c, pt

MethodAccess

表示被调用的方法,例如:a.b(),那么这个b()就是被调用的方法

import javafrom Method method,MethodAccess call
where
call.getMethod() = method and
//.getCompilationUnit().fromSource()该条件指定了查询结果中的函数或方法必须来自源代码文件,而非库文件等其他来源。
method.getCompilationUnit().fromSource()
select method,call

PrimitiveType

PrimitiveType表示原始类型,PrimitiveType represents a primitive type, that is, one of boolean, byte, char, double, float, int, long, short; QL also classifies void and (the type of the null literal) as primitive types.

RefType

表示引用(其他对象),即非原始类型,例如:

Student stu //非原始类型
String str //原始类型

TopLevelType

TopLevelType表示在编译单元顶层声明的引用类型。

NestedType

NestedType是在另一个类型中声明的类型。

Call

指代被传递进入的参数,例如a(test),a即为call

例子1

查找new URL(param)这种形式的代码

import java
import semmle.code.java.dataflow.DataFlowfrom Constructor fileReader, Call call, Expr src
wherecall.getCallee() = fileReader andDataFlow::localFlow(DataFlow::exprNode(src), DataFlow::exprNode(call.getArgument(0))) andfileReader.getDeclaringType().hasQualifiedName("java.net","URL")
select call.getCallee(),call,src,fileReader

查找new URL(param)中的param参数

import java
import semmle.code.java.dataflow.DataFlowfrom Constructor fileReader, Call call, Parameter p
wherefileReader.getDeclaringType().hasQualifiedName("java.net", "URL") andcall.getCallee() = fileReader andDataFlow::localFlow(DataFlow::parameterNode(p), DataFlow::exprNode(call.getArgument(0)))
select p,call.getArgument(0)

例子2(spring提取注解参数名)

参考连接:https://tttang.com/archive/1512/

有如下例子:

(@RequestParam(required=true,value="id") String id)

1、getAnAnnotation获取注解
2、Annotation注解
3、hasQualifiedName
4、Expr表达式.getValue(“value”)例如:,则getValue(“value”)的值就是id

string getRequestParam(Method m){exists(Parameter p, Expr e, Annotation a | p = m.getAParameter()and a = p.getAnAnnotation()and a.getType().hasQualifiedName("org.springframework.web.bind.annotation", "RequestParam")and e = a.getValue("value")and e.getParent().toString() = "RequestParam"and e.toString() != "\"\""and result = e.toString())
}

例子3 (某堡垒机练手)

这是自己首次尝试构造出的第一个查询,构造过程中学到了很多:
在我参考:https://tttang.com/archive/1512/文章尝试定位注解的名称为RequestParam时,发现返回值居然为空(这可能是我构建数据库代码不全导致)

Annotation a
a.getType().hasQualifiedName("org.springframework.web.bind.annotation", "RequestParam")

解决办法:
通过getAQlClass来获取注解的类,判断其类名为:MetricElement
Annotation:注解

import javafrom Parameter p, Annotation a
wherea = p.getAnAnnotation() anda.getAQlClass().toString() = "MetricElement" 
select a

我主要想实现的功能是查找调用了sendMessageWithResponse的上级方法,全局数据流如下,但由于我分析的是闭源代码,效果不是很理想:

/*** @name Unsafe shiro deserialization* @kind problem* @id java/unsafe-deserialization*/
import java
import semmle.code.java.dataflow.DataFlow// TODO add previous class and predicate definitions hereclass ShiroUnsafeDeserializationConfig extends DataFlow::Configuration {ShiroUnsafeDeserializationConfig() { this = "ShiroUnsafeDeserializationConfig" }override predicate isSource(DataFlow::Node source) {exists(Annotation a,Parameter p |p.getAQlClass().toString().matches("MetricElement") anda = p.getAnAnnotation() andp.getCallable().hasModifier("public")//这里的p.getCallable()就是method)}predicate isDes(Expr arg){exists(MethodAccess des |des.getMethod().hasName("sendMessageWithResponse") andarg = des.getArgument(0))
}override predicate isSink(DataFlow::Node sink) {exists(Expr arg|isDes(arg))}
}from ShiroUnsafeDeserializationConfig config, DataFlow::Node source, DataFlow::Node sink
where config.hasFlow(source, sink)
select source,sink

本地数据流查询实现如下(不完善):

/*** @name Find public callers of sendMessageWithResponse and trace their callers* @description Recursively finds public methods that call sendMessageWithResponse and traces their callers.* @language java*/import javaclass SendMessageWithResponse extends MethodAccess {SendMessageWithResponse() {this.getMethod().hasName("sendMessageWithResponse")}}class PublicCallerOfSendMessage extends Method {PublicCallerOfSendMessage() {this.getDeclaringType().isPublic() andthis.fromSource() andexists(SendMessageWithResponse m |this.calls(m.getMethod()))}}predicate methodRecursivelyCalls(Method caller, Method callee) {caller = callee orexists(Method intermediate |caller.calls(intermediate) andmethodRecursivelyCalls(intermediate, callee))}class TopLevelCaller extends Method {TopLevelCaller() {this.getDeclaringType().isPublic() andthis.fromSource() andexists(Method m |methodRecursivelyCalls(this, m) andm instanceof PublicCallerOfSendMessage)}}from TopLevelCaller topLevelCallerselect topLevelCaller.getDeclaringType(), topLevelCaller

这篇关于codeql常用类型及函数积累(进阶)| 相关自写案例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1130983

相关文章

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

Pydantic中Optional 和Union类型的使用

《Pydantic中Optional和Union类型的使用》本文主要介绍了Pydantic中Optional和Union类型的使用,这两者在处理可选字段和多类型字段时尤为重要,文中通过示例代码介绍的... 目录简介Optional 类型Union 类型Optional 和 Union 的组合总结简介Pyd

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

Oracle数据库常见字段类型大全以及超详细解析

《Oracle数据库常见字段类型大全以及超详细解析》在Oracle数据库中查询特定表的字段个数通常需要使用SQL语句来完成,:本文主要介绍Oracle数据库常见字段类型大全以及超详细解析,文中通过... 目录前言一、字符类型(Character)1、CHAR:定长字符数据类型2、VARCHAR2:变长字符数

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Kotlin 作用域函数apply、let、run、with、also使用指南

《Kotlin作用域函数apply、let、run、with、also使用指南》在Kotlin开发中,作用域函数(ScopeFunctions)是一组能让代码更简洁、更函数式的高阶函数,本文将... 目录一、引言:为什么需要作用域函数?二、作用域函China编程数详解1. apply:对象配置的 “流式构建器”最

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis