sandialabs/Hql

GitHub: sandialabs/Hql

Hql 是一种基于 Kusto 语法的跨数据库通用查询语言,内置检测即代码能力,旨在弥合免费工具与商业 SIEM 平台之间的差距。

Stars: 0 | Forks: 0

# Hash 查询语言 (Hql)

Current Phase: Cambrian - Ultra Alpha

Current Phase: Primordial Ooze

Hash 查询语言 (Hql) 是一种查询语言,旨在为所有数据库后端实现一致的功能集。 这是通过使用 [Kusto 查询语言](https://github.com/microsoft/Kusto-Query-Language) (KQL) 的修改版语法来实现的,KQL 是微软为 Azure Data Explorer 创建的一种查询语言,也是 Log Analytics Workspace 的基础。 这使得可以使用 Elasticsearch 或 SQLite 等替代数据库后端,同时不会牺牲功能。 Hql 的灵感来自于我在 [DEATHCON](https://deathcon.io) 2024 上搭建好 Graylog 并用于个人 homelab 后,在使用过程中产生的挫败感,请参阅[此处的原始吐槽想法](docs/MANIFESTO.md)。 其实现与 Kusto 的不同之处在于,它支持并拥抱 nosql 数据集,而不是专有的、结构化的类 SQL 数据库后端。 此外还有许多其他功能变化,**它是一门完全不同的语言**,但其目标是复制 Kusto 的功能/特性集,并且在很大程度上与 Kusto 兼容。 Hql 提供了一些 Kusto 不支持的功能,例如在不同数据库和数据集之间进行 join: ``` let ElasticZeek = database("tf11-elastic").index("so-network-2022.10") | where event.module == "zeek" | extend IPAddress = source.ip; database("sentinel").SigninLogs | where Username == "iamcompromised" | project IPAddress | join type=inner ElasticZeek on IPAddress | summarize count() by destination.ip, destination.port, source.ip, source.port ``` 在上面的示例中,我们在 Azure(即 o365)中发现了一个攻击者 IOC,并能够立即转向我们在 Elasticsearch 中的 zeek 日志。 将 Elastic 替换为 Splunk,你就会得到一个绝佳的用例:将云端与本地部署连接起来。 当给定的后端不支持某些特定功能(如分析函数和 Lucene)时,这些功能会由 Hql 来实现。 在下面的示例中,第 1-3 行可以被合并为一个针对 Elastic 的单次查询。 结果被返回并加载到一个 [polars](https://docs.pola.rs/) DataFrame 中,随后执行以下操作: 1. extend - 在 DataFrame 中创建了一个名为 Hostname 的新列,其内容来自 winlog.computer_name - 将 event.code 列转换为 INT64 类型,并赋值给 EventID 列。 2. project - 将 EventID 列输入到 series_stats 中,生成一个包含各个统计值键的字典。 - 由于该函数作为单独的表达式提供,没有指定分配的名称,因此它会被扩展为新的输出 DataFrame。 ``` 1 database("tf11-elastic").index("so-beats-2022.10.*") 2 | where ['@timestamp'] between ("2022-10-21T15:45:00.000Z" .. "2022-10-21T15:55:00.000Z") 3 | where winlog.computer_name == "asarea.vxnwua.net" 4 | extend Hostname = winlog.computer_name, EventID = toint(event.code) 5 | project series_stats(EventID) ``` 结果如下: ``` [{"series_stats_EventID_min": 1, "series_stats_EventID_min_idx": 105, "series_stats_EventID_max": 16394, "series_stats_EventID_max_idx": 225, "series_stats_EventID_avg": 1709.3838936669272, "series_stats_EventID_stdev": 2257.263833183075, "series_stats_EventID_variance": 5095240.012596348}] ``` 使用 Polars 作为后端计算引擎可以实现超快的处理速度。 目前,全面影响性能的三个主要瓶颈是: 1. 对数据库的 IO 等待、滚动等 2. 解析,因为这仍然是在 Python 中完成的。 3. 启动程序 ## 检测即代码 Hql 原生支持检测即代码。 它直接接受 Sigma 查询,并能立即针对您的环境进行查询。 通用的源定义和字段映射是编译器的一部分,让定义变得毫不费力。 Sigma 也得到了扩展,包含两个新字段,用于定义 cron 计划和 Sigma 后续指令(例如推送到 soar)。 ``` title: Headless Process Launched Via Conhost.EXE id: 00ca75ab-d5ce-43be-b86c-55ff39c6abfc related: - id: 056c7317-9a09-4bd4-9067-d051312752ea type: derived schedule: '* * * * *' status: test description: | Detects the launch of a child process via "conhost.exe" with the "--headless" flag. The "--headless" flag hides the windows from the user upon execution. references: - https://www.huntress.com/blog/fake-browser-updates-lead-to-boinc-volunteer-computing-software author: Nasreddine Bencherchali (Nextron Systems) date: 2024-07-23 tags: - attack.defense-evasion - attack.t1059.001 - attack.t1059.003 - detection.threat-hunting logsource: category: process_creation product: windows detection: selection: ParentImage|endswith: '\conhost.exe' ParentCommandLine|contains: '--headless' condition: selection posthql: scot4 falsepositives: - Unknown level: medium ``` Hql 还将文档与代码放在一起,因此它永远不会脱离检测,从而实现真正的检测即代码。 它被定义为 Deoxygen 风格的注释,Hql as Code (HaC) 元数据随查询一起携带,允许在语言内部使用它。 ``` /** * @title Set Files as System Files Using Attrib.EXE * @id bb19e94c-59ae-4c15-8c12-c563d23fe52b * @status test * @schedule 0 * * * * * * @description * Detects the execution of "attrib" with the "+s" flag to mark files as system files * * @author frack113 * @related * - {'id': 'efec536f-72e8-4656-8960-5e85d091345b', 'type': 'similar'} * * @references * - https://github.com/redcanaryco/atomic-red-team/blob/f339e7da7d05f6057fdfcdd3742bfcf365fee2a9/atomics/T1564.001/T1564.001.md#atomic-test-3---create-windows-system-file-with-attrib * - https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/attrib * - https://unit42.paloaltonetworks.com/unit42-sure-ill-take-new-combojack-malware-alters-clipboards-steal-cryptocurrency/ * * @date 2022-02-04 * @modified 2023-03-14 * @tags * - attack.defense-evasion * - attack.t1564.001 * - detection.threat-hunting * * @falsepositives * - Unknown * * @level low */ product('windows').category('process_creation') | where Image endswith '\\attrib.exe' or OriginalFileName in ('ATTRIB.EXE') | where CommandLine contains_any (' +s ') ``` Sigma 查询也可以通过使用某个 flag 瞬间转换为这种 HaC 格式。 上面是一个直接转换的示例,允许对 Sigma 的功能集进行即时改进。 ``` python3 -m Hql -dpar -v -f ./proc_creation_win_attrib_system.yml > proc_creation_win_attrib_system.hql ``` HaC 也可以像这样在查询中初始化,自动生成 guid 和日期: ``` python3 -m Hql --init-hac -v -f ./new-detection.hql ``` ## HaC 引擎 为了完善整个系统,HaC 查询可以在多线程引擎中运行,从而将检测与它们受限的平台解耦。 例如,Sentinel 不允许您删除检测,而且重新部署检测会更改触发时间,从而产生重复项。 它可以在笔记本电脑上运行,无论您必须在什么环境中进行狩猎,都能快速完成设置。 它会自动调度检测,并确保它们按时运行。 在 http://localhost:8081 还有一个 **vibe coded**(凭感觉编程)的界面,允许您与之交互。 管理检测、运行并查看。 API 也存在于该端口上。 ## 已实现的功能 **已过时** 请参阅[此处](docs/features/README.md)了解已实现的功能。 我会在某个时候把它们整理成 issue。 在我完成文档之前,查看已关闭的 issue 可能会更好。 ## 运行说明 您至少需要 Python 3.9,但多线程和 HaC 引擎支持需要 Python 3.14t。 如果您使用 Python 3.14t,请准备好构建 polars,这需要一套完善的工具链。 为了方便起见,这里还提供了一个容器。 ``` # 复制并配置 Hql cp -r conf.example conf python3 -m venv .venv source .venv/bin/activate pip3 install -e . ## 或 pypi pip3 install pyhql ## 或 container(根据需要将 podman 替换为 docker) podman pull hashfastr/hql:latest # 根据需要添加 z # 用作 python3 -m Hql 的替代 podman run -v .:/data:z -it hashfastr/hql:latest --help # 进行你的第一次查询 python3 -m Hql -v -f ./examples/databases/json/json.hql # 运行 sigma,需要 Sigma source definitions python3 -m Hql -v -f ./examples/sigma/rules-threat-hunting/windows/process_creation/proc_creation_win_attrib_system.yml # 转换 sigma git submodule init python3 -m Hql -v -dpar -f ./examples/sigma/rules-threat-hunting/windows/process_creation/proc_creation_win_attrib_system.yml # 启动 HaC engine python3 -m Hql -v -eng -d ./examples/interface ```
标签:代码示例, 数据分析, 查询语言, 请求拦截, 跨数据库查询, 逆向工具