alessio/shellescape

GitHub: alessio/shellescape

shellescape 是一个 Go 语言库,用于对任意字符串进行 shell 转义,使其安全地用作命令行参数。

Stars: 201 | Forks: 19

# shellescape ![Build](https://static.pigsec.cn/wp-content/uploads/repos/cas/3b/3b39c55110f11a97d10cda63d1c1193c1b78ee3e26d559adaea16b191fc65e8a.svg) [![GoDoc](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/alessio/shellescape?tab=overview) [![sourcegraph](https://sourcegraph.com/github.com/alessio/shellescape/-/badge.svg)](https://sourcegraph.com/github.com/alessio/shellescape) [![codecov](https://codecov.io/gh/alessio/shellescape/branch/master/graph/badge.svg)](https://codecov.io/gh/alessio/shellescape) [![Go Report Card](https://goreportcard.com/badge/github.com/alessio/shellescape)](https://goreportcard.com/report/github.com/alessio/shellescape) 对任意字符串进行转义,以安全地用作命令行参数。 ## 包内容 此包提供了 `shellescape.Quote()` 函数,该函数返回字符串的 shell 转义副本。当已知 Go 程序的输出将被附加到/用于 shell 程序的命令行参数上下文中时,此功能会非常有用。 此项目受到了 Python 原始包 [shellescape](https://pypi.python.org/pypi/shellescape) 的启发。 ## 用法 以下代码片段展示了一个典型的不安全用法: ``` package main import ( "fmt" "os" ) func main() { fmt.Printf("ls -l %s\n", os.Args[1]) } ``` _[在 Go Playground 中查看](https://play.golang.org/p/Wj2WoUfH_d)_ 特别是在创建可能最终由 shell 解释器执行的命令 pipeline 时,不对参数进行转义是极其不安全的。 `shellescape.Quote()` 派上了用场,可以安全地转义字符串: ``` package main import ( "fmt" "os" "al.essio.dev/pkg/shellescape" ) func main() { fmt.Printf("ls -l %s\n", shellescape.Quote(os.Args[1])) } ``` _[在 Go Playground 中查看](https://go.dev/play/p/GeguukpSUTk)_ ## escargs 工具 __escargs__ 从标准输入读取行,并打印 shell 转义后的版本。与 __xargs__ 不同,标准输入上的空行不会被丢弃。
标签:EVTX分析, Go, Ruby工具, Shell转义, 字符串转义, 日志审计