step-3

query a function named strlen

import cpp

from Function f
where f.getName() = "strlen"
select f, "a function named strlen"

step-4

query a function named memcpy

import cpp

from Function f
where f.getName() = "memcpy"
select f, "a function named memcpy"

step-5

query macros named ntohs or ntohl or ntohll

import cpp

from Macro macro
where macro.getName() = "ntohs"
or macro.getName() = "ntohl"
or macro.getName() = "ntohll"
select macro, "found macro"

more effective

import cpp

from Macro macro
where macro.getName() in ["ntohs", "ntohl", "ntohll"]
select macro, "found macro"

use Regular Expression

import cpp

from Macro macro
where macro.getName().regexpMatch("ntoh(s|l|ll)")
select macro, "found macro"

step-6

query the caller of a function

import cpp

from FunctionCall fc
where fc.getTarget().getName() = "memcpy"
select fc, "caller of the memcpy"

step-7

query the invocations of macros

import cpp

from MacroInvocation mi
where mi.getMacro().getName().regexpMatch("ntoh(s|l|ll)")
select mi

step-8

query the expressions that correspond to macro invocations.

import cpp

from MacroInvocation mi
where mi.getMacro().getName().regexpMatch("ntoh(s|l|ll)")
select mi.getExpr()

step-9

Write your own CodeQL class to represent a set of interesting source code elements

To define a class, you write:

  1. The keyword class.
  2. The name of the class. This is an identifier starting with an uppercase letter.
  3. The supertypes that the class is derived from via extends and/or instanceof
  4. The body of the class, enclosed in braces.
class OneTwoThree extends int {
OneTwoThree() { // characteristic predicate
this = 1 or this = 2 or this = 3
} string getAString() { // member predicate
result = "One, two or three: " + this.toString()
} predicate isEven() { // member predicate
this = 2
}
}
import cpp

/**
* An expression involved when swapping the byte order of network data.
* Its value is likely to have been read from the network.
*/
class NetworkByteSwap extends Expr {
NetworkByteSwap() {
exists(MacroInvocation mi |
mi.getMacroName().regexpMatch("ntoh(s|l|ll)") and
this = mi.getExpr()
)
}
} from NetworkByteSwap n
select n

step-10

query to track the flow of tainted data from network controlled interges to the memcpy length argument

import cpp
import semmle.code.cpp.dataflow.TaintTracking
import DataFlow::PathGraph /**
* An expression involved when swapping the byte order of network data.
* Its value is likely to have been read from the network.
*/
class NetworkByteSwap extends Expr {
NetworkByteSwap() {
exists(MacroInvocation mi |
mi.getMacroName().regexpMatch("ntoh(s|l|ll)") and
this = mi.getExpr()
)
}
} class Config extends TaintTracking::Configuration {
Config() { this = "no matter" } override predicate isSource(DataFlow::Node source) {
source.asExpr() instanceof NetworkByteSwap
} override predicate isSink(DataFlow::Node sink) {
exists(FunctionCall fc | fc.getTarget().getName() = "memcpy" and sink.asExpr() = fc.getArgument(2))
}
} from Config cfg, DataFlow::PathNode source, DataFlow::PathNode sink
where cfg.hasFlowPath(source, sink)
select sink, source, sink, "Network byte swap flows to memcpy"

CodeQl lab learn的更多相关文章

  1. RH253读书笔记(1)-Lab 1 System Monitoring

    Lab 1 System Monitoring Goal: To build skills to better assess system resources, performance and sec ...

  2. Learn to securely share files on the blockchain with IPFS!

    https://medium.com/@mycoralhealth/learn-to-securely-share-files-on-the-blockchain-with-ipfs-219ee47d ...

  3. 什么是 Meta Learning / Learning to Learn ?

    Learning to Learn Chelsea Finn    Jul 18, 2017 A key aspect of intelligence is versatility – the cap ...

  4. Lab 6-2

    Analyze the malware found in the file Lab06-02.exe. Questions and Short Answers What operation does ...

  5. Lab 6-1

    LABS The goal of the labs for this chapter is to help you to understand the overall functionality of ...

  6. Lab 1-4

    Analyze the file Lab01-04.exe. Questions and Short Answers Upload the Lab01-04.exe file to http://ww ...

  7. Lab 1-1

    LABS The purpose of the labs is to give you an opportunity to practice the skills taught in the chap ...

  8. 第六章:Reminders实验:第二部分[Learn Android Studio 汉化教程]

    Learn Android Studio 汉化教程 Reminders Lab: Part 2 This chapter covers capturing user input through the ...

  9. 第五章:Reminders实验:第一部分[Learn Android Studio 汉化教程]

    Learn Android Studio 汉化教程 By now you are familiar with the basics of creating a new project, program ...

  10. 6.824 Lab 2: Raft 2A

    6.824 Lab 2: Raft Part 2A Due: Feb 23 at 11:59pm Part 2B Due: Mar 2 at 11:59pm Part 2C Due: Mar 9 at ...

随机推荐

  1. 声网自研传输层协议 AUT 的落地实践丨Dev for Dev 专栏

    本文为「Dev for Dev 专栏」系列内容,作者为声网大后端传输协议负责人 夏天. 针对实时互动应用对网络传输带来的新需求和新挑战,声网通过将实时互动中的应用层业务需求与传输策略的分层和解耦,于 ...

  2. Mybatisplus标准数据层CRUD功能

    package com.itheima; import com.itheima.dao.UserDao; import com.itheima.domain.User; import org.juni ...

  3. 1 - Windows 10 - Python 类的常用高级系统函数(方法)通识

    @ 目录 一.系统函数__init__() 初始化类函数 二.系统函数__call__() 调用对象函数 三.系统函数__dict__类属性查询函数 四.系统函数__str__()描述类信息函数 五. ...

  4. 非常小的一个东西,Spring依赖注入Bean类型的8种情况

    大家好,我是三友~~ 今天来讲一个可能看似没有用但是实际又有点用的一个小东西,那就是@Autowired支持注入哪些Bean的类型. 为啥要讲这个呢? 故事说起来可能就比较长了. 不过长话可以短说,仅 ...

  5. 使用golang+antlr4构建一个自己的语言解析器(二)

    Antlr4文件解析流程 该图展示了一个语言应用程序中的基本流动过程 输入一个字符流,首先经过词法分析,获取各个Token 然后经过语法分析,组成语法分析树 Antlr4语法书写规范 语法关键字和使用 ...

  6. API网关:开源Apinto网关快速入门

    Apinto网关基于GO语言模块化开发,5分钟极速部署,配置简单.易于维护,支持集群与动态扩容,开箱即用.Apinto除了提供丰富的网关插件外,还提供监控告警.用户角色等扩展应用,同时支持自定义网关插 ...

  7. [Linux]命令行分类

    0 操作系统 / 编译 / 进程 / 内存 / 硬盘 / 硬件 hostname / hostnamectl / uname hostnamectl set-hostname xxxx 重置hostn ...

  8. 深度学习之PyTorch实战(5)——对CrossEntropyLoss损失函数的理解与学习

    其实这个笔记起源于一个报错,报错内容也很简单,希望传入一个三维的tensor,但是得到了一个四维. RuntimeError: only batches of spatial targets supp ...

  9. vue表单绑定v-model

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. odoo 开发入门教程系列-模块交互

    模块交互 在上一章中,我们使用继承来修改模块的行为.在我们的房地产场景中,我们希望更进一步,能够为客户生成发票.Odoo提供了一个开发票模块,因此直接从我们的房地产模块创建发票是很简单的,也就是说,一 ...