-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (91 loc) · 1.66 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
package main
import (
"bytes"
"compress/zlib"
"encoding/hex"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
)
const (
// PM9SCREW is a flag
PM9SCREW = "\tPM9SCREW\t"
)
var (
pm9ScrewMyCryptKey []byte
)
func main() {
key := flag.String("k", "", "pm9 screw crypt key")
f := flag.String("f", "-", "screwed file, default from stdin")
flag.Parse()
var err error
k2 := *key
if k2 == "" {
k2, err = loadKey()
if err != nil {
log.Fatal(err)
}
}
pm9ScrewMyCryptKey, err = parseKey(k2)
if err != nil {
log.Fatal(err)
}
var bs []byte
if *f == "-" {
bs, err = ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
} else {
bs, err = ioutil.ReadFile(*f)
if err != nil {
log.Fatal(err)
}
}
data, err := decrypt(bs, PM9SCREW, pm9ScrewMyCryptKey)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
}
func loadKey() (string, error) {
bs, err := ioutil.ReadFile(os.ExpandEnv(`$HOME/.php-unscrew`))
if err != nil {
return "", err
}
bs = bytes.TrimSpace(bs)
return string(bs), nil
}
func parseKey(s string) ([]byte, error) {
bs, err := hex.DecodeString(s)
if err != nil {
return nil, err
}
var buf bytes.Buffer
for i, x := range bs {
if i&1 == 0 {
buf.WriteByte(x)
}
}
return buf.Bytes(), nil
}
func decrypt(data []byte, screw string, key []byte) ([]byte, error) {
if len(data) < len(screw) {
return nil, fmt.Errorf("invalid data")
}
data = data[len(screw):]
var buf bytes.Buffer
for i, x := range data {
r := key[(len(data)-i)%len(key)] ^ (^x)
if err := buf.WriteByte(r); err != nil {
return nil, err
}
}
r, err := zlib.NewReader(&buf)
if err != nil {
return nil, err
}
return ioutil.ReadAll(r)
}