-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.go
317 lines (245 loc) · 7.47 KB
/
actions.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"errors"
"fmt"
"strings"
"github.com/siddontang/go/ioutil2"
ntfs "github.com/corebreaker/ntfstool/core"
"github.com/corebreaker/ntfstool/core/data"
datafile "github.com/corebreaker/ntfstool/core/data/file"
"github.com/corebreaker/ntfstool/inspect"
)
var (
actions = []iActionDef{
tDefaultActionDef{handler: do_help, name: "help"},
// Commands using input/output files
tStringActionDef{handler: do_set_source, name: "in", next: true},
tStringActionDef{handler: do_set_destination, name: "out", next: true},
tStringActionDef{handler: do_set_file, name: "file", next: true},
tStringActionDef{handler: do_set_from_param, name: "from", next: true},
tStringActionDef{handler: do_set_into_param, name: "to", next: true},
tDefaultActionDef{handler: do_record_count, name: "record-count"},
tIntegerActionDef{handler: do_show, name: "show"},
tIntegerActionDef{handler: do_head, name: "head"},
tIntegerActionDef{handler: do_tail, name: "tail"},
tIntegerActionDef{handler: do_offsets, name: "positions"},
tDefaultActionDef{handler: do_find_state, name: "find"},
tStringActionDef{handler: do_show_id, name: "id"},
tStringActionDef{handler: do_show_parent, name: "parent"},
tIntegerActionDef{handler: do_show_parent_ref, name: "parent-ref"},
tIntegerActionDef{handler: do_position, name: "at", offset: true},
tBoolActionDef{handler: do_check, name: "check"},
tIntegerActionDef{handler: do_show_attribute, name: "show-attr"},
tStringActionDef{handler: do_show_mft, name: "show-mft"},
tDefaultActionDef{handler: do_listnames, name: "list-names"},
tDefaultActionDef{handler: do_shownames, name: "show-names"},
tDefaultActionDef{handler: do_scan, name: "scan"},
tStringActionDef{handler: do_list_files, name: "ls"},
tStringActionDef{handler: do_move_to, name: "mv"},
tStringActionDef{handler: do_copy_to, name: "cp"},
tStringActionDef{handler: do_remove_from, name: "rm"},
tStringActionDef{handler: do_make_dir, name: "mkdir"},
tDefaultActionDef{handler: do_compact, name: "compact"},
// Commands to use partition with the help of input/output files
tConfigActionDef{handler: do_open_disk},
tStringActionDef{handler: do_save_file, name: "save"},
tDefaultActionDef{handler: do_fillinfo, name: "fill"},
tBoolActionDef{handler: do_fixmft, name: "fix-mft"},
tBoolActionDef{handler: do_mkfilelist, name: "make-filelist"},
tDefaultActionDef{handler: do_complete, name: "complete"},
// Command to explore partition
tIntegerActionDef{handler: do_start, name: "start", next: true, offset: true},
tIntegerActionDef{handler: do_mft, name: "mft", next: true, offset: true},
tStringActionDef{handler: do_count, name: "count"},
tDefaultActionDef{handler: do_mftnames, name: "mft-names"},
tIntegerActionDef{handler: do_record, name: "record", offset: true},
tIntegerActionDef{handler: do_sector, name: "sector", offset: true},
tIntegerActionDef{handler: do_cluster, name: "cluster", offset: true},
tIntegerActionDef{handler: do_file_num, name: "file-num"},
}
)
func do_open_disk(arg *tActionArg) error {
disk, err := inspect.OpenNtfsDisk(arg.partition, 0)
if err != nil {
return err
}
arg.disk = disk
return nil
}
func do_start(offset int64, arg *tActionArg) error {
arg.disk.SetStart(offset)
return nil
}
func do_mft(offset int64, arg *tActionArg) error {
arg.disk.SetMftShift(offset)
return nil
}
func do_set_source(source string, arg *tActionArg) error {
if source == "" {
return ntfs.WrapError(fmt.Errorf("No source file specified"))
}
if !ioutil2.FileExists(source) {
return ntfs.WrapError(fmt.Errorf("Can't access to source file"))
}
f, err := ntfs.OpenFile(source, ntfs.OPEN_RDONLY)
if err != nil {
return err
}
arg.source = f
return nil
}
func do_set_destination(dest string, arg *tActionArg) error {
if dest == "" {
return ntfs.WrapError(fmt.Errorf("No destination file specified"))
}
f, err := ntfs.OpenFile(dest, ntfs.OPEN_WRONLY)
if err != nil {
return err
}
arg.dest = f
return nil
}
func do_set_file(pathname string, arg *tActionArg) error {
if pathname == "" {
return ntfs.WrapError(fmt.Errorf("No destination file specified"))
}
f, err := ntfs.OpenFile(pathname, ntfs.OPEN_RDWR)
if err != nil {
return err
}
arg.file = f
return nil
}
func do_set_from_param(val string, arg *tActionArg) error {
arg.from = val
return nil
}
func do_set_into_param(val string, arg *tActionArg) error {
arg.into = val
return nil
}
func do_count(pattern string, arg *tActionArg) error {
if pattern == "" {
return ntfs.WrapError(errors.New("No pattern specified"))
}
patterns := strings.Split(pattern, ",")
list := make([][]byte, len(patterns)-1)
for _, p := range patterns[1:] {
if p != "" {
list = append(list, []byte(p))
}
}
indexes, err := inspect.FindPositionsWithPattern(arg.partition, []byte(patterns[0]), list...)
if err != nil {
return err
}
lengths := make([]int, len(patterns))
for i, idx_list := range indexes {
lengths[i] = len(idx_list)
}
fmt.Println("Results:")
for i, idx_list := range indexes {
sz := lengths[i]
if sz > 10 {
sz = 10
}
fmt.Println(fmt.Sprintf(" - `%s`: Count=%d Firsts=%v", patterns[i], lengths[i], idx_list[:sz]))
}
return nil
}
func do_record_count(arg *tActionArg) error {
src, err := arg.GetInput()
if err != nil {
return err
}
fmt.Println("Reading")
records, err := datafile.MakeDataReader(src, datafile.ANY_FILEFORMAT)
if err != nil {
return err
}
defer ntfs.DeferedCall(records.Close)
fmt.Println()
fmt.Println("Format:", records.GetFormatName())
fmt.Println()
for rec_type, count := range records.GetCounts() {
fmt.Println(rec_type.GetLabel(), "=", count)
}
fmt.Println()
fmt.Println("Total Record Count =", records.GetCount())
return nil
}
func do_mftnames(arg *tActionArg) error {
fmt.Println()
fmt.Println("Names:")
for i := data.FileRef(0); true; i++ {
var val ntfs.RecordHeader
if err := arg.disk.ReadRecordHeaderFromRef(i, &val); err != nil {
return err
}
if !val.Type.IsGood() {
return nil
}
if val.Type != ntfs.RECTYP_FILE {
continue
}
var file ntfs.FileRecord
if err := arg.disk.ReadFileRecordFromRef(i, &file); err != nil {
return err
}
name, err := arg.disk.GetFileRecordFilename(&file)
if err != nil {
return err
}
if name == "" {
continue
}
fmt.Println(fmt.Sprintf(" - %d : %s", i.GetFileIndex(), name))
}
return nil
}
func do_record(record_num int64, arg *tActionArg) error {
var record ntfs.RecordHeader
if err := arg.disk.ReadRecordHeader(record_num, &record); err != nil {
return err
}
fmt.Println()
fmt.Println("Record:")
ntfs.PrintStruct(record)
return nil
}
func do_sector(offset int64, arg *tActionArg) error {
var sector [512]byte
num := (offset + 511) / 512
disk := arg.disk.GetDisk()
defer ntfs.DeferedCall(disk.Close)
if err := disk.ReadSector(num, sector[:]); err != nil {
return err
}
fmt.Println()
fmt.Println("Content:")
fmt.Printf("Content at %d:", num)
fmt.Println()
ntfs.PrintBytes(sector[:])
return nil
}
func do_cluster(offset int64, arg *tActionArg) error {
var cluster [4096]byte
num := (offset + 4095) / 4096
disk := arg.disk.GetDisk()
defer ntfs.DeferedCall(disk.Close)
if err := disk.ReadCluster(num, cluster[:]); err != nil {
return err
}
fmt.Println()
fmt.Printf("Content at %d:", num*8)
fmt.Println()
ntfs.PrintBytes(cluster[:])
return nil
}
func do_scan(arg *tActionArg) error {
destination, err := arg.GetOutput()
if err != nil {
return err
}
return inspect.Scan(arg.partition, destination)
}