This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpixel.go
102 lines (84 loc) · 2.91 KB
/
pixel.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 akamai
import (
"errors"
"io"
"regexp"
"strconv"
"strings"
)
var (
pixelHtmlExpr = regexp.MustCompile(`bazadebezolkohpepadr="(\d+)"`)
pixelScriptUrlExpr = regexp.MustCompile(`src="(https?://.+/akam/\d+/\w+)"`)
pixelScriptVarExpr = regexp.MustCompile(`g=_\[(\d+)]`)
pixelScriptStringArrayExpr = regexp.MustCompile(`var _=\[(.+)];`)
pixelScriptStringsExpr = regexp.MustCompile(`("[^",]*")`)
ErrPixelHtmlVarNotFound = errors.New("akamai-sdk-go: pixel HTML var not found")
ErrPixelScriptUrlNotFound = errors.New("akamai-sdk-go: script URL not found")
ErrPixelScriptVarNotFound = errors.New("akamai-sdk-go: script var not found")
)
// ParsePixelHtmlVar gets the required pixel challenge variable from the given HTML code src.
func ParsePixelHtmlVar(reader io.Reader) (int, error) {
src, err := io.ReadAll(reader)
if err != nil {
return 0, errors.Join(ErrPixelHtmlVarNotFound, err)
}
matches := pixelHtmlExpr.FindSubmatch(src)
if len(matches) < 2 {
return 0, ErrPixelHtmlVarNotFound
}
if v, err := strconv.Atoi(string(matches[1])); err == nil {
return v, nil
} else {
return 0, errors.Join(ErrPixelHtmlVarNotFound, err)
}
}
// TODO: manually search for the stuff and then use regex to match instead of find
// ParsePixelScriptURL gets the script URL of the pixel challenge script and the URL
// to post a generated payload to from the given HTML code src.
func ParsePixelScriptURL(reader io.Reader) (string, string, error) {
src, err := io.ReadAll(reader)
if err != nil {
return "", "", errors.Join(ErrPixelScriptUrlNotFound, err)
}
matches := pixelScriptUrlExpr.FindSubmatch(src)
if len(matches) < 2 {
return "", "", errors.Join(ErrPixelScriptUrlNotFound, err)
}
scriptUrl := string(matches[1])
// Create postUrl
parts := strings.Split(scriptUrl, "/")
parts[len(parts)-1] = "pixel_" + parts[len(parts)-1]
postUrl := strings.Join(parts, "/")
return scriptUrl, postUrl, nil
}
// ParsePixelScriptVar gets the dynamic value from the pixel script
func ParsePixelScriptVar(reader io.Reader) (string, error) {
src, err := io.ReadAll(reader)
if err != nil {
return "", errors.Join(ErrPixelScriptVarNotFound, err)
}
index := pixelScriptVarExpr.FindSubmatch(src)
if len(index) < 2 {
return "", ErrPixelScriptVarNotFound
}
stringIndex, err := strconv.Atoi(string(index[1]))
if err != nil {
return "", ErrPixelScriptVarNotFound
}
arrayDeclaration := pixelScriptStringArrayExpr.FindSubmatch(src)
if len(arrayDeclaration) < 2 {
return "", ErrPixelScriptVarNotFound
}
rawStrings := pixelScriptStringsExpr.FindAllSubmatch(arrayDeclaration[1], -1)
if stringIndex >= len(rawStrings) {
return "", ErrPixelScriptVarNotFound
}
if len(rawStrings[stringIndex]) < 2 {
return "", ErrPixelScriptVarNotFound
}
if v, err := strconv.Unquote(string(rawStrings[stringIndex][1])); err == nil {
return v, nil
} else {
return "", errors.Join(ErrPixelScriptVarNotFound, err)
}
}