This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDF_Decrypt.go
245 lines (231 loc) · 6.37 KB
/
PDF_Decrypt.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
package main
import (
"errors"
"flag"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/fatih/color"
"github.com/pdfcpu/pdfcpu/pkg/api"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
)
var (
buildTime, commitId, versionData, author string
inputFolder, outputFolder, passWords string
help, version bool
)
var (
fileList []string
errorList []string
)
func Flag() {
flag.BoolVar(&help, "h", false, "Display help information")
flag.BoolVar(&help, "help", false, "Display help information")
flag.BoolVar(&version, "v", false, "PDF_Decrypt version")
flag.BoolVar(&version, "version", false, "PDF_Decrypt version")
flag.StringVar(&inputFolder, "in", "PDF", "in explorer")
flag.StringVar(&outputFolder, "out", "out", "out explorer")
flag.StringVar(&passWords, "pass", "123456", "password ('abc' | 'abc\\def\\ghi')")
}
func passWordLists(passWord string) []string {
return strings.Split(passWord, "\\")
}
func adsPath(folder string) string {
var (
adspath string
err error
)
if adspath, err = filepath.Abs(folder); err != nil {
_, _ = color.New(color.FgYellow).Println(err)
os.Exit(1)
}
return adspath
}
func relPath(basePath, targPath string) (relPath string) {
var err error
if relPath, err = filepath.Rel(adsPath(basePath), targPath); err != nil {
_, _ = color.New(color.FgYellow).Println(err)
os.Exit(1)
}
return relPath
}
func inputFolders(filePath string) []string {
if err := filepath.Walk(adsPath(filePath), func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".pdf" {
fileList = append(fileList, path)
}
if info.IsDir() {
var outFileName, outFilePath string
outFileName = relPath(adsPath(inputFolder), path)
outFilePath = filepath.Join(outputFolder, outFileName)
if _, err := os.Stat(outFilePath); err != nil {
if err := os.Mkdir(outFilePath, 0755); err != nil {
_, _ = color.New(color.FgYellow).Println(err)
os.Exit(1)
}
}
}
return nil
}); err != nil {
_, _ = color.New(color.FgYellow).Println(err)
os.Exit(1)
}
return fileList
}
func outPutFolder(filePath string) {
filePath = adsPath(filePath)
if _, err := os.Stat(filePath); err != nil {
if !os.IsExist(err) {
err := os.Mkdir(filePath, 0755)
if err != nil {
_, _ = color.New(color.FgYellow).Println(err)
os.Exit(1)
}
}
}
}
func outPutFolderClean(filePath string) {
var (
files []fs.FileInfo
err error
)
filePath = adsPath(filePath)
if files, err = ioutil.ReadDir(filePath); err != nil {
_, _ = color.New(color.FgYellow).Println(err)
os.Exit(1)
}
if len(files) == 0 {
if filePath == outputFolder {
return
}
if err := os.Remove(filePath); err != nil {
_, _ = color.New(color.FgYellow).Println(err)
os.Exit(1)
}
_, _ = color.New(color.FgRed).Printf(" Remove Folder: ")
_, _ = color.New(color.FgWhite).Println(filePath)
return
}
for _, v := range files {
if v.IsDir() {
outPutFolderClean(path.Join(filePath, v.Name()))
}
}
}
func Decrypt(fileList []string) {
for _, v := range fileList {
for _, p := range passWordLists(passWords) {
if DecryptFile(v, p) {
break
}
}
}
}
func DecryptFile(filePath, pass string) bool {
//conf.UserPW = i
filePath = adsPath(filePath)
if !pdfcpu.MemberOf(pdfcpu.ConfigPath, []string{"default", "disable"}) {
if err := pdfcpu.EnsureDefaultConfigAt(pdfcpu.ConfigPath); err != nil {
_, _ = color.New(color.FgYellow).Println(err)
os.Exit(1)
}
}
conf := pdfcpu.NewDefaultConfiguration()
conf.OwnerPW = pass
_, _ = color.New(color.FgCyan).Printf(" Decrypting: ")
_, _ = color.New(color.FgWhite).Printf(path.Base(filePath))
outFileRouter := relPath(adsPath(inputFolder), filePath)
outFilePath := path.Join(adsPath(outputFolder), outFileRouter)
if err := api.DecryptFile(filePath, outFilePath, conf); err != nil {
type errorFile struct {
Index int
Error error
Status bool
}
var (
errFile = errorFile{
Index: 0,
Error: err,
Status: false,
}
errPath string
)
if strings.Contains(errFile.Error.Error(), "not encrypted") {
_, _ = color.New(color.FgGreen).Println(" Not encrypted")
var l3 []byte
if l3, err = ioutil.ReadFile(filePath); err != nil {
_, _ = color.New(color.FgYellow).Println(" ", err)
os.Exit(1)
}
if err = ioutil.WriteFile(outFilePath, l3, 0644); err != nil {
_, _ = color.New(color.FgYellow).Println(" ", err)
os.Exit(1)
}
return true
}
for i, v := range errorList {
if strings.Contains(v, relPath(adsPath(inputFolder), filePath)) {
errFile.Status = true
errFile.Index = i
}
}
errPath = relPath(adsPath(inputFolder), filePath)
if strings.Contains(errFile.Error.Error(), "correct password") {
errFile.Error = errors.New("PassError:" + pass)
errPath += ":PassError\n"
} else {
errPath += fmt.Sprintf(":[%v]\n", errFile.Error.Error())
}
if errFile.Status {
errorList[errFile.Index] = errPath
} else {
errorList = append(errorList, errPath)
}
_, _ = color.New(color.FgYellow).Println(fmt.Sprintf(" %v", errFile.Error))
return false
}
_, _ = color.New(color.FgGreen).Printf(" Decrypted!")
_, _ = color.New(color.FgWhite).Println(" PassWord:", pass)
return true
}
func main() {
_, _ = color.New(color.FgMagenta).Println("\n --------------------------------------")
defer func() {
_, _ = color.New(color.FgMagenta).Println(" --------------------------------------\n")
}()
Flag()
flag.Parse()
//if help || len(os.Args) == 1 {
// fmt.Println(` Usage of PDF_Decrypt:
//-help -h
// Display help information
//-in string
// in explorer
//-out string
// out explorer
//-pass string
// password (abc | abc\\def\\ghi)
//-version -v
// PDF_Decrypt version`)
// return
//}
if version {
_, _ = color.New(color.FgMagenta).Println(" | Version:", versionData)
_, _ = color.New(color.FgMagenta).Println(" | BuildTime:", buildTime)
_, _ = color.New(color.FgMagenta).Println(" | Author:", author)
_, _ = color.New(color.FgMagenta).Println(" | CommitId:", commitId)
return
}
_, _ = color.New(color.FgMagenta).Println(" Start PDF_Decrypt ...")
outPutFolder(outputFolder)
Decrypt(inputFolders(inputFolder))
for i := 0; i < 50; i++ {
outPutFolderClean(outputFolder)
}
_, _ = color.New(color.FgGreen).Printf("\n Decrypted %v File\n", len(fileList))
_, _ = color.New(color.FgGreen).Printf(" Decrypt Error %v Files: %v\n", len(errorList), errorList)
}