util

package
v1.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2023 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DebugPrefix = "[DEBUG] "
	InfoPrefix  = "[INFO_]  "
	WarnPrefix  = "[WARN_]  "
	ErrorPrefix = "[ERROR] "
)
View Source
const Colorful = true

Variables

View Source
var (
	RedBold    = color.New(color.FgRed).Add(color.Bold)
	GreenBold  = color.New(color.FgGreen).Add(color.Bold)
	YellowBold = color.New(color.FgYellow).Add(color.Bold)
	BlueBold   = color.New(color.FgBlue).Add(color.Bold)
)

Colorful Bold use like `GreenBold.Sprint(str)`

View Source
var (
	ColorDebugPrefix = GreenBold.Sprint(DebugPrefix)
	ColorInfoPrefix  = BlueBold.Sprint(InfoPrefix)
	ColorWarnPrefix  = YellowBold.Sprint(WarnPrefix)
	ColorErrorPrefix = RedBold.Sprint(ErrorPrefix)
)
View Source
var DefaultLevel = INFO

Functions

func ByteToString

func ByteToString(orig []byte) string

func CheckUnpriUserNS added in v1.0.6

func CheckUnpriUserNS() error

CheckUnpriUserNS checks if the current host enable unprivileged user namespace. reference:

https://blog.trailofbits.com/2019/07/19/understanding-docker-container-escapes/
https://unit42.paloaltonetworks.com/cve-2022-0492-cgroups/

exceptional case:

the sysctl files(/proc/sys/kernel/unprivileged_userns_clone) only exist in Debian, Ubuntu.
We can not check the sysctl file in other distros, test in CentOS Linux release 8.4.2105 (Core).

func FileExist added in v1.0.3

func FileExist(path string) bool

func FindTargetDeviceID

func FindTargetDeviceID(mi *MountInfo) bool

find block device id

func GetAllCGroupSubSystem added in v1.0.6

func GetAllCGroupSubSystem() ([]string, error)

func GetCgroupVersion added in v1.0.6

func GetCgroupVersion() (int, error)

get cgroup version V1/V2 hybrid mode will not work in container

func GetGateway added in v1.3.0

func GetGateway() (string, error)

GetGateway returns the default gateway for the system.

func GetKernelVersion added in v1.0.6

func GetKernelVersion() ([]int, error)

get kernel version

func HttpSendJson added in v0.1.10

func HttpSendJson(method string, url string, data string) (string, error)

func IntContains added in v1.0.4

func IntContains(s []int, e int) bool

IntContains check string array contains a int number

func IsDir

func IsDir(FilePath string) bool

func IsDirectory added in v1.0.2

func IsDirectory(path string) bool
func IsSoftLink(FilePath string) bool

func MakeDev

func MakeDev(major, minor string) int

func PrintH2 added in v1.4.1

func PrintH2(title string)

fmt.Printf(util.GreenBold.Sprint("\n[Information Gathering - System Info]\n"))

func PrintItemKey added in v1.4.1

func PrintItemKey(key string, color bool)

func PrintItemValue added in v1.4.1

func PrintItemValue(value string, color bool)

func PrintItemValueWithKeyOneLine added in v1.5.0

func PrintItemValueWithKeyOneLine(key, value string, color bool)

func PrintOrignal added in v1.5.0

func PrintOrignal(out string)

func RandString

func RandString(n int) string

func ReadLines added in v1.0.3

func ReadLines(path string) ([]string, error)

ReadLines reads a whole file into memory and returns a slice of its lines. from https://stackoverflow.com/questions/5884154/read-text-file-into-string-array-and-write

func RemoveDuplicateElement

func RemoveDuplicateElement(addrs []string) []string

func RewriteFile

func RewriteFile(path string, content string, perm os.FileMode)

func SetBlockAccessible

func SetBlockAccessible(path string) error

set all block device accessible

func ShellExec

func ShellExec(shellPath string) error

ShellExec run shell script by bash

func StringContains added in v1.0.4

func StringContains(s []string, e string) bool

StringContains check string array contains a string

func UnixHttpSend

func UnixHttpSend(method string, unixPath string, uri string, data string) (string, error)

ref https://docs.docker.com/engine/api/v1.24/

func WriteFile

func WriteFile(path string, content string) error

func WriteFileAdd added in v1.0.1

func WriteFileAdd(path string, content string) error

func WriteShellcodeToCrontab added in v1.0.1

func WriteShellcodeToCrontab(header string, filePath string, shellcode string) error

Types

type CgroupInfo added in v1.0.6

type CgroupInfo struct {
	HierarchyID   int
	ControllerLst string // split by "," but should not be split
	CgroupPath    string
	OriginalInfo  string
}

func GetAllCGroup added in v1.0.6

func GetAllCGroup() ([]CgroupInfo, error)

func GetCgroup added in v1.1.0

func GetCgroup(pid int) ([]CgroupInfo, error)

GetCgroup returns the cgroup info of the process param pid: 0 = self, 1 = container main process

type Level added in v1.1.0

type Level uint8
const (
	ERROR Level = iota
	WARNNING
	INFO
	DEBUG
)

type LevelLogger added in v1.1.0

type LevelLogger struct {
	Level Level
	Color bool

	PrintFunc func(format string, v ...interface{})
}

func (*LevelLogger) Close added in v1.1.0

func (l *LevelLogger) Close()

func (*LevelLogger) Debug added in v1.1.0

func (l *LevelLogger) Debug(format string, v ...interface{})

func (*LevelLogger) Error added in v1.1.0

func (l *LevelLogger) Error(format string, v ...interface{})

func (*LevelLogger) Info added in v1.1.0

func (l *LevelLogger) Info(format string, v ...interface{})

func (*LevelLogger) Warn added in v1.1.0

func (l *LevelLogger) Warn(format string, v ...interface{})

type MountInfo

type MountInfo struct {
	Device            string
	Fstype            string
	Root              string
	MountPoint        string
	Opts              []string
	Major             string
	Minor             string
	SuperBlockOptions []string
}

MountInfo Sample: 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue Sample2: 1659 1605 253:1 /var/lib/kubelet/pods/cc76265f-d44d-4624-91c8-6f6812f85c7e/etc-hosts /etc/hosts rw,noatime - ext4 /dev/vda1 rw Sample3: 52 36 0:47 / /sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime shared:26 - cgroup cgroup rw,memory format: mountID parentID major:minor root mountPoint opts - Fstype device SuperBlockOptions

func GetMountInfo

func GetMountInfo() ([]MountInfo, error)

func (MountInfo) String added in v1.4.0

func (mi MountInfo) String() string

String format: major:minor root mountPoint opts - Fstype device SuperBlockOptions

type Wrapper added in v1.1.0

type Wrapper struct {
	LevelLogger
	// contains filtered or unexported fields
}

func NewStdoutWrapper added in v1.1.0

func NewStdoutWrapper() *Wrapper

func NewWrapper added in v1.1.0

func NewWrapper(writer io.Writer, colorful bool) *Wrapper

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL