From 404792c2ebe4f0d4a94c2c13af3cac5ea4cff53c Mon Sep 17 00:00:00 2001 From: nemunaire Date: Thu, 8 Feb 2018 22:27:30 +0100 Subject: [PATCH] Initial commit --- validator/.gitignore | 1 + validator/arp.go | 61 ++++++++++++++++++++++++++++++++++ validator/index.go | 78 ++++++++++++++++++++++++++++++++++++++++++++ validator/main.go | 34 +++++++++++++++++++ 4 files changed, 174 insertions(+) create mode 100644 validator/.gitignore create mode 100644 validator/arp.go create mode 100644 validator/index.go create mode 100644 validator/main.go diff --git a/validator/.gitignore b/validator/.gitignore new file mode 100644 index 0000000..07d07ff --- /dev/null +++ b/validator/.gitignore @@ -0,0 +1 @@ +validator diff --git a/validator/arp.go b/validator/arp.go new file mode 100644 index 0000000..c30eed0 --- /dev/null +++ b/validator/arp.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + "io/ioutil" + "net" + "strings" +) + +var ARPTable string = "/proc/net/arp" + +type ARPEntry struct { + IP net.IP + HWType int + Flags int + HWAddress net.HardwareAddr + Mask string + Device string +} + +func ARPAnalyze() (ents []ARPEntry, err error) { + var content []byte + if content, err = ioutil.ReadFile(ARPTable); err != nil { + return + } + + for _, line := range strings.Split(string(content), "\n") { + f := strings.Fields(line) + if len(f) > 5 { + var e ARPEntry + + if _, err := fmt.Sscanf(f[1], "0x%x", &e.HWType); err != nil { + continue + } + if _, err := fmt.Sscanf(f[2], "0x%x", &e.Flags); err != nil { + continue + } + + e.IP = net.ParseIP(f[0]) + if e.HWAddress, err = net.ParseMAC(f[3]); err != nil { + continue + } + e.Mask = f[4] + e.Device = f[5] + + ents = append(ents, e) + } + } + + return +} + +func ARPContainsIP(ents []ARPEntry, ip net.IP) *ARPEntry { + for i, e := range ents { + if e.IP.Equal(ip) && e.Flags == 2 { + return &ents[i] + } + } + + return nil +} diff --git a/validator/index.go b/validator/index.go new file mode 100644 index 0000000..a21c7ee --- /dev/null +++ b/validator/index.go @@ -0,0 +1,78 @@ +package main + +import ( + "net/http" + "text/template" +) + +const indextpl = ` + + + + Challenge Forensic - Administration + + + + + + + + +
+
+ + {{"{{ box.title }}"}} {{"{{ box.msg }}"}} +
    +
  • {{"{{ i }}"}}
  • +
+ + +
+
+ +
+ + + + + + + + + +` + +func Index(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") + + if indexTmpl, err := template.New("index").Parse(indextpl); err != nil { + http.Error(w, "Cannot create template: " + err.Error(), http.StatusInternalServerError) + } else if err := indexTmpl.Execute(w, nil); err != nil { + http.Error(w, "An error occurs during template execution: " + err.Error(), http.StatusInternalServerError) + } +} diff --git a/validator/main.go b/validator/main.go new file mode 100644 index 0000000..90a0e85 --- /dev/null +++ b/validator/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "flag" + "log" + "net/http" + "path/filepath" +) + +func main() { + var staticDir string + var bind = flag.String("bind", ":8081", "Bind port/socket") + flag.StringVar(&staticDir, "static", "./static/", "Directory containing static files") + flag.StringVar(&ARPTable, "arp", ARPTable, "Path to ARP table") + //var tftpdLogs = flag.String("tftpd", "/var/logs/tftpd.log", "Path to TFTPd logs") + flag.Parse() + + var err error + + // Sanitize options + log.Println("Checking paths...") + if staticDir, err = filepath.Abs(staticDir); err != nil { + log.Fatal(err) + } + + log.Println("Registering handlers...") + mux := http.NewServeMux() + mux.HandleFunc("/", Index) + mux.Handle("/login", loginChecker{students}) + http.HandleFunc("/", mux.ServeHTTP) + + log.Println("Ready, listening on port", *bind) + http.ListenAndServe(*bind, nil) +}