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

相关文章

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

MySQL 中的 JSON 查询案例详解

《MySQL中的JSON查询案例详解》:本文主要介绍MySQL的JSON查询的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 的 jsON 路径格式基本结构路径组件详解特殊语法元素实际示例简单路径复杂路径简写操作符注意MySQL 的 J

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

usb接口驱动异常问题常用解决方案

《usb接口驱动异常问题常用解决方案》当遇到USB接口驱动异常时,可以通过多种方法来解决,其中主要就包括重装USB控制器、禁用USB选择性暂停设置、更新或安装新的主板驱动等... usb接口驱动异常怎么办,USB接口驱动异常是常见问题,通常由驱动损坏、系统更新冲突、硬件故障或电源管理设置导致。以下是常用解决

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

springboot项目中常用的工具类和api详解

《springboot项目中常用的工具类和api详解》在SpringBoot项目中,开发者通常会依赖一些工具类和API来简化开发、提高效率,以下是一些常用的工具类及其典型应用场景,涵盖Spring原生... 目录1. Spring Framework 自带工具类(1) StringUtils(2) Coll