-
Notifications
You must be signed in to change notification settings - Fork 5
/
hashlist.go
213 lines (174 loc) · 5.52 KB
/
hashlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package controllers
import (
"net/http"
log "github.com/sirupsen/logrus"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/lachlan2k/phatcrack/api/internal/accesscontrol"
"github.com/lachlan2k/phatcrack/api/internal/auth"
"github.com/lachlan2k/phatcrack/api/internal/db"
"github.com/lachlan2k/phatcrack/api/internal/fleet"
"github.com/lachlan2k/phatcrack/api/internal/hashcathelpers"
"github.com/lachlan2k/phatcrack/api/internal/util"
"github.com/lachlan2k/phatcrack/common/pkg/apitypes"
)
func HookHashlistEndpoints(api *echo.Group) {
api.GET("/ping", func(c echo.Context) error {
return c.String(http.StatusOK, "pong hashlist")
})
api.POST("/create", handleHashlistCreate)
api.GET("/:hashlist-id", handleHashlistGet)
api.DELETE("/:hashlist-id", handleHashlistDelete)
api.GET("/:hashlist-id/attacks", handleAttackGetAllForHashlist)
api.GET("/:hashlist-id/attacks-with-jobs", handleAttacksAndJobsForHashlist)
}
func handleHashlistGetAllForProj(c echo.Context) error {
projId := c.Param("proj-id")
if !util.AreValidUUIDs(projId) {
return echo.ErrBadRequest
}
user := auth.UserFromReq(c)
if user == nil {
return echo.ErrForbidden
}
allowed, err := accesscontrol.HasRightsToProjectID(user, projId)
if err != nil {
return err
}
if !allowed {
return echo.ErrForbidden
}
hashlists, err := db.GetAllHashlistsForProject(projId)
if err != nil {
return util.ServerError("Failed to get hashlists", err)
}
res := apitypes.HashlistResponseMultipleDTO{}
res.Hashlists = make([]apitypes.HashlistDTO, len(hashlists))
for i, hashlist := range hashlists {
res.Hashlists[i] = hashlist.ToDTO(false)
}
return c.JSON(http.StatusOK, res)
}
func handleHashlistGet(c echo.Context) error {
hashlistId := c.Param("hashlist-id")
if !util.AreValidUUIDs(hashlistId) {
return echo.ErrBadRequest
}
user := auth.UserFromReq(c)
if user == nil {
return echo.ErrForbidden
}
hashlist, err := db.GetHashlistWithHashes(hashlistId)
if err == db.ErrNotFound {
return echo.ErrNotFound
}
if err != nil {
return util.ServerError("Failed to load hashlist", err)
}
allowed, err := accesscontrol.HasRightsToProjectID(user, hashlist.ProjectID.String())
if err != nil {
return util.ServerError("Failed to check project", err)
}
if !allowed {
return echo.ErrForbidden
}
return c.JSON(http.StatusOK, hashlist.ToDTO(true))
}
func handleHashlistDelete(c echo.Context) error {
hashlistId := c.Param("hashlist-id")
if !util.AreValidUUIDs(hashlistId) {
return echo.ErrBadRequest
}
user := auth.UserFromReq(c)
if user == nil {
return echo.ErrForbidden
}
hashlist, err := db.GetHashlistWithHashes(hashlistId)
if err == db.ErrNotFound {
return echo.ErrNotFound
}
if err != nil {
return util.ServerError("Failed to load hashlist", err)
}
allowed, err := accesscontrol.HasRightsToProjectID(user, hashlist.ProjectID.String())
if err != nil {
return util.ServerError("Failed to check project", err)
}
if !allowed {
return echo.ErrForbidden
}
jobsToStop, err := db.GetJobsForHashlist(hashlistId)
if err != nil {
return util.ServerError("Failed to get jobs to kill", err)
}
AuditLog(c, log.Fields{
"hashlist_id": hashlist.ID.String(),
"hashlist_name": hashlist.Name,
"project_id": hashlist.ProjectID.String(),
}, "User is deleting hashlist")
err = db.HardDelete(hashlist)
if err != nil {
return util.ServerError("Failed to delete project", err)
}
for _, job := range jobsToStop {
fleet.StopJob(job, db.JobStopReasonUserStopped)
}
return c.JSON(http.StatusOK, "ok")
}
func handleHashlistCreate(c echo.Context) error {
user := auth.UserFromReq(c)
if user == nil {
return echo.ErrForbidden
}
req, err := util.BindAndValidate[apitypes.HashlistCreateRequestDTO](c)
if err != nil {
return err
}
// Access control
allowed, err := accesscontrol.HasRightsToProjectID(user, req.ProjectID)
if err != nil {
return err
}
if !allowed {
return echo.ErrForbidden
}
log.Infof("Validating hashes for new hashlist (%q) for project %q", req.Name, req.ProjectID)
// Ensure provided algorithm type is valid and normalize
normalizedHashes, err := hashcathelpers.NormalizeHashes(req.InputHashes, req.HashType, req.HasUsernames)
if err != nil {
log.Warnf("Failed to validated hashes for project %q because %v", req.ProjectID, err)
return echo.NewHTTPError(http.StatusBadRequest, "Failed to validate and normalize hashes. Please ensure your hashes are valid for the given hash type.").SetInternal(err)
}
hashes := make([]db.HashlistHash, len(normalizedHashes))
for i, inputHash := range req.InputHashes {
hashes[i].InputHash = inputHash
hashes[i].NormalizedHash = normalizedHashes[i]
}
newHashlist, err := db.CreateHashlist(&db.Hashlist{
ProjectID: uuid.MustParse(req.ProjectID),
Name: req.Name,
Version: 1,
HashType: req.HashType,
Hashes: hashes,
})
if err != nil {
return util.ServerError("Failed to create hashlist", err)
}
AuditLog(c, log.Fields{
"hashlist_name": newHashlist.Name,
"hashlist_id": newHashlist.ID.String(),
"project_id": req.ProjectID,
}, "User created a new hashlist")
numFromPotfile, err := db.PopulateHashlistFromPotfile(newHashlist.ID.String())
if err != nil {
log.WithFields(log.Fields{
"hashlist_name": newHashlist.Name,
"hashlist_id": newHashlist.ID.String(),
"project_id": req.ProjectID,
}).WithError(err).Warn("Failed to populated hashlist from potfile")
}
return c.JSON(http.StatusCreated, apitypes.HashlistCreateResponseDTO{
ID: newHashlist.ID.String(),
NumPopulatedFromPotfile: numFromPotfile,
})
}