-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
376 lines (313 loc) · 7.63 KB
/
main.go
File metadata and controls
376 lines (313 loc) · 7.63 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
type Config struct {
APIKey string
BaseURL string
Override bool
Workers int
}
type UploadResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
DecodeID int `json:"decode_id"`
Preview bool `json:"preview"`
}
type StatusResponse struct {
Success bool `json:"success"`
Status string `json:"status"`
Message string `json:"message"`
DecodeID int `json:"decode_id"`
}
type Client struct {
config Config
httpClient *http.Client
}
func NewClient(config Config) *Client {
return &Client{
config: config,
httpClient: &http.Client{Timeout: 120 * time.Second},
}
}
func (c *Client) Upload(filePath string) (*UploadResponse, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
part, err := writer.CreateFormFile("file", filepath.Base(filePath))
if err != nil {
return nil, err
}
if _, err := io.Copy(part, file); err != nil {
return nil, err
}
writer.Close()
req, err := http.NewRequest("POST", c.config.BaseURL+"/accounts/upload/", &buf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("X-API-Key", c.config.APIKey)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode == 401 {
return nil, fmt.Errorf("invalid API key")
}
var result UploadResponse
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("invalid response: %s", string(body))
}
return &result, nil
}
func (c *Client) Status(decodeID int) (*StatusResponse, error) {
url := fmt.Sprintf("%s/accounts/decode/%d/status/", c.config.BaseURL, decodeID)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("X-API-Key", c.config.APIKey)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result StatusResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}
func (c *Client) Download(decodeID int) ([]byte, error) {
url := fmt.Sprintf("%s/accounts/decode/%d/download/", c.config.BaseURL, decodeID)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("X-API-Key", c.config.APIKey)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
func (c *Client) Decode(filePath string) ([]byte, error) {
upload, err := c.Upload(filePath)
if err != nil {
return nil, err
}
if !upload.Success {
if upload.Preview {
return nil, fmt.Errorf("no credits: %s", upload.Message)
}
return nil, fmt.Errorf("upload failed: %s", upload.Message)
}
for {
status, err := c.Status(upload.DecodeID)
if err != nil {
return nil, err
}
switch status.Status {
case "success":
return c.Download(upload.DecodeID)
case "failed":
return nil, fmt.Errorf("decode failed: %s", status.Message)
case "pending":
time.Sleep(500 * time.Millisecond)
default:
return nil, fmt.Errorf("unknown status: %s", status.Status)
}
}
}
func isIonCubeEncoded(filePath string) bool {
file, err := os.Open(filePath)
if err != nil {
return false
}
defer file.Close()
buf := make([]byte, 200)
n, err := file.Read(buf)
if err != nil || n == 0 {
return false
}
content := strings.ToLower(string(buf[:n]))
return strings.Contains(content, "ioncube") ||
strings.Contains(content, "<?php //0") ||
strings.Contains(content, "sg_load")
}
func findPHPFiles(path string) ([]string, error) {
info, err := os.Stat(path)
if err != nil {
return nil, err
}
if !info.IsDir() {
if strings.HasSuffix(strings.ToLower(path), ".php") {
return []string{path}, nil
}
return nil, fmt.Errorf("not a PHP file: %s", path)
}
var files []string
err = filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if !info.IsDir() && strings.HasSuffix(strings.ToLower(p), ".php") {
files = append(files, p)
}
return nil
})
return files, err
}
type Result struct {
File string
Success bool
Message string
}
func processFiles(files []string, client *Client, override bool, workers int) []Result {
jobs := make(chan string, len(files))
results := make(chan Result, len(files))
var wg sync.WaitGroup
for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for file := range jobs {
result := Result{File: file}
if !isIonCubeEncoded(file) {
result.Success = true
result.Message = "skipped (not encoded)"
results <- result
continue
}
decoded, err := client.Decode(file)
if err != nil {
result.Message = err.Error()
results <- result
continue
}
result.Success = true
if override {
if err := os.WriteFile(file, decoded, 0644); err != nil {
result.Success = false
result.Message = fmt.Sprintf("write error: %v", err)
} else {
result.Message = "decoded and overwritten"
}
} else {
ext := filepath.Ext(file)
base := strings.TrimSuffix(file, ext)
outFile := base + "_decoded" + ext
if err := os.WriteFile(outFile, decoded, 0644); err != nil {
result.Success = false
result.Message = fmt.Sprintf("write error: %v", err)
} else {
result.Message = fmt.Sprintf("saved to %s", outFile)
}
}
results <- result
}
}()
}
for _, file := range files {
jobs <- file
}
close(jobs)
go func() {
wg.Wait()
close(results)
}()
var allResults []Result
for r := range results {
allResults = append(allResults, r)
status := "✓"
if !r.Success {
status = "✗"
}
fmt.Printf("%s %s: %s\n", status, r.File, r.Message)
}
return allResults
}
func main() {
apiKey := flag.String("k", "", "API key (account number)")
baseURL := flag.String("u", "https://console.decodephp.io", "Base API URL")
override := flag.Bool("o", false, "Override original files")
workers := flag.Int("w", 4, "Number of concurrent workers")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] <file-or-directory>\n\nOptions:\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if *apiKey == "" {
*apiKey = os.Getenv("IONCUBE_API_KEY")
}
if *apiKey == "" {
fmt.Fprintln(os.Stderr, "Error: API key required (-k or IONCUBE_API_KEY env)")
os.Exit(1)
}
if len(*apiKey) != 16 {
fmt.Fprintln(os.Stderr, "Error: API key must be 16 digits")
os.Exit(1)
}
if flag.NArg() < 1 {
fmt.Fprintln(os.Stderr, "Error: file or directory path required")
flag.Usage()
os.Exit(1)
}
path := flag.Arg(0)
files, err := findPHPFiles(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
if len(files) == 0 {
fmt.Fprintln(os.Stderr, "No PHP files found")
os.Exit(0)
}
fmt.Printf("Found %d PHP file(s)\n\n", len(files))
config := Config{
APIKey: *apiKey,
BaseURL: strings.TrimSuffix(*baseURL, "/"),
Override: *override,
Workers: *workers,
}
client := NewClient(config)
results := processFiles(files, client, *override, *workers)
var success, failed, skipped int
for _, r := range results {
if r.Success {
if strings.Contains(r.Message, "skipped") {
skipped++
} else {
success++
}
} else {
failed++
}
}
fmt.Printf("\nDone: %d decoded, %d skipped, %d failed\n", success, skipped, failed)
if failed > 0 {
os.Exit(1)
}
}