forked from u-root/u-root
/
pe.go
51 lines (47 loc) · 910 Bytes
/
pe.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
// Copyright 2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Dump the headers of a PE file.
//
// Synopsis:
// pe [FILENAME]
//
// Description:
// Windows and EFI executables are in the portable executable (PE) format.
// This command prints the headers in a JSON format.
package main
import (
"debug/pe"
"encoding/json"
"flag"
"fmt"
"log"
"os"
)
func main() {
// Parse flags
flag.Parse()
var (
f *pe.File
err error
)
switch flag.NArg() {
case 0:
f, err = pe.NewFile(os.Stdin)
case 1:
filename := flag.Arg(0)
f, err = pe.Open(filename)
default:
log.Fatal("Usage: pe [FILENAME]")
}
if err != nil {
log.Fatal(err)
}
defer f.Close()
// Convert to JSON
j, err := json.MarshalIndent(f, "", "\t")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(j))
}