improve drop root code; remove reliance on C
This commit is contained in:
parent
da4f262454
commit
8cf7b02e1b
|
@ -2,10 +2,10 @@
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
Start by installing Go, Git, GCC and glibc (the program is written in Go, but a tiny bit of C code is used to drop privileges when running as root). On Debian, run:
|
Start by installing Go and Git. On Debian, run:
|
||||||
|
|
||||||
```
|
```
|
||||||
apt install golang git gcc libc6-dev
|
apt install golang git
|
||||||
```
|
```
|
||||||
|
|
||||||
Then fetch and build the program:
|
Then fetch and build the program:
|
||||||
|
@ -66,7 +66,7 @@ systemctl start client-hello-mirror.service
|
||||||
|
|
||||||
## Drop root
|
## Drop root
|
||||||
|
|
||||||
A standard web-facing setup involves using a CA-signed certificate and binding to privileged port 443. For security reasons, the program will drop root privileges imediately after loading the certificate and binding to the specified port. Use the `-u` option to select a user to switch to; the default is `www-data`.
|
A standard web-facing setup involves using a CA-signed certificate and binding to privileged port 443. For security reasons, the program will drop root privileges imediately after loading the certificate and binding to the specified port. Use the `-u` option to select a user to switch to. If you really want to run as root, set `-u root` (not recommended).
|
||||||
|
|
||||||
## Redirect http:// to https://
|
## Redirect http:// to https://
|
||||||
|
|
||||||
|
|
57
server.go
57
server.go
|
@ -14,6 +14,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -21,12 +22,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
import (
|
|
||||||
//#include <unistd.h>
|
|
||||||
//#include <errno.h>
|
|
||||||
"C"
|
|
||||||
)
|
|
||||||
|
|
||||||
type tlsConnectionInfo struct {
|
type tlsConnectionInfo struct {
|
||||||
TlsVersion uint16 `json:"tls_version"`
|
TlsVersion uint16 `json:"tls_version"`
|
||||||
CipherSuite uint16 `json:"cipher_suite"`
|
CipherSuite uint16 `json:"cipher_suite"`
|
||||||
|
@ -280,11 +275,11 @@ func main() {
|
||||||
// Parse arguments
|
// Parse arguments
|
||||||
flag.StringVar(&certFile, "c", "", "path to certificate file")
|
flag.StringVar(&certFile, "c", "", "path to certificate file")
|
||||||
flag.StringVar(&keyFile, "k", "", "path to private key file")
|
flag.StringVar(&keyFile, "k", "", "path to private key file")
|
||||||
flag.StringVar(&userToSwitchTo, "u", "www-data", "user to switch to, if running as root")
|
flag.StringVar(&userToSwitchTo, "u", "", "user to switch to, if running as root")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
hostAndPort = flag.Arg(0)
|
hostAndPort = flag.Arg(0)
|
||||||
if certFile == "" || keyFile == "" || hostAndPort == "" {
|
if certFile == "" || keyFile == "" || hostAndPort == "" {
|
||||||
fmt.Println("usage: client-hello-mirror -c cert.pem -k key.pem host:port")
|
fmt.Println("usage: client-hello-mirror -c cert.pem -k key.pem [-u user] host:port")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -310,36 +305,48 @@ func main() {
|
||||||
defer ln.Close()
|
defer ln.Close()
|
||||||
|
|
||||||
// Drop root
|
// Drop root
|
||||||
if syscall.Getuid() == 0 {
|
if syscall.Getuid() == 0 { // running as root
|
||||||
if userToSwitchTo == "" {
|
if userToSwitchTo == "" {
|
||||||
fmt.Println("Running as root. Please specify an unprivileged user to switch to, using the -u flag")
|
fmt.Println("When running as root, use the -u option to switch to an unprivileged user.")
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
// Get user and group IDs for the user we want to switch to.
|
||||||
userInfo, err := user.Lookup(userToSwitchTo)
|
userInfo, err := user.Lookup(userToSwitchTo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
if userToSwitchTo == "www-data" {
|
os.Exit(1)
|
||||||
fmt.Println("Running as root. Please specify an unprivileged user to switch to, using the -u flag")
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
uid, err := strconv.ParseInt(userInfo.Uid, 10, 32)
|
// Convert group id and user id from string to int.
|
||||||
|
gid, err := strconv.Atoi(userInfo.Gid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
gid, err := strconv.ParseInt(userInfo.Gid, 10, 32)
|
uid, err := strconv.Atoi(userInfo.Uid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
cerr, errno := C.setgid(C.__gid_t(gid))
|
if uid == 0 {
|
||||||
if cerr != 0 {
|
fmt.Println("WARNING: running as root is not recommended!")
|
||||||
log.Fatalln("Unable to set GID due to error:", errno)
|
|
||||||
}
|
}
|
||||||
cerr, errno = C.setuid(C.__uid_t(uid))
|
// Unset supplementary group IDs.
|
||||||
if cerr != 0 {
|
err = syscall.Setgroups([]int{})
|
||||||
log.Fatalln("Unable to set UID due to error:", errno)
|
if err != nil {
|
||||||
|
fmt.Println("Failed to unset supplementary group IDs: " + err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
// Set group ID.
|
||||||
|
err = syscall.Setgid(gid)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failed to set group ID: " + err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
// Set user ID.
|
||||||
|
err = syscall.Setuid(uid)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failed to set user ID: " + err.Error())
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue