Skip to content

Instantly share code, notes, and snippets.

@bradleypeabody
Last active June 14, 2016 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradleypeabody/3f2c65f665ea25373028cb7fb98380df to your computer and use it in GitHub Desktop.
Save bradleypeabody/3f2c65f665ea25373028cb7fb98380df to your computer and use it in GitHub Desktop.
Use ident to get username of incoming connection
// Example in Go of using the ident protocol to extract the username of the connecting user.
// The idea is to use this on corporate networks to identify users logged in to a Windows
// RDP machine by their ActiveDirectory username.
// NOTE: For Windows, this is a good ident server https://sourceforge.net/projects/retinascan/
// that supports multiple users and all that good stuff.
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
parts := strings.SplitN(r.RemoteAddr, ":", 2)
// whatever, just my test for a specific machine - may need to do something slightly different for ipv6, since that contains colons
if parts[0] == "192.168.27.4" {
port := parts[1]
conn, err := net.Dial("tcp", parts[0]+":113")
if err != nil {
panic(err)
}
// cmd := fmt.Sprintf("80, %s", port)
cmd := fmt.Sprintf("%s, 9000", port) // FIXME: should look at actual server port number
log.Printf("SENDING IDENT COMMAND: %s\n", cmd)
fmt.Fprintf(conn, "%s\r\n", cmd)
result, err := bufio.NewReader(conn).ReadString('\n')
log.Printf("Identd result: %s\n", result)
resultParts := strings.Split(result, ":")
if len(resultParts) > 3 && strings.TrimSpace(resultParts[1]) == "USERID" {
userName := strings.TrimSpace(resultParts[3])
log.Printf("USERNAME: %s\n", userName)
} else {
log.Printf("Unknown result\n")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment