db

package
v0.0.0-...-5e2eff2 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AgentStatusHealthy                  = "AgentStatusHealthy"
	AgentStatusUnhealthyButConnected    = "AgentStatusUnhealthyButConnected"
	AgentStatusUnhealthyAndDisconnected = "AgentStatusUnhealthyAndDisconnected"
	AgentStatusDead                     = "AgentStatusDead"
)
View Source
const (
	JobStatusCreated       = "JobStatus-Created"
	JobStatusAwaitingStart = "JobStatus-AwaitingStart"
	JobStatusStarted       = "JobStatus-Started"
	JobStatusExited        = "JobStatus-Exited"
)
View Source
const (
	// Clean exit
	JobStopReasonFinished = "JobStopReason-Finished"
	// User stopped it
	JobStopReasonUserStopped = "JobStopReason-UserStopped"
	// Never started in the first place
	JobStopReasonFailedToStart = "JobStopReason-FailedToStart"
	// General failure
	JobStopReasonFailed = "JobStopReason-Failed"
	// Agent timed out and we lost contact
	JobStopReasonTimeout = "JobStopReason-Timeout"
)
View Source
const (
	JobStdLineStreamStdout = "stdout"
	JobStdLineStreamStderr = "stderr"
)
View Source
const (
	ListfileTypeWordlist = "Wordlist"
	ListfileTypeRulefile = "Rulefile"
)
View Source
const MaxJobOutputs = 10

TODO: actually, on second thought, I want to keep all stderr lines, and only roll-over stdout lines

Variables

View Source
var ErrNotFound = gorm.ErrRecordNotFound

Functions

func AddJobCrackedHash

func AddJobCrackedHash(jobId string, hash string, plaintextHex string) error

func AddJobStatusUpdate

func AddJobStatusUpdate(jobId string, status hashcattypes.HashcatStatus) error

func AddJobStdline

func AddJobStdline(jobId string, stream string, line string) error

func Connect

func Connect(dsn string) error

func DeleteAttack

func DeleteAttack(attackId string) error

// Also deletes jobs

func DeleteHashlist

func DeleteHashlist(hashlistId string) error

Also deletes attacks, jobs

func DeleteProject

func DeleteProject(projectId string) error

Also deletes hashlists, attacks, jobs

func DeleteProjectShare

func DeleteProjectShare(projId string, userId string) error

func FindAgentIDByAuthKey

func FindAgentIDByAuthKey(authKey string) (string, error)

func GetAttackProjID

func GetAttackProjID(attackId string) (string, error)

func GetConfig

func GetConfig[ConfigT interface{}]() (*ConfigT, error)

func GetConfigJSONString

func GetConfigJSONString() (string, error)

func GetHashlistProjID

func GetHashlistProjID(hashlistId string) (string, error)

func GetInstance

func GetInstance() *gorm.DB

func GetJobHashtype

func GetJobHashtype(jobId string) (uint, error)

func GetJobProjID

func GetJobProjID(jobId string) (string, error)

func HardDelete

func HardDelete[T any](obj *T) error

func MarkListfileAsAvailable

func MarkListfileAsAvailable(id string) error

func MarkListfileForDeletion

func MarkListfileForDeletion(id string) error

func NormalizeUsername

func NormalizeUsername(username string) string

func PopulateHashlistFromPotfile

func PopulateHashlistFromPotfile(hashlistId string) (int64, error)

func Save

func Save[T any](obj *T) error

func SeedConfig

func SeedConfig[ConfigT interface{}](defaultConfig ConfigT) error

func SetAttackProgressString

func SetAttackProgressString(attackId string, progressString string) error

func SetConfig

func SetConfig[ConfigT interface{}](newConf ConfigT) error

func SetJobExited

func SetJobExited(jobId string, reason string, errStr string, exitTime time.Time) error

func SetJobScheduled

func SetJobScheduled(jobId string, agentId string) error

func SetJobStarted

func SetJobStarted(jobId string, cmdLine string, startTime time.Time) error

func SoftDelete

func SoftDelete[T any](obj *T) error

func UpdateAgentDevices

func UpdateAgentDevices(agentId string, devices []hashcattypes.HashcatStatusDevice) error

func UpdateAgentInfo

func UpdateAgentInfo(agentId string, info AgentInfo) error

func UpdateAgentMaintenanceMode

func UpdateAgentMaintenanceMode(agentId string, isMaintenance bool) error

func UpdateAgentStatus

func UpdateAgentStatus(agentId string, status string) error

func WipeEverything

func WipeEverything() error

Types

type Agent

type Agent struct {
	UUIDBaseModel
	Name              string
	KeyHash           string
	IsMaintenanceMode bool `gorm:"default:false; not null"`
	AgentInfo         datatypes.JSONType[AgentInfo]
	AgentDevices      datatypes.JSONType[AgentDeviceInfo]
}

func CreateAgent

func CreateAgent(name string) (newAgent *Agent, plaintextKey string, err error)

func FindAgentByAuthKey

func FindAgentByAuthKey(authKey string) (*Agent, error)

func GetAgent

func GetAgent(id string) (*Agent, error)

func GetAllAgents

func GetAllAgents() ([]Agent, error)

func GetAllHealthyAgents

func GetAllHealthyAgents() ([]Agent, error)

func GetAllSchedulableAgents

func GetAllSchedulableAgents() ([]Agent, error)

func (Agent) ToDTO

func (a Agent) ToDTO() apitypes.AgentDTO

type AgentDeviceInfo

type AgentDeviceInfo struct {
	Devices []hashcattypes.HashcatStatusDevice
}

type AgentFile

type AgentFile struct {
	Name string `json:"name"`
	Size int64  `json:"size"`
}

func (AgentFile) ToDTO

func (a AgentFile) ToDTO() apitypes.AgentFileDTO

type AgentInfo

type AgentInfo struct {
	Status               string      `json:"status"`
	Version              string      `json:"version"`
	TimeOfLastHeartbeat  time.Time   `json:"time_of_last_heartbeat,omitempty"`
	TimeOfLastDisconnect time.Time   `json:"time_of_last_disconnect,omitempty"`
	TimeOfLastConnect    time.Time   `json:"time_of_last_connect,omitempty"`
	AvailableListfiles   []AgentFile `json:"available_listfiles,omitempty"`
	ActiveJobIDs         []string    `json:"active_job_ids,omitempty"`
}

func (AgentInfo) ToDTO

func (a AgentInfo) ToDTO() apitypes.AgentInfoDTO

type Attack

type Attack struct {
	UUIDBaseModel
	HashcatParams  datatypes.JSONType[hashcattypes.HashcatParams]
	IsDistributed  bool
	ProgressString string

	Jobs       []Job     `gorm:"constraint:OnDelete:CASCADE;"`
	HashlistID uuid.UUID `gorm:"type:uuid"`
}

func CreateAttack

func CreateAttack(attack *Attack) (*Attack, error)

func GetAllAttacksForHashlist

func GetAllAttacksForHashlist(hashlistId string) ([]Attack, error)

func GetAttack

func GetAttack(attackId string) (*Attack, error)

func (*Attack) ToDTO

func (a *Attack) ToDTO() apitypes.AttackDTO

type AttackIDTree

type AttackIDTree struct {
	ProjectID  string
	HashlistID string
	AttackID   string
}

func GetAllAttacksWithProgressStringsForUser

func GetAllAttacksWithProgressStringsForUser(user *User) ([]AttackIDTree, error)

func (*AttackIDTree) ToDTO

type Config

type Config struct {
	SimpleBaseModel
	Config datatypes.JSON
}

func (Config) TableName

func (c Config) TableName() string

type Hashlist

type Hashlist struct {
	UUIDBaseModel
	ProjectID uuid.UUID `gorm:"type:uuid"`

	Name    string
	Version uint

	HashType int
	Hashes   []HashlistHash `gorm:"constraint:OnDelete:CASCADE;"`

	Attacks []Attack `gorm:"constraint:OnDelete:CASCADE;"`
}

func CreateHashlist

func CreateHashlist(hashlist *Hashlist) (*Hashlist, error)

func GetAllHashlistsForProject

func GetAllHashlistsForProject(projId string) ([]Hashlist, error)

func GetHashlist

func GetHashlist(hashlistId string) (*Hashlist, error)

func GetHashlistWithHashes

func GetHashlistWithHashes(hashlistId string) (*Hashlist, error)

func (*Hashlist) ToDTO

func (h *Hashlist) ToDTO(withHashes bool) apitypes.HashlistDTO

type HashlistHash

type HashlistHash struct {
	SimpleBaseModel
	HashlistID     uuid.UUID `gorm:"type:uuid"`
	NormalizedHash string
	InputHash      string
	PlaintextHex   string
	IsCracked      bool
}

func (*HashlistHash) ToDTO

type Job

type Job struct {
	UUIDBaseModel

	HashlistVersion uint

	Attack   Attack
	AttackID *uuid.UUID `gorm:"type:uuid"`

	HashcatParams datatypes.JSONType[hashcattypes.HashcatParams]

	TargetHashes pq.StringArray `gorm:"type:text[]"`
	HashType     int

	RuntimeData JobRuntimeData `gorm:"constraint:OnDelete:CASCADE;"`

	AssignedAgent   Agent      `gorm:"constraint:OnDelete:SET NULL;"`
	AssignedAgentID *uuid.UUID `gorm:"type:uuid"`
}

func CreateJob

func CreateJob(job *Job) (*Job, error)

func CreateJobTx

func CreateJobTx(job *Job, tx *gorm.DB) (*Job, error)

func GetAllIncompleteJobs

func GetAllIncompleteJobs(includeRuntimeData bool) ([]Job, error)

func GetAllPendingJobs

func GetAllPendingJobs(includeRuntimeData bool) ([]Job, error)

func GetJob

func GetJob(jobId string, includeRuntimeData bool) (*Job, error)

func GetJobsForAttack

func GetJobsForAttack(attackId string, includeRuntimeData bool) ([]Job, error)

func GetJobsForHashlist

func GetJobsForHashlist(hashlistId string) ([]Job, error)

func GetJobsForProject

func GetJobsForProject(projectId string) ([]Job, error)

func (Job) ToDTO

func (j Job) ToDTO() apitypes.JobDTO

func (*Job) ToSimpleDTO

func (j *Job) ToSimpleDTO() apitypes.JobSimpleDTO

type JobCrackedHash

type JobCrackedHash struct {
	Hash         string
	PlaintextHex string
}

func (JobCrackedHash) ToDTO

type JobRuntimeData

type JobRuntimeData struct {
	SimpleBaseModel
	JobID uuid.UUID `gorm:"type:uuid"`

	// when we ask the job to start
	StartRequestTime time.Time
	// when it actually starts on the agent
	StartedTime time.Time
	StoppedTime time.Time
	Status      string
	StopReason  string
	ErrorString string

	CmdLine string // hashcat command

	OutputLines   pgJSONBArray[JobRuntimeOutputLine]
	StatusUpdates pgJSONBArray[hashcattypes.HashcatStatus]
	CrackedHashes pgJSONBArray[JobCrackedHash]
}

func (JobRuntimeData) ToDTO

func (*JobRuntimeData) ToSummaryDTO

func (r *JobRuntimeData) ToSummaryDTO() apitypes.JobRuntimeSummaryDTO

type JobRuntimeOutputLine

type JobRuntimeOutputLine struct {
	Stream string
	Line   string
}

type Listfile

type Listfile struct {
	UUIDBaseModel
	Name                 string
	AvailableForDownload bool
	AvailableForUse      bool
	FileType             string
	SizeInBytes          uint64
	Lines                uint64
	PendingDelete        bool
	CreatedByUser        User      `gorm:"constraint:OnDelete:SET NULL;"`
	CreatedByUserID      uuid.UUID `gorm:"type:uuid"`
}

func CreateListfile

func CreateListfile(listfile *Listfile) (*Listfile, error)

func GetAllListfiles

func GetAllListfiles() ([]Listfile, error)

func GetAllRulefiles

func GetAllRulefiles() ([]Listfile, error)

func GetAllWordlists

func GetAllWordlists() ([]Listfile, error)

func GetListfile

func GetListfile(id string) (*Listfile, error)

func (*Listfile) Save

func (l *Listfile) Save() error

func (*Listfile) ToDTO

func (w *Listfile) ToDTO() apitypes.ListfileDTO

type PotfileEntry

type PotfileEntry struct {
	UUIDBaseModel
	Hash         string `gorm:"index:idx_potfile_hash,type:hash"`
	PlaintextHex string
	HashType     uint
}

func AddPotfileEntry

func AddPotfileEntry(newEntry *PotfileEntry) (*PotfileEntry, error)

type PotfileSearchResult

type PotfileSearchResult struct {
	Entry *PotfileEntry
	Hash  string
	Found bool
}

func SearchPotfile

func SearchPotfile(hashes []string) ([]PotfileSearchResult, error)

func (PotfileSearchResult) ToDTO

type Project

type Project struct {
	UUIDBaseModel
	Name        string
	Description string
	Hashlists   []Hashlist `gorm:"constraint:OnDelete:CASCADE;"`

	OwnerUser   User      `gorm:"constraint:OnDelete:SET NULL;"`
	OwnerUserID uuid.UUID `gorm:"type:uuid"`

	ProjectShare []ProjectShare `gorm:"constraint:OnDelete:CASCADE;"`
}

func CreateProject

func CreateProject(proj *Project) (*Project, error)

func GetAllProjects

func GetAllProjects() ([]Project, error)

func GetAllProjectsForUser

func GetAllProjectsForUser(user *User) ([]Project, error)

func GetProject

func GetProject(projId string) (*Project, error)

func GetProjectForUser

func GetProjectForUser(projId string, user *User) (*Project, error)

func (*Project) ToDTO

func (p *Project) ToDTO() apitypes.ProjectDTO

type ProjectShare

type ProjectShare struct {
	SimpleBaseModel

	ProjectID uuid.UUID `gorm:"type:uuid"`
	Project   *Project  `gorm:"constraint:OnDelete:CASCADE;"`

	UserID uuid.UUID `gorm:"type:uuid"`
	User   *User     `gorm:"constraint:OnDelete:CASCADE;"`
}

func CreateProjectShare

func CreateProjectShare(share *ProjectShare) (*ProjectShare, error)

type ProjectShares

type ProjectShares []ProjectShare

func GetProjectShares

func GetProjectShares(projId string) (ProjectShares, error)

func (ProjectShares) ToDTO

func (shares ProjectShares) ToDTO() apitypes.ProjectSharesDTO

type RunningJobForUser

type RunningJobForUser struct {
	ProjectID  uuid.UUID
	HashlistID uuid.UUID
	AttackID   uuid.UUID
	JobID      uuid.UUID
}

func (*RunningJobForUser) ToDTO

type RunningJobsForUser

type RunningJobsForUser []RunningJobForUser

func GetAllRunningJobsForUser

func GetAllRunningJobsForUser(user *User) (RunningJobsForUser, error)

func (RunningJobsForUser) ToDTO

type SimpleBaseModel

type SimpleBaseModel struct {
	ID uint `gorm:"primarykey"`
}

type UUIDBaseModel

type UUIDBaseModel struct {
	ID        uuid.UUID `gorm:"primaryKey;type:uuid;default:gen_random_uuid()"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`
}

Same as gorm default, except uses uuid instead of uint

type User

type User struct {
	UUIDBaseModel
	Username     string `gorm:"uniqueIndex"`
	PasswordHash string
	Roles        datatypes.JSONSlice[string]
	APIKeyHash   string // Only for service accounts

	MFAType string
	MFAData datatypes.JSON
}

func GetAllUsers

func GetAllUsers() ([]User, error)

func GetServiceAccountByAPIKey

func GetServiceAccountByAPIKey(key string) (*User, error)

func GetUserByID

func GetUserByID(id string) (*User, error)

func GetUserByUsername

func GetUserByUsername(username string) (*User, error)

func RegisterServiceAccount

func RegisterServiceAccount(username string, apiKey string, roles []string) (*User, error)

caller is responsible for ensuring service account role is present

func RegisterUserWithCredentials

func RegisterUserWithCredentials(username, password string, roles []string) (*User, error)

func RegisterUserWithoutPassword

func RegisterUserWithoutPassword(username string, roles []string) (*User, error)

func (*User) HasRole

func (u *User) HasRole(roleToCheck string) bool

func (*User) ToDTO

func (u *User) ToDTO() apitypes.UserDTO

func (*User) ToMinimalDTO

func (u *User) ToMinimalDTO() apitypes.UserMinimalDTO

Jump to

Keyboard shortcuts

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