winapi

package module
v2.0.0-...-28dc089 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2020 License: MIT Imports: 3 Imported by: 0

README

GoLang Windows API Wrappers

For System Info / User Management.

For an internal project, this is a set of wrappers for snippets of the Windows API.

Tested and developed for Windows 10 x64.

All functions that return useful data, do so in the form of JSON exportable structs.

These structs are available in the shared library, "github.com/iamacarpet/go-win64api/v2/shared"

Process List
package main

import (
    "fmt"
    wapi "github.com/iamacarpet/go-win64api/v2"
)

func main(){
    pr, err := wapi.ProcessList()
    if err != nil {
        fmt.Printf("Error fetching process list... %s\r\n", err.Error())
    }
    for _, p := range pr {
        fmt.Printf("%8d - %-30s - %-30s - %s\r\n", p.Pid, p.Username, p.Executable, p.Fullpath)
    }
}
Active Session List (Logged in users + Run-As users)
package main

import (
    "fmt"
    wapi "github.com/iamacarpet/go-win64api/v2"
)

func main(){
    // This check runs best as NT AUTHORITY\SYSTEM
    //
    // Running as a normal or even elevated user,
    // we can't properly detect who is an admin or not.
    //
    // This is because we require TOKEN_DUPLICATE permission,
    // which we don't seem to have otherwise (Win10).
    users, err := wapi.ListLoggedInUsers()
    if err != nil {
        fmt.Printf("Error fetching user session list.\r\n")
        return
    }

    fmt.Printf("Users currently logged in (Admin check doesn't work for AD Accounts):\r\n")
    for _, u := range users {
        fmt.Printf("\t%-50s - Local User: %-5t - Local Admin: %t\r\n", u.FullUser(), u.LocalUser, u.LocalAdmin)
    }
}
Installed Software List
package main

import (
    "fmt"
    wapi "github.com/iamacarpet/go-win64api/v2"
)

func main(){
    sw, err := wapi.InstalledSoftwareList()
    if err != nil {
        fmt.Printf("%s\r\n", err.Error())
    }

    for _, s := range sw {
        fmt.Printf("%-100s - %s - %s\r\n", s.Name(), s.Architecture(), s.Version())
    }
}
Windows Update Status
package main

import (
        "fmt"
        "time"
        wapi "github.com/iamacarpet/go-win64api/v2"
)

func main() {
        ret, err := wapi.UpdatesPending()
        if err != nil {
                fmt.Printf("Error fetching data... %s\r\n", err.Error())
        }

        fmt.Printf("Number of Updates Available: %d\n", ret.NumUpdates)
        fmt.Printf("Updates Pending:             %t\n\n", ret.UpdatesReq)
        fmt.Printf("%25s | %25s | %s\n", "EVENT DATE", "STATUS", "UPDATE NAME")
        for _, v := range ret.UpdateHistory {
                fmt.Printf("%25s | %25s | %s\n", v.EventDate.Format(time.RFC822), v.Status, v.UpdateName)
        }
}

Local Service Management

List Services
package main

import (
    "fmt"

    wapi "github.com/iamacarpet/go-win64api/v2"
)

func main(){
    svc, err := wapi.GetServices()
    if err != nil {
        fmt.Printf("%s\r\n", err.Error())
    }

    for _, v := range svc {
        fmt.Printf("%-50s - %-75s - Status: %-20s - Accept Stop: %-5t, Running Pid: %d\r\n", v.SCName, v.DisplayName, v.StatusText, v.AcceptStop, v.RunningPid)
    }
}
Start Service
err := wapi.StartService(service_name)
Stop Service
err := wapi.StopService(service_name)

Local User Management

List Local Users
package main

import (
    "fmt"
    "time"
    wapi "github.com/iamacarpet/go-win64api/v2"
)

func main(){
    users, err := wapi.ListLocalUsers()
    if err != nil {
        fmt.Printf("Error fetching user list, %s.\r\n", err.Error())
        return
    }

    for _, u := range users {
        fmt.Printf("%s (%s)\r\n", u.Username, u.FullName)
        fmt.Printf("\tIs Enabled:                   %t\r\n", u.IsEnabled)
        fmt.Printf("\tIs Locked:                    %t\r\n", u.IsLocked)
        fmt.Printf("\tIs Admin:                     %t\r\n", u.IsAdmin)
        fmt.Printf("\tPassword Never Expires:       %t\r\n", u.PasswordNeverExpires)
        fmt.Printf("\tUser can't change password:   %t\r\n", u.NoChangePassword)
        fmt.Printf("\tPassword Age:                 %.0f days\r\n", (u.PasswordAge.Hours()/24))
        fmt.Printf("\tLast Logon Time:              %s\r\n", u.LastLogon.Format(time.RFC850))
        fmt.Printf("\tBad Password Count:           %d\r\n", u.BadPasswordCount)
        fmt.Printf("\tNumber Of Logons:             %d\r\n", u.NumberOfLogons)
    }
}
Adding a Local User
ok, err := wapi.UserAdd(username, fullname, password)
Deleting a Local User
ok, err := wapi.UserDelete(username)
Set Full Name Attribute
ok, err := wapi.UserUpdateFullname(username, fullname)
Give Admin Privileges
ok, err := wapi.SetAdmin(username)
Revoke Admin Privileges
ok, err := wapi.RevokeAdmin(username)
Disable/Enable User
s := true   // disable user
s := false  // enable user
ok, err := wapi.UserDisabled(username, s)
Change Attribute - User Can't Change Password
s := true   // User can't change password
s := false  // User can change password
ok, err := wapi.UserDisablePasswordChange(username, s)
Change Attribute - Password Never Expires
s := true   // Password never expires.
s := false  // Enable password expiry.
ok, err := wapi.UserPasswordNoExpires(username, s)
Forced Password Change
ok, err := wapi.ChangePassword(username, newpassword)
Windows Firewall - Add Inbound Rule
added, err := wapi.FirewallRuleCreate(
	"App Rule Name",
	"App Rule Long Description.",
	"My Rule Group",
	"%systemDrive%\\path\\to\\my.exe",
	"port number as string",
	wapi.NET_FW_IP_PROTOCOL_TCP,
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ACLAddControl

func ACLAddControl(usernames []string, acl *windows.ACL, accessMask windows.ACCESS_MASK, accessMode windows.ACCESS_MODE) (*windows.ACL, error)

ACLAddControl adds the indicated permission in accessMask for the given users in an ACL

func ACLSetControl

func ACLSetControl(usernames []string, accessMask windows.ACCESS_MASK, accessMode windows.ACCESS_MODE) (*windows.ACL, error)

ACLSetControl makes an ACL with the indicated permission in accessMask for the given users

func GetExplicitEntriesFromACL

func GetExplicitEntriesFromACL(acl *windows.ACL) (*[]windows.EXPLICIT_ACCESS, error)

GetExplicitEntriesFromACL gets a list of explicit entries from an ACL

func GetFilePermissions

func GetFilePermissions(path string) ([]windows.EXPLICIT_ACCESS, error)

GetFilePermissions return the list of Explicit entries on the file's DACL

func GetFileSecurityDescriptor

func GetFileSecurityDescriptor(path string, secInfo windows.SECURITY_INFORMATION) ([]uint16, error)

GetFileSecurityDescriptor returns a buffer with the file sec Descriptor

func GetSecurityDescriptorDACL

func GetSecurityDescriptorDACL(pSecDescriptor []uint16) (*windows.ACL, bool, bool, error)

GetSecurityDescriptorDACL gets an DACL from a security descriptor

func IsValidSecDescriptor

func IsValidSecDescriptor(secDescriptor []uint16) (bool, error)

IsValidSecDescriptor returns true is the secDescriptor is valid

func MakeAbsoluteSD

func MakeAbsoluteSD(selfRelative []uint16) ([]uint16, error)

MakeAbsoluteSD makes an absolute security descriptor out of a self-relative

func MakeSelfRelativeSD

func MakeSelfRelativeSD(absoluteSD []uint16) ([]uint16, error)

MakeSelfRelativeSD makes an absolute security descriptor out of a self-relative

func ORAddFullPermissions

func ORAddFullPermissions(srcHivePath string, dstHivePath string, username string) error

ORAddFullPermissions adds full control to a user over an offline registry hive.

func ORCloseHive

func ORCloseHive(key syscall.Handle) error

ORCloseHive closes offline registry hive

func ORGetKeySecurityBuffer

func ORGetKeySecurityBuffer(key syscall.Handle, secInfo windows.SECURITY_INFORMATION) ([]uint16, error)

ORGetKeySecurityBuffer Gets

func ORGetKeySecurityStructure

func ORGetKeySecurityStructure(key syscall.Handle, secInfo windows.SECURITY_INFORMATION) (windows.SECURITY_DESCRIPTOR, error)

ORGetKeySecurityStructure Gets

func OROpenHive

func OROpenHive(hivePath string) (syscall.Handle, error)

OROpenHive opens a registry hive outside the active system

func ORSaveHive

func ORSaveHive(key syscall.Handle, hivePath string) error

ORSaveHive saves changes to the offline registry hive

func ORSetKeySecurity

func ORSetKeySecurity(key syscall.Handle, secInfo windows.SECURITY_INFORMATION, secDescriptor []uint16) error

ORSetKeySecurity Gets

func SetFileACL

func SetFileACL(path string, acl *windows.ACL, inherit InheritMode) error

SetFileACL sets the given ACL to the object pointed to by path

func SetFilePermissions

func SetFilePermissions(usernames []string, path string,
	permissions windows.ACCESS_MASK, accessMode windows.ACCESS_MODE, inherit InheritMode, replace bool) error

SetFilePermissions gives the requested permissions to the given users on the given file. If replace is false, the new file permissions will include old permissions; it will only contain the ones set on this call otherwise

func SetFileSecurityDescriptor

func SetFileSecurityDescriptor(path string, secDescriptor []uint16, secInfo windows.SECURITY_INFORMATION) error

SetFileSecurityDescriptor sets a file security descriptor to the indicated file

func SetSecurityDescriptorDACL

func SetSecurityDescriptorDACL(pSecDescriptor []uint16, acl *windows.ACL, present bool, defaulted bool) error

SetSecurityDescriptorDACL sets an DACL for a security descriptor

Types

type InheritMode

type InheritMode int

InheritMode tells the permissions changer how to set the object's inheritance

const (
	// NoChange does not change the inheritance status
	NoChange InheritMode = iota
	// DoNotInherit disables inheritance
	DoNotInherit
	// Inherit enables inheritance
	Inherit
)

Directories

Path Synopsis
identity

Jump to

Keyboard shortcuts

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