Skip to content

Commit

Permalink
Merge pull request #1 from TheBestLL/master
Browse files Browse the repository at this point in the history
兼容windows user表 还有ssh协议公钥查询表
  • Loading branch information
dean2021 authored Jul 17, 2024
2 parents 7c3aca4 + 0cc9cea commit 984b3a4
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.DS_Store
*.log
dist/
vendor/
.vscode/
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ func main() {

## Build

go build -tags=sqlite_vtable
* darwin
> go build -tags=sqlite_vtable
* linux
> CGO_ENABLED=1 GOOS=linux CC="x86_64-linux-musl-gcc" GOARCH=amd64 go build -tags=sqlite_vtable -ldflags "-s -w --extldflags "-static""
* windows
> CGO_ENABLED=1 GOOS=windows CC="x86_64-w64-mingw32-gcc" GOARCH=amd64 go build -tags=sqlite_vtable
## Playground
```sql
Expand Down
42 changes: 42 additions & 0 deletions extend/tables/ssh_keys_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//go:build linux

package tables

import (
"github.com/dean2021/sysql/extend/tables/system"
"github.com/dean2021/sysql/table"
)

func init() {
t := &SSHKeysTable{}
err := table.Register(t, t.Name())
if err != nil {
panic(err)
}
}

type SSHKeysTable struct{}

func (p *SSHKeysTable) Name() string {
return "ssh_keys"
}

func (p *SSHKeysTable) Columns() table.TableColumns {
return table.TableColumns{
{Name: "uid", Type: table.BIGINT_TYPE, Options: table.INDEX},
{Name: "path", Type: table.TEXT_TYPE, Options: table.DEFAULT},
{Name: "username", Type: table.TEXT_TYPE, Options: table.DEFAULT},
{Name: "file_name", Type: table.TEXT_TYPE, Options: table.DEFAULT},
{Name: "file_size", Type: table.BIGINT_TYPE, Options: table.DEFAULT},
{Name: "mod_time", Type: table.TEXT_TYPE, Options: table.DEFAULT},
{Name: "key", Type: table.TEXT_TYPE, Options: table.DEFAULT},
}
}

func (p *SSHKeysTable) Generate(context *table.QueryContext) (table.TableRows, error) {
list, err := system.GenSSHKeys(context)
if err != nil {
return nil, err
}
return list, nil
}
65 changes: 65 additions & 0 deletions extend/tables/system/ssh_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//go:build linux

package system

import (
"bufio"
"os"
"path/filepath"

"github.com/dean2021/sysql/table"
)

var authorizedKeyFileNames = []string{"authorized_keys", "authorized_keys2"}

func GenSSHKeys(context *table.QueryContext) (table.TableRows, error) {
var results table.TableRows
users, err := getUsers()
if err != nil {
return nil, err
}
for _, u := range users {
for _, name := range authorizedKeyFileNames {
//判断是否存在.ssh文件
path := filepath.Join(u.Directory, ".ssh", name)
stat, err := os.Stat(path)
if err != nil {
continue
}
keys, err := getPublicKeys(path)
if err != nil {
return nil, err
}
for _, key := range keys {
results = append(results, table.TableRow{
"uid": u.Uid,
"path": path,
"username": u.Username,
"file_name": name,
"file_size": stat.Size(),
"mod_time": stat.ModTime().Format("2006-01-02 15:04:05"),
"key": key,
})
}
}
}
return results, nil
}

func getPublicKeys(path string) ([]string, error) {
//遍历authorized_keys文件
fi, err := os.Open(path)
if err != nil {
return nil, err
}
defer fi.Close()
var lines []string
scanner := bufio.NewScanner(fi)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return lines, nil
}
51 changes: 41 additions & 10 deletions extend/tables/system/users.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
//go:build linux || darwin

package system

import (
"bufio"
"github.com/dean2021/sysql/extend/tables/common"
"github.com/dean2021/sysql/table"
"os"
"strings"

"github.com/dean2021/sysql/extend/tables/common"
"github.com/dean2021/sysql/table"
)

type User struct {
Uid string `json:"uid"`
Gid string `json:"gid"`
Username string `json:"username"`
Description string `json:"description"`
Directory string `json:"directory"`
Shell string `json:"shell"`
}

func GenUsers(context *table.QueryContext) (table.TableRows, error) {
var results table.TableRows
users, err := getUsers()
if err != nil {
return nil, err
}
for _, u := range users {
results = append(results, table.TableRow{
"uid": u.Uid,
"gid": u.Gid,
"username": u.Username,
"description": u.Description,
"directory": u.Directory,
"shell": u.Shell,
})
}
return results, nil
}

func getUsers() ([]User, error) {
fi, err := os.Open(common.HostEtc("passwd"))
if err != nil {
return nil, err
}
defer fi.Close()
br := bufio.NewReader(fi)
users := []User{}
for {
s, _, err := br.ReadLine()
if err != nil {
Expand All @@ -27,15 +58,15 @@ func GenUsers(context *table.QueryContext) (table.TableRows, error) {
if len(items) < 7 {
continue
}
results = append(results, table.TableRow{
"uid": items[2],
"gid": items[3],
"username": items[0],
"description": items[4],
"directory": items[5],
"shell": items[6],
users = append(users, User{
Uid: items[2],
Gid: items[3],
Username: items[0],
Description: items[4],
Directory: items[5],
Shell: items[6],
})
}
}
return results, nil
return users, nil
}
65 changes: 65 additions & 0 deletions extend/tables/system/users_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//go:build windows
// +build windows

package system

import (
"github.com/dean2021/sysql/table"
"github.com/yusufpapurcu/wmi"
)

type Win32UserAccount struct {
AccountType int64 `json:"accountType"`
Caption string `json:"caption"`
Description string `json:"description"`
Disabled bool `json:"disabled"`
Domain string `json:"domain"`
FullName string `json:"fullName"`
InstallDate string `json:"installDate"`
LocalAccount bool `json:"localAccount"`
Lockout bool `json:"lockout"`
PasswordChangeable bool `json:"passwordChangeable"`
PasswordExpires bool `json:"passwordExpires"`
PasswordRequired bool `json:"passwordRequired"`
Name string `json:"name"`
SID string `json:"sid"`
SIDType int64 `json:"sidType"`
Status string `json:"status"`
}

func getWin32UserAccount() ([]Win32UserAccount, error) {
var s []Win32UserAccount
err := wmi.Query("SELECT * FROM Win32_UserAccount WHERE LocalAccount=True", &s)
if err != nil {
return nil, err
}
return s, nil
}
func GenUsers(context *table.QueryContext) (table.TableRows, error) {
var results table.TableRows
accounts, err := getWin32UserAccount()
if err != nil {
return nil, err
}
for _, a := range accounts {
results = append(results, table.TableRow{
"accountType": a.AccountType,
"caption": a.Caption,
"description": a.Description,
"disabled": a.Disabled,
"domain": a.Domain,
"fullName": a.FullName,
"installDate": a.InstallDate,
"localAccount": a.LocalAccount,
"lockout": a.Lockout,
"passwordChangeable": a.PasswordChangeable,
"passwordExpires": a.PasswordExpires,
"passwordRequired": a.PasswordRequired,
"name": a.Name,
"sid": a.SID,
"sidType": a.SIDType,
"status": a.Status,
})
}
return results, nil
}
2 changes: 2 additions & 0 deletions extend/tables/users_table.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

package tables

import (
Expand Down
52 changes: 52 additions & 0 deletions extend/tables/users_table_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//go:build windows
// +build windows

package tables

import (
"github.com/dean2021/sysql/extend/tables/system"
"github.com/dean2021/sysql/table"
)

func init() {
t := &UsersTable{}
err := table.Register(t, t.Name())
if err != nil {
panic(err)
}
}

type UsersTable struct{}

func (p *UsersTable) Name() string {
return "users"
}

func (p *UsersTable) Columns() table.TableColumns {
return table.TableColumns{
{Name: "sid", Type: table.TEXT_TYPE, Options: table.INDEX},
{Name: "name", Type: table.TEXT_TYPE, Options: table.DEFAULT},
{Name: "accountType", Type: table.INTEGER_TYPE, Options: table.DEFAULT},
{Name: "caption", Type: table.TEXT_TYPE, Options: table.DEFAULT},
{Name: "description", Type: table.TEXT_TYPE, Options: table.DEFAULT},
{Name: "disabled", Type: table.INTEGER_TYPE, Options: table.DEFAULT}, // Assuming boolean is represented as integer
{Name: "domain", Type: table.TEXT_TYPE, Options: table.DEFAULT},
{Name: "fullName", Type: table.TEXT_TYPE, Options: table.DEFAULT},
{Name: "installDate", Type: table.TEXT_TYPE, Options: table.DEFAULT},
{Name: "localAccount", Type: table.INTEGER_TYPE, Options: table.DEFAULT}, // Assuming boolean is represented as integer
{Name: "lockout", Type: table.INTEGER_TYPE, Options: table.DEFAULT}, // Assuming boolean is represented as integer
{Name: "passwordChangeable", Type: table.INTEGER_TYPE, Options: table.DEFAULT}, // Assuming boolean is represented as integer
{Name: "passwordExpires", Type: table.INTEGER_TYPE, Options: table.DEFAULT}, // Assuming boolean is represented as integer
{Name: "passwordRequired", Type: table.INTEGER_TYPE, Options: table.DEFAULT}, // Assuming boolean is represented as integer
{Name: "sidType", Type: table.INTEGER_TYPE, Options: table.DEFAULT},
{Name: "status", Type: table.TEXT_TYPE, Options: table.DEFAULT},
}
}

func (p *UsersTable) Generate(context *table.QueryContext) (table.TableRows, error) {
list, err := system.GenUsers(context)
if err != nil {
return nil, err
}
return list, nil
}

0 comments on commit 984b3a4

Please sign in to comment.