forked from reconquest/shadowd
/
handle_table_generate.go
180 lines (144 loc) · 3.43 KB
/
handle_table_generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"bufio"
"errors"
"fmt"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
// #cgo LDFLAGS: -lcrypt
// #include <unistd.h>
// #include <crypt.h>
import "C"
type AlgorithmImplementation func(token string) string
func handleTableGenerate(args map[string]interface{}) error {
var (
token = args["<token>"].(string)
amountString = args["-n"].(string)
algorithm = args["-a"].(string)
hashTablesDir = args["-t"].(string)
)
err := validateToken(token)
if err != nil {
return err
}
password, err := getPassword("Enter password: ")
if err != nil {
return err
}
proofPassword, err := getPassword("Retype password: ")
if err != nil {
return err
}
if password != proofPassword {
return fmt.Errorf("specified passwords do not match")
}
amount, err := strconv.Atoi(amountString)
if err != nil {
return err
}
implementation := getAlgorithmImplementation(algorithm)
if implementation == nil {
return errors.New("specified algorithm is not available")
}
hashTablePath := filepath.Join(hashTablesDir, token)
hashTableDir := filepath.Dir(hashTablePath)
if _, err := os.Stat(hashTableDir); os.IsNotExist(err) {
err = os.MkdirAll(hashTableDir, 0700)
if err != nil {
return err
}
}
err = validateTablesDirPermissions(hashTablesDir)
if err != nil {
return err
}
file, err := os.Create(hashTablePath)
if err != nil {
return err
}
defer file.Close()
fmt.Println("Generating hash table...")
for i := 0; i < amount; i++ {
fmt.Fprintln(file, implementation(password))
}
fmt.Printf(
"Hash table %s with %d items successfully created.\n",
hashTablePath,
amount,
)
return file.Close()
}
func getAlgorithmImplementation(algorithm string) AlgorithmImplementation {
switch algorithm {
case "sha256":
return generateSha256
case "sha512":
return generateSha512
}
return nil
}
func generateSha256(password string) string {
shadowRecord := fmt.Sprintf("$5$%s", generateShaSalt())
return C.GoString(C.crypt(C.CString(password), C.CString(shadowRecord)))
}
func generateSha512(password string) string {
shadowRecord := fmt.Sprintf("$6$%s", generateShaSalt())
return C.GoString(C.crypt(C.CString(password), C.CString(shadowRecord)))
}
func generateShaSalt() string {
size := 16
letters := []rune("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM")
salt := make([]rune, size)
for i := 0; i < size; i++ {
salt[i] = letters[rand.Intn(len(letters))]
}
return string(salt)
}
func validateTablesDirPermissions(path string) error {
stat, err := os.Stat(path)
if err != nil {
return err
}
if stat.Mode()&0077 != 0 {
return fmt.Errorf(
"hash tables dir is too open: %s "+
"(should be accessible only by owner)",
stat.Mode())
}
return nil
}
func validateToken(token string) error {
if strings.Contains(token, "../") {
return fmt.Errorf(
"specified token is not available, do not use '../' in token",
)
}
return nil
}
func getPassword(prompt string) (string, error) {
var (
sttyEchoDisable = exec.Command("stty", "-F", "/dev/tty", "-echo")
sttyEchoEnable = exec.Command("stty", "-F", "/dev/tty", "echo")
)
fmt.Print(prompt)
err := sttyEchoDisable.Run()
if err != nil {
return "", err
}
defer func() {
sttyEchoEnable.Run()
fmt.Println()
}()
stdin := bufio.NewReader(os.Stdin)
password, err := stdin.ReadString('\n')
if err != nil {
return "", err
}
password = strings.TrimRight(password, "\n")
return password, nil
}