| 1 | package tls |
| 2 | |
| 3 | import ( |
| 4 | "crypto/rand" |
| 5 | "crypto/rsa" |
| 6 | "crypto/x509" |
| 7 | "crypto/x509/pkix" |
| 8 | "encoding/pem" |
| 9 | "math/big" |
| 10 | "os" |
| 11 | "strconv" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/bettercap/bettercap/session" |
| 15 | ) |
| 16 | |
| 17 | type CertConfig struct { |
| 18 | Bits int |
| 19 | Country string |
| 20 | Locality string |
| 21 | Organization string |
| 22 | OrganizationalUnit string |
| 23 | CommonName string |
| 24 | } |
| 25 | |
| 26 | var ( |
| 27 | DefaultLegitConfig = CertConfig{ |
| 28 | Bits: 4096, |
| 29 | Country: "US", |
| 30 | Locality: "", |
| 31 | Organization: "bettercap devteam", |
| 32 | OrganizationalUnit: "https://bettercap.org/", |
| 33 | CommonName: "bettercap", |
| 34 | } |
| 35 | DefaultSpoofConfig = CertConfig{ |
| 36 | Bits: 4096, |
| 37 | Country: "US", |
| 38 | Locality: "Scottsdale", |
| 39 | Organization: "GoDaddy.com, Inc.", |
| 40 | OrganizationalUnit: "https://certs.godaddy.com/repository/", |
| 41 | CommonName: "Go Daddy Secure Certificate Authority - G2", |
| 42 | } |
| 43 | ) |
| 44 | |
| 45 | func CertConfigToModule(prefix string, m *session.SessionModule, defaults CertConfig) { |
| 46 | m.AddParam(session.NewIntParameter(prefix+".certificate.bits", strconv.Itoa(defaults.Bits), |
| 47 | "Number of bits of the RSA private key of the generated HTTPS certificate.")) |
| 48 | m.AddParam(session.NewStringParameter(prefix+".certificate.country", defaults.Country, ".*", |
| 49 | "Country field of the generated HTTPS certificate.")) |
| 50 | m.AddParam(session.NewStringParameter(prefix+".certificate.locality", defaults.Locality, ".*", |
| 51 | "Locality field of the generated HTTPS certificate.")) |
| 52 | m.AddParam(session.NewStringParameter(prefix+".certificate.organization", defaults.Organization, ".*", |
| 53 | "Organization field of the generated HTTPS certificate.")) |
| 54 | m.AddParam(session.NewStringParameter(prefix+".certificate.organizationalunit", defaults.OrganizationalUnit, ".*", |
| 55 | "Organizational Unit field of the generated HTTPS certificate.")) |
| 56 | m.AddParam(session.NewStringParameter(prefix+".certificate.commonname", defaults.CommonName, ".*", |
| 57 | "Common Name field of the generated HTTPS certificate.")) |
| 58 | } |
| 59 | |
| 60 | func CertConfigFromModule(prefix string, m session.SessionModule) (cfg CertConfig, err error) { |
| 61 | if err, cfg.Bits = m.IntParam(prefix + ".certificate.bits"); err != nil { |
| 62 | return cfg, err |
| 63 | } else if err, cfg.Country = m.StringParam(prefix + ".certificate.country"); err != nil { |
| 64 | return cfg, err |
| 65 | } else if err, cfg.Locality = m.StringParam(prefix + ".certificate.locality"); err != nil { |
| 66 | return cfg, err |
| 67 | } else if err, cfg.Organization = m.StringParam(prefix + ".certificate.organization"); err != nil { |
| 68 | return cfg, err |
| 69 | } else if err, cfg.OrganizationalUnit = m.StringParam(prefix + ".certificate.organizationalunit"); err != nil { |
| 70 | return cfg, err |
| 71 | } else if err, cfg.CommonName = m.StringParam(prefix + ".certificate.commonname"); err != nil { |
| 72 | return cfg, err |
| 73 | } |
| 74 | return cfg, err |
| 75 | } |
| 76 | |
| 77 | func CreateCertificate(cfg CertConfig, ca bool) (*rsa.PrivateKey, []byte, error) { |
| 78 | priv, err := rsa.GenerateKey(rand.Reader, cfg.Bits) |
| 79 | if err != nil { |
| 80 | return nil, nil, err |
| 81 | } |
| 82 | |
| 83 | notBefore := time.Now() |
| 84 | aYear := time.Duration(365*24) * time.Hour |
| 85 | notAfter := notBefore.Add(aYear) |
| 86 | serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) |
| 87 | serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) |
| 88 | if err != nil { |
| 89 | return nil, nil, err |
| 90 | } |
| 91 | |
| 92 | template := x509.Certificate{ |
| 93 | SerialNumber: serialNumber, |
| 94 | Subject: pkix.Name{ |
| 95 | Country: []string{cfg.Country}, |
| 96 | Locality: []string{cfg.Locality}, |
| 97 | Organization: []string{cfg.Organization}, |
| 98 | OrganizationalUnit: []string{cfg.OrganizationalUnit}, |
| 99 | CommonName: cfg.CommonName, |
| 100 | }, |
| 101 | NotBefore: notBefore, |
| 102 | NotAfter: notAfter, |
| 103 | KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, |
| 104 | ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, |
| 105 | BasicConstraintsValid: true, |
| 106 | IsCA: ca, |
| 107 | } |
| 108 | |
| 109 | cert, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv) |
| 110 | if err != nil { |
| 111 | return nil, nil, err |
| 112 | } |
| 113 | |
| 114 | return priv, cert, err |
| 115 | } |
| 116 | |
| 117 | func Generate(cfg CertConfig, certPath string, keyPath string, ca bool) error { |
| 118 | keyFile, err := os.Create(keyPath) |
| 119 | if err != nil { |
| 120 | return err |
| 121 | } |
| 122 | defer keyFile.Close() |
| 123 | |
| 124 | certFile, err := os.Create(certPath) |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | defer certFile.Close() |
| 129 | |
| 130 | priv, cert, err := CreateCertificate(cfg, ca) |
| 131 | if err != nil { |
| 132 | return err |
| 133 | } |
| 134 | |
| 135 | if err := pem.Encode(keyFile, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}); err != nil { |
| 136 | return err |
| 137 | } |
| 138 | |
| 139 | return pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: cert}) |
| 140 | } |