forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SAML authentication feature in Velociraptor (Velocidex#62)
- Loading branch information
Showing
17 changed files
with
749 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
_ "fmt" | ||
"github.com/crewjam/saml/samlsp" | ||
"github.com/sirupsen/logrus" | ||
"net/http" | ||
"net/url" | ||
api_proto "www.velocidex.com/golang/velociraptor/api/proto" | ||
config_proto "www.velocidex.com/golang/velociraptor/config/proto" | ||
"www.velocidex.com/golang/velociraptor/crypto" | ||
"www.velocidex.com/golang/velociraptor/logging" | ||
"www.velocidex.com/golang/velociraptor/users" | ||
) | ||
|
||
var samlMiddleware *samlsp.Middleware | ||
|
||
func MaybeAddSAMLHandlers(config_obj *config_proto.Config, mux *http.ServeMux) error { | ||
if SAMLEnabled(config_obj) { | ||
key, err := crypto.ParseRsaPrivateKeyFromPemStr([]byte(config_obj.GUI.SamlPrivateKey)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cert, err := crypto.ParseX509CertFromPemStr([]byte(config_obj.GUI.SamlCertificate)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
idpMetadataURL, err := url.Parse(config_obj.GUI.SamlIdpMetadataUrl) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rootURL, err := url.Parse(config_obj.GUI.SamlRootUrl) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
samlMiddleware, _ = samlsp.New(samlsp.Options{ | ||
IDPMetadataURL: idpMetadataURL, | ||
URL: *rootURL, | ||
Key: key, | ||
Certificate: cert, | ||
}) | ||
mux.Handle("/saml/", samlMiddleware) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func userAttr(config_obj *config_proto.Config) string { | ||
if config_obj.GUI.SamlUserAttribute == "" { | ||
return "name" | ||
} | ||
return config_obj.GUI.SamlUserAttribute | ||
} | ||
|
||
func SAMLEnabled(config_obj *config_proto.Config) bool { | ||
return config_obj.GUI.SamlCertificate != "" && config_obj.GUI.SamlPrivateKey != "" && | ||
config_obj.GUI.SamlIdpMetadataUrl != "" && config_obj.GUI.SamlRootUrl != "" | ||
} | ||
|
||
func authenticateSAML(config_obj *config_proto.Config, parent http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if token := samlMiddleware.GetAuthorizationToken(r); token != nil { | ||
|
||
username := token.Attributes.Get(userAttr(config_obj)) | ||
user_record, err := users.GetUser(config_obj, username) | ||
if err != nil || user_record.Locked || user_record.Name != username { | ||
w.Header().Set("Content-Type", "text/html; charset=utf-8") | ||
w.WriteHeader(http.StatusUnauthorized) | ||
|
||
fmt.Fprintf(w, ` | ||
<html><body> | ||
Authorization failed. You are not registered on this system as %v. | ||
Contact your system administrator to get an account, then try again. | ||
</body></html> | ||
`, username) | ||
|
||
logging.GetLogger(config_obj, &logging.Audit). | ||
WithFields(logrus.Fields{ | ||
"user": username, | ||
"remote": r.RemoteAddr, | ||
"method": r.Method, | ||
}).Error("User rejected by GUI") | ||
|
||
return | ||
} | ||
|
||
user_info := &api_proto.VelociraptorUser{ | ||
Name: username, | ||
} | ||
|
||
serialized, _ := json.Marshal(user_info) | ||
ctx := context.WithValue( | ||
r.Context(), "USER", string(serialized)) | ||
GetLoggingHandler(config_obj)(parent).ServeHTTP( | ||
w, r.WithContext(ctx)) | ||
return | ||
} | ||
samlMiddleware.RequireAccountHandler(w, r) | ||
}) | ||
} |
Oops, something went wrong.