This repository has been archived by the owner on Jan 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
144 lines (114 loc) · 3.65 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/Yara-Rules/yago/yago"
docopt "github.com/docopt/docopt-go"
)
var (
// Name represents the executable name
Name = "YaGo"
// Version of the YaGo
Version = "v0.0.0"
// BuildID is the git commit
BuildID = "-1"
// BuildDate is the compilation date
BuildDate = "-1"
)
func main() {
usage := `YaGo - Parsing Yara rules like a Gopher.
Usage:
yago fileName <fileName> [ --validJSON ]
yago dirName <dirName> [ --validJSON ]
yago indexFile <indexFile> [ cwd <path> ] [ --validJSON ]
yago inputFile <inputFile> outputDir <outputDir> [ --overwrite ] [ --validJSON ]
yago inputFile <inputFile> outputFile <outputFile> [ --overwrite ] [ --validJSON ]
yago -h | --help
yago --version
Options:
-h --help Show this screen.
--overwrite Overwrites existing files [dafault: false].
--validJSON Print rules using a valid JSON format [dafault: false].
--version Show version.
`
version := printVersion()
arguments, _ := docopt.Parse(usage, nil, true, version, false)
if arguments["fileName"].(bool) {
if arguments["<fileName>"].(string) == "" {
errAndExit("ERROR: You must provide a file.")
}
validJSON := arguments["--validJSON"].(bool)
fileName := arguments["<fileName>"].(string)
res := yago.ProcessFile(fileName)
yago.GenerateOutputFromYara(res, validJSON)
} else if arguments["dirName"].(bool) {
if arguments["<dirName>"].(string) == "" {
errAndExit("ERROR: You must provide a directory.")
}
validJSON := arguments["--validJSON"].(bool)
dirName := arguments["<dirName>"].(string)
res := yago.ProcessDir(dirName)
yago.GenerateOutputFromYara(res, validJSON)
} else if arguments["indexFile"].(bool) {
if arguments["<indexFile>"].(string) == "" {
errAndExit("ERROR: You must provide a index file.")
}
cwd := ""
if arguments["cwd"].(bool) {
cwd = arguments["<path>"].(string)
}
validJSON := arguments["--validJSON"].(bool)
indexFile := arguments["<indexFile>"].(string)
res := yago.ProcessIndex(indexFile, cwd)
yago.GenerateOutputFromYara(res, validJSON)
} else if arguments["inputFile"].(bool) {
if arguments["<inputFile>"].(string) == "" {
errAndExit("ERROR: You must provide a input file.")
}
inputFile := arguments["<inputFile>"].(string)
validJSON := arguments["--validJSON"].(bool)
overwrite := arguments["--overwrite"].(bool)
if arguments["outputDir"].(bool) {
if arguments["<outputDir>"].(string) == "" {
errAndExit("ERROR: You must provide a output directory.")
}
outputDir := arguments["<outputDir>"].(string)
res := yago.ProcessInputFile(inputFile, validJSON)
yago.GenerateOutputToYaraDir(res, outputDir, overwrite)
} else if arguments["outputFile"].(bool) {
if arguments["<outputFile>"].(string) == "" {
errAndExit("ERROR: You must provide a output file.")
}
outputFile := arguments["<outputFile>"].(string)
res := yago.ProcessInputFile(inputFile, validJSON)
uniq := yago.UnifyRules(res)
yago.GenerateOutputToYaraFile(uniq, outputFile, overwrite)
}
} else {
errAndExit("Unexpected argument")
}
}
/******************************
** HELPERS **
******************************/
func printVersion() string {
return fmt.Sprintf("%s %s\nBuild date: %s\nBuild ID: %s\nLicense: Apache License 2.0", Name, Version, BuildDate, BuildID)
}
func printError(msg error) {
err := make(map[string]string)
err["error"] = msg.Error()
j, _ := json.Marshal(err)
os.Stderr.Write(j)
os.Exit(1)
}
func errAndExit(msg string) {
os.Stderr.WriteString(msg + "\n")
os.Exit(1)
}
func checkErr(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}