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

相关文章

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

python判断文件是否存在常用的几种方式

《python判断文件是否存在常用的几种方式》在Python中我们在读写文件之前,首先要做的事情就是判断文件是否存在,否则很容易发生错误的情况,:本文主要介绍python判断文件是否存在常用的几种... 目录1. 使用 os.path.exists()2. 使用 os.path.isfile()3. 使用

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

从基础到进阶详解Pandas时间数据处理指南

《从基础到进阶详解Pandas时间数据处理指南》Pandas构建了完整的时间数据处理生态,核心由四个基础类构成,Timestamp,DatetimeIndex,Period和Timedelta,下面我... 目录1. 时间数据类型与基础操作1.1 核心时间对象体系1.2 时间数据生成技巧2. 时间索引与数据

六个案例搞懂mysql间隙锁

《六个案例搞懂mysql间隙锁》MySQL中的间隙是指索引中两个索引键之间的空间,间隙锁用于防止范围查询期间的幻读,本文主要介绍了六个案例搞懂mysql间隙锁,具有一定的参考价值,感兴趣的可以了解一下... 目录概念解释间隙锁详解间隙锁触发条件间隙锁加锁规则案例演示案例一:唯一索引等值锁定存在的数据案例二:

Python中bisect_left 函数实现高效插入与有序列表管理

《Python中bisect_left函数实现高效插入与有序列表管理》Python的bisect_left函数通过二分查找高效定位有序列表插入位置,与bisect_right的区别在于处理重复元素时... 目录一、bisect_left 基本介绍1.1 函数定义1.2 核心功能二、bisect_left 与

java中BigDecimal里面的subtract函数介绍及实现方法

《java中BigDecimal里面的subtract函数介绍及实现方法》在Java中实现减法操作需要根据数据类型选择不同方法,主要分为数值型减法和字符串减法两种场景,本文给大家介绍java中BigD... 目录Java中BigDecimal里面的subtract函数的意思?一、数值型减法(高精度计算)1.