danieljoos/wincred
GitHub: danieljoos/wincred
Go 语言的 Windows Credential Manager API 封装库,提供凭据的创建、检索、删除与列举功能。
Stars: 148 | Forks: 22
# wincred
Windows Credential Manager API 函数的 Go 封装。
[](https://github.com/danieljoos/wincred/releases/latest)
[](https://github.com/danieljoos/wincred/actions?query=workflow%3Atest)
[](https://goreportcard.com/report/github.com/danieljoos/wincred)
[](https://codecov.io/gh/danieljoos/wincred)
[](https://pkg.go.dev/github.com/danieljoos/wincred)
## 安装
```
go get github.com/danieljoos/wincred
```
## 用法
请参阅以下示例:
### 创建并存储新的通用凭据对象
```
package main
import (
"fmt"
"github.com/danieljoos/wincred"
)
func main() {
cred := wincred.NewGenericCredential("myGoApplication")
cred.CredentialBlob = []byte("my secret")
err := cred.Write()
if err != nil {
fmt.Println(err)
}
}
```
### 检索凭据对象
```
package main
import (
"fmt"
"github.com/danieljoos/wincred"
)
func main() {
cred, err := wincred.GetGenericCredential("myGoApplication")
if err == nil {
fmt.Println(string(cred.CredentialBlob))
}
}
```
### 删除凭据对象
```
package main
import (
"fmt"
"github.com/danieljoos/wincred"
)
func main() {
cred, err := wincred.GetGenericCredential("myGoApplication")
if err != nil {
fmt.Println(err)
return
}
cred.Delete()
}
```
### 列出所有可用的凭据
```
package main
import (
"fmt"
"github.com/danieljoos/wincred"
)
func main() {
creds, err := wincred.List()
if err != nil {
fmt.Println(err)
return
}
for i := range(creds) {
fmt.Println(creds[i].TargetName)
}
}
```
## 提示
### 编码
凭据对象只是存储没有任何特定含义或编码的字节数组。
为了在不同的应用程序之间共享,应用一种显式的字符串编码可能更有意义——例如 **UTF-16 LE**(在 Win32 API 中几乎随处可见)。
```
package main
import (
"fmt"
"os"
"github.com/danieljoos/wincred"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
func main() {
cred := wincred.NewGenericCredential("myGoApplication")
encoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewEncoder()
blob, _, err := transform.Bytes(encoder, []byte("mysecret"))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
cred.CredentialBlob = blob
err = cred.Write()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
```
### 限制
根据 Windows API 的规定,凭据 blob 的大小被限制为 **2560 字节**。
标签:EVTX分析, 日志审计, 端点可见性