-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
223 lines (191 loc) · 5.1 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"
)
var (
// Version (set by compiler) is the version of program
Version = "undefined"
// BuildTime (set by compiler) is the program build time in '+%Y-%m-%dT%H:%M:%SZ' format
BuildTime = "undefined"
// GitHash (set by compiler) is the git commit hash of source tree
GitHash = "undefined"
)
func main() {
if err := run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
var usageErr usageError
if errors.As(err, &usageErr) {
flag.Usage()
}
os.Exit(1)
}
}
type usageError string
func (u usageError) Error() string {
return string(u)
}
func run() error {
cfg, err := parseArgs()
if err != nil {
return err
}
card, err := parseProxMark3JSONFile(cfg.InputJSONFile)
if err != nil {
return err
}
return writeNFCFile(cfg.OutputNFCFile, card)
}
type config struct {
InputJSONFile string
OutputNFCFile string
}
func parseArgs() (*config, error) {
var cfg config
flag.StringVar(&cfg.InputJSONFile, "i", "", "input Proxmark3 dump file in JSON format")
flag.StringVar(&cfg.OutputNFCFile, "o", "", "output Flipper file in NFC format")
defaultUsage := flag.Usage
flag.Usage = func() {
defaultUsage()
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "Version: %s\tBuildTime: %v\tGitHash: %s\n", Version, BuildTime, GitHash)
}
flag.Parse()
if cfg.InputJSONFile == "" {
return nil, usageError("please provide input Proxmark3 dump file in JSON format")
}
if cfg.OutputNFCFile == "" {
return nil, usageError("please provide input Proxmark3 dump file in JSON format")
}
return &cfg, nil
}
type hexData []byte
func (h hexData) String() string {
var sb strings.Builder
n := len(h)
for i := 0; i < n-1; i++ {
sb.WriteString(fmt.Sprintf("%02X ", h[i]))
}
if n >= 1 {
sb.WriteString(fmt.Sprintf("%02X", h[n-1]))
}
return sb.String()
}
type mifareCard struct {
UID hexData
ATQA hexData
SAK hexData
Blocks []hexData
}
func parseProxMark3JSONFile(fileName string) (*mifareCard, error) {
jsonFile, err := os.Open(fileName)
if err != nil {
return nil, fmt.Errorf("failed to parse Proxmark3 dump file '%s': %w", fileName, err)
}
defer jsonFile.Close()
return parseProxmark3JSON(jsonFile)
}
func parseProxmark3JSON(r io.Reader) (*mifareCard, error) {
var proxmark3JSON struct {
Created string `json:"Created"`
FileType string `json:"FileType"`
Card struct {
UID string `json:"UID"`
ATQA string `json:"ATQA"`
SAK string `json:"SAK"`
} `json:"Card"`
Blocks map[string]string `json:"blocks"`
}
if err := json.NewDecoder(r).Decode(&proxmark3JSON); err != nil {
return nil, fmt.Errorf("failed to decode Proxmark3 JSON file: %w", err)
}
if proxmark3JSON.Created != "proxmark3" {
return nil, errors.New("JSON file must be produced by Proxmark3")
}
if proxmark3JSON.FileType != "mfcard" {
return nil, errors.New("expecting Mifare card dump")
}
card := &proxmark3JSON.Card
uid, err := decodeHexData(card.UID)
if err != nil {
return nil, fmt.Errorf("cannot parse card UID: %w", err)
}
atqa, err := decodeHexData(card.ATQA)
if err != nil {
return nil, fmt.Errorf("cannot parse card ATQA: %w", err)
}
sak, err := decodeHexData(card.SAK)
if err != nil {
return nil, fmt.Errorf("cannot parse card SAK: %w", err)
}
blocksMap := proxmark3JSON.Blocks
blocksNum := len(blocksMap)
blocks := make([]hexData, blocksNum)
for i := 0; i < blocksNum; i++ {
blockNumStr := strconv.Itoa(i)
blockData, ok := blocksMap[blockNumStr]
if !ok {
return nil, fmt.Errorf("cannot find Mifare card data for block %d", i)
}
bs, err := decodeHexData(blockData)
if err != nil {
return nil, fmt.Errorf("cannot parse block %d data: %w", i, err)
}
// TODO: check block length
blocks[i] = bs
}
return &mifareCard{
UID: uid,
ATQA: atqa,
SAK: sak,
Blocks: blocks,
}, nil
}
func decodeHexData(hexStr string) (bs hexData, err error) {
bs, err = hex.DecodeString(hexStr)
if err != nil {
err = fmt.Errorf("failed to parse hex data '%s': %w", hexStr, err)
}
return
}
func writeNFCFile(fileName string, c *mifareCard) error {
nfcFile, err := os.Create(fileName)
if err != nil {
return fmt.Errorf("failed to create NFC file '%s': %w", fileName, err)
}
defer nfcFile.Close()
return writeNFC(nfcFile, c)
}
func writeNFC(w io.Writer, c *mifareCard) error {
_, err := fmt.Fprintln(w, `Filetype: Flipper NFC device
Version: 2
# Nfc device type can be UID, Mifare Ultralight, Mifare Classic, Bank card
Device type: Mifare Classic
# UID, ATQA and SAK are common for all formats`)
_, err = fmt.Fprintf(w, "UID: %s\n", c.UID)
_, err = fmt.Fprintf(w, "ATQA: %s\n", c.ATQA)
_, err = fmt.Fprintf(w, "SAK: %s\n", c.SAK)
_, err = fmt.Fprintln(w, "# Mifare Classic specific data")
mfSize := 0
switch len(c.Blocks) {
case 64:
mfSize = 1
case 128:
mfSize = 2
case 256:
mfSize = 4
}
_, err = fmt.Fprintf(w, "Mifare Classic type: %dK\n", mfSize)
_, err = fmt.Fprintln(w, `Data format version: 2
# Mifare Classic blocks, '??' means unknown data`)
for i, block := range c.Blocks {
_, err = fmt.Fprintf(w, "Block %d: %s\n", i, block)
}
return err
}