Skip to content

Commit

Permalink
added function comments
Browse files Browse the repository at this point in the history
  • Loading branch information
saschamonteiro committed Jul 24, 2024
1 parent 491b8ad commit f49329a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
6 changes: 5 additions & 1 deletion internal/app/app.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// package app holds the application business logic
package app

import (
Expand All @@ -13,6 +14,7 @@ import (
"golang.org/x/sync/errgroup"
)

// StartTlsCollect will start the scan for TLS certificates on the specified networks/ports
func StartTlsCollect(cidrAddressList string, portList string, skipNoDnsFound bool, Assets embed.FS, htmlOut string, jsonOut string) {
cidrAdd := strings.Split(cidrAddressList, ",")
allHosts := []string{}
Expand Down Expand Up @@ -61,6 +63,7 @@ func StartTlsCollect(cidrAddressList string, portList string, skipNoDnsFound boo
}
}

// findHostCerts will scan a host for TLS certs
func findHostCerts(ip string, ports []string, skipNoDnsFound bool) []certs.TlsCert {
serveraddr, err := net.LookupAddr(ip)
cres := []certs.TlsCert{}
Expand All @@ -86,12 +89,12 @@ func findHostCerts(ip string, ports []string, skipNoDnsFound bool) []certs.TlsCe
return cres
}

// hostsFromCIDR will return a list of hosts from a CIDR
func hostsFromCIDR(cidr string) ([]string, error) {
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}

var ips []string
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
ips = append(ips, ip.String())
Expand All @@ -102,6 +105,7 @@ func hostsFromCIDR(cidr string) ([]string, error) {
return ips[1 : len(ips)-1], nil
}

// inc will increment an IP
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
Expand Down
18 changes: 3 additions & 15 deletions internal/certs/certs.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// package certs holds the functions related to TLS certificates
package certs

import (
Expand All @@ -8,6 +9,7 @@ import (
"time"
)

// TlsCert holds the data for a TLS certificate
type TlsCert struct {
HostNameVerified bool `json:"hostnameVerified"`
SubjectCN string `json:"subjectCN"`
Expand All @@ -21,22 +23,17 @@ type TlsCert struct {
HostPort string `json:"hostPort"`
SNIVerified bool `json:"sniVerified"`
}
type TlsPageData struct {
TlsCerts []TlsCert
}

// CheckCert will connect to the host and check if the certificate is valid
func CheckCert(server, port, ip string) TlsCert {
hostnameVerified := false
SNIVerified := false
conf := &tls.Config{InsecureSkipVerify: false}
if server != ip {
conf.ServerName = server
}
// fmt.Printf("\n --> Start: %s\n", server)
conn, err := tls.DialWithDialer(&net.Dialer{Timeout: 1 * time.Second}, "tcp", ip+":"+port, conf)
if err != nil {
// fmt.Printf("DialWithSNI Error %v\n", err)
// fmt.Println("SecureTLS failed, Try InsecureSkipVerify", err)
conn, err = tls.DialWithDialer(&net.Dialer{Timeout: 1 * time.Second}, "tcp", ip+":"+port, &tls.Config{InsecureSkipVerify: true})
if err != nil {
if strings.Contains(err.Error(), "network is unreachable") || strings.Contains(err.Error(), "i/o timeout") || strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "no such host") || strings.Contains(err.Error(), "no route to host") {
Expand All @@ -46,7 +43,6 @@ func CheckCert(server, port, ip string) TlsCert {
}
return TlsCert{}
} else {
// fmt.Println("trying hostname validation")
if strings.Split(server, ":")[0] == conn.ConnectionState().PeerCertificates[0].Subject.CommonName {
hostnameVerified = true
} else {
Expand All @@ -63,21 +59,13 @@ func CheckCert(server, port, ip string) TlsCert {
if err != nil {
fmt.Printf("Hostname doesn't match with certificate: %v\n", err.Error())
} else {
// fmt.Printf("SNIVerified", server, conn.ConnectionState().PeerCertificates[0].Subject.CommonName)
hostnameVerified = true
SNIVerified = true
}

}
// fmt.Printf("ServerName: %v\n", conn.ConnectionState().)
expiry := conn.ConnectionState().PeerCertificates[0].NotAfter
expired := expiry.Before(time.Now())
// expiredString := string(colorGreen) + "false" + string(colorReset)
// if expired {
// expiredString = string(colorRed) + "true" + string(colorReset)
// }
// fmt.Printf("HostnameVerified: %v\nSubject CN: %v\nDNSNames: %v\nIPAddr: %v\nIssuer: %s\nExpiry: %v\nExpired: %v\n\n", hostnameVerified, conn.ConnectionState().PeerCertificates[0].Subject.CommonName, conn.ConnectionState().PeerCertificates[0].DNSNames, conn.ConnectionState().PeerCertificates[0].IPAddresses, conn.ConnectionState().PeerCertificates[0].Issuer, expiry.Format(time.RFC850), expiredString)
// fmt.Printf("-- %+v\n", conn.ConnectionState().PeerCertificates[0])
cert := TlsCert{
HostNameVerified: hostnameVerified,
SubjectCN: conn.ConnectionState().PeerCertificates[0].Subject.CommonName,
Expand Down
24 changes: 18 additions & 6 deletions internal/output/output.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// package output holds the output logic
package output

import (
Expand All @@ -16,11 +17,10 @@ var (
colorReset = "\033[0m"
colorRed = "\033[31m"
colorGreen = "\033[32m"
// colorYellow = "\033[33m"
)

// ShowCertTable will print the table to the console
func ShowCertTable(data []certs.TlsCert) {
//table
table := simpletable.New()
table.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
Expand Down Expand Up @@ -52,16 +52,21 @@ func ShowCertTable(data []certs.TlsCert) {
fmt.Println(table.String())
}

// TlsPageData holds the data for the html page
type TlsPageData struct {
TlsCerts []certs.TlsCert
}

// CreateOutFile will create the output file
func CreateOutFile(data []certs.TlsCert, fileName string, templateFile string, Assets embed.FS) {
// t, _ := template.ParseFiles("certs.tmpl")
t, _ := template.ParseFS(Assets, templateFile)
f, err := os.Create(fileName)
if err != nil {
fmt.Println("error create file: ", err)
return
}
defer f.Close()
err = t.Execute(f, certs.TlsPageData{
err = t.Execute(f, TlsPageData{
TlsCerts: data,
})
if err != nil {
Expand All @@ -71,19 +76,21 @@ func CreateOutFile(data []certs.TlsCert, fileName string, templateFile string, A

}

type Meta struct {
// JsonMeta holds the metadata
type JsonMeta struct {
Certs []certs.TlsCert `json:"certs"`
DateTime time.Time `json:"dateTime"`
}

// CreateJsonFile will create the json file
func CreateJsonFile(data []certs.TlsCert, fileName string) {
f, err := os.Create(fileName)
if err != nil {
fmt.Println("error create file: ", err)
return
}
defer f.Close()
meta := Meta{
meta := JsonMeta{
Certs: data,
DateTime: time.Now(),
}
Expand All @@ -95,18 +102,23 @@ func CreateJsonFile(data []certs.TlsCert, fileName string) {
f.Write(jsonData)
}

// exp formats the expired status
func exp(s bool) string {
if s {
return fmt.Sprintf("%s%v%s", colorRed, s, colorReset)
}
return fmt.Sprintf("%s%v%s", colorGreen, s, colorReset)
}

// valid formats the valid status
func valid(s bool) string {
if !s {
return fmt.Sprintf("%s%v%s", colorRed, s, colorReset)
}
return fmt.Sprintf("%s%v%s", colorGreen, s, colorReset)
}

// truncateText will truncate the text to given size
func truncateText(s string, max int) string {
if max >= len(s) {
return s
Expand Down

0 comments on commit f49329a

Please sign in to comment.