Skip to content

Commit 5e22f51

Browse files
committed
Initial Commit
0 parents  commit 5e22f51

File tree

14 files changed

+728
-0
lines changed

14 files changed

+728
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Hyper Solutions
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Hyper Solutions SDK for Python
2+
3+
## Installation
4+
5+
To use the Hyper Solutions SDK in your Python project, you need to install it using the following command:
6+
7+
```
8+
pip install hyper-sdk
9+
```
10+
11+
## Usage
12+
13+
### Creating a Session
14+
15+
To start using the SDK, you need to create a new `Session` instance by providing your API key:
16+
17+
```python
18+
from hyper_sdk import Session
19+
20+
session = Session(api_key="your-api-key")
21+
```
22+
23+
You can also optionally set a JWT key and a custom HTTP client:
24+
25+
```python
26+
from hyper_sdk import Session
27+
28+
session = Session(api_key="your-api-key", jwt_key="your-jwt-key", client=custom_http_client)
29+
```
30+
31+
## Akamai
32+
33+
The Akamai package provides functions for interacting with Akamai Bot Manager, including generating sensor data, parsing script path, parsing pixel challenges, and handling sec-cpt challenges.
34+
35+
### Generating Sensor Data
36+
37+
To generate sensor data required for generating valid Akamai cookies, use the `generate_sensor_data` method:
38+
39+
```python
40+
from hyper_sdk import SensorInput
41+
42+
sensor_input = SensorInput(
43+
page_url="https://example.com/",
44+
user_agent="your-user-agent",
45+
abck="your-abck-cookie",
46+
bmsz="your-bmsz-cookie",
47+
version="2.0"
48+
)
49+
sensor_data = session.generate_sensor_data(sensor_input)
50+
```
51+
52+
### Parsing Script Path
53+
54+
To parse the Akamai Bot Manager script path from the given HTML code, use the `parse_script_path` function from the `script_path` module:
55+
56+
```python
57+
from hyper_sdk.akamai.script_path import parse_script_path
58+
59+
script_path = parse_script_path(html_source)
60+
```
61+
62+
### Handling Sec-Cpt Challenges
63+
64+
The Akamai package provides functions for handling sec-cpt challenges:
65+
66+
- `SecCptChallenge.parse`: Parses a sec-cpt challenge from an HTML source.
67+
- `generate_sec_cpt_payload`: Generates a sec-cpt payload using the provided sec-cpt cookie.
68+
- `sleep`: Sleeps for the duration specified in the sec-cpt challenge.
69+
70+
Example usage:
71+
72+
```python
73+
from hyper_sdk.akamai.sec_cpt import SecCptChallenge
74+
75+
challenge = SecCptChallenge.parse(html_source)
76+
payload = challenge.generate_sec_cpt_payload(sec_cpt_cookie)
77+
challenge.sleep()
78+
```
79+
80+
### Validating Cookies
81+
82+
The Akamai package provides functions for validating cookies:
83+
84+
- `is_cookie_valid`: Determines if the provided `_abck` cookie value is valid based on the given request count.
85+
- `is_cookie_invalidated`: Determines if the current session requires more sensors to be sent.
86+
87+
Example usage:
88+
89+
```python
90+
from hyper_sdk.akamai.stop_signal import is_cookie_valid, is_cookie_invalidated
91+
92+
is_valid = is_cookie_valid(abck_cookie, request_count)
93+
is_invalidated = is_cookie_invalidated(abck_cookie)
94+
```
95+
96+
### Generating Pixel Data
97+
98+
To generate pixel data, use the `generate_pixel_data` method:
99+
100+
```python
101+
from hyper_sdk import PixelInput
102+
103+
pixel_input = PixelInput(
104+
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36",
105+
html_var="your-html-var",
106+
script_var="your-script-var"
107+
)
108+
pixel_data = session.generate_pixel_data(pixel_input)
109+
```
110+
111+
### Parsing Pixel Challenges
112+
113+
The Akamai package provides functions for parsing pixel challenges:
114+
115+
- `parse_pixel_html_var`: Parses the required pixel challenge variable from the given HTML code.
116+
- `parse_pixel_script_url`: Parses the script URL of the pixel challenge script and the URL to post a generated payload to from the given HTML code.
117+
- `parse_pixel_script_var`: Parses the dynamic value from the pixel script.
118+
119+
Example usage:
120+
121+
```python
122+
from hyper_sdk.akamai.pixel import parse_pixel_html_var, parse_pixel_script_url, parse_pixel_script_var
123+
124+
html_var = parse_pixel_html_var(html_source)
125+
script_url, post_url = parse_pixel_script_url(html_source)
126+
script_var = parse_pixel_script_var(script_source)
127+
```
128+
129+
130+
## Incapsula
131+
132+
The Incapsula package provides functions for interacting with Imperva Incapsula, including generating Reese84 sensor data, UTMVC cookies, and parsing UTMVC script paths.
133+
134+
### Generating Reese84 Sensor
135+
136+
To generate sensor data required for generating valid Reese84 cookies, use the `generate_reese84_sensor` method:
137+
138+
```python
139+
sensor_data = session.generate_reese84_sensor(site="example.com", user_agent="your-user-agent")
140+
```
141+
142+
### Generating UTMVC Cookie
143+
144+
To generate the UTMVC cookie using the Hyper Solutions API, use the `generate_utmvc_cookie` method:
145+
146+
```python
147+
from hyper_sdk import UtmvcInput
148+
149+
utmvc_input = UtmvcInput(
150+
user_agent="your-user-agent",
151+
session_ids=["session-id-1", "session-id-2"],
152+
script="your-script"
153+
)
154+
utmvc_cookie = session.generate_utmvc_cookie(utmvc_input)
155+
```
156+
157+
### Parsing UTMVC Script Path
158+
159+
To parse the UTMVC script path from a given script content, use the `parse_utmvc_script_path` function from the `utmvc` module:
160+
161+
```python
162+
from hyper_sdk.incapsula.utmvc import parse_utmvc_script_path
163+
164+
script_path = parse_utmvc_script_path(script_content)
165+
```
166+
167+
### Generating UTMVC Submit Path
168+
169+
To generate a unique UTMVC submit path with a random query parameter, use the `get_utmvc_submit_path` function from the `utmvc` module:
170+
171+
```python
172+
from hyper_sdk.incapsula.utmvc import get_utmvc_submit_path
173+
174+
submit_path = get_utmvc_submit_path()
175+
```

hyper_sdk/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from .akamai.pixel import *
2+
from .akamai.script_path import *
3+
from .akamai.sec_cpt import *
4+
from .akamai.stop_signal import *
5+
from .incapsula.utmvc import *
6+
from .session import *

hyper_sdk/akamai/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .pixel import *
2+
from .script_path import *
3+
from .sec_cpt import *
4+
from .stop_signal import *

hyper_sdk/akamai/pixel.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import re
2+
3+
pixel_html_expr = re.compile(r'bazadebezolkohpepadr="(\d+)"')
4+
pixel_script_url_expr = re.compile(r'src="(https?://.+/akam/\d+/\w+)"')
5+
pixel_script_var_expr = re.compile(r'g=_\[(\d+)]')
6+
pixel_script_string_array_expr = re.compile(r'var _=\[(.+?)];')
7+
pixel_script_strings_expr = re.compile(r'("[^",]*")')
8+
9+
10+
def parse_pixel_html_var(src: str) -> int:
11+
"""
12+
ParsePixelHtmlVar gets the required pixel challenge variable from the given HTML code src.
13+
14+
Args:
15+
src (str): HTML source code as a string.
16+
17+
Returns:
18+
int: The parsed pixel HTML variable.
19+
20+
Raises:
21+
Exception: If the pixel HTML var is not found in the source.
22+
"""
23+
match = pixel_html_expr.search(src)
24+
if match:
25+
return int(match.group(1))
26+
else:
27+
raise Exception("hyper-sdk: pixel HTML var not found")
28+
29+
30+
def parse_pixel_script_url(src: str) -> tuple[str, str]:
31+
"""
32+
ParsePixelScriptURL gets the script URL of the pixel challenge script and the URL
33+
to post a generated payload to from the given HTML code src.
34+
35+
Args:
36+
src (str): HTML source code as a string.
37+
38+
Returns:
39+
tuple[str, str]: A tuple containing the script URL and the post URL.
40+
41+
Raises:
42+
Exception: If the script URL is not found in the source.
43+
"""
44+
match = pixel_script_url_expr.search(src)
45+
if match:
46+
script_url = match.group(1)
47+
# Create post_url
48+
parts = script_url.split("/")
49+
parts[-1] = "pixel_" + parts[-1]
50+
post_url = "/".join(parts)
51+
return script_url, post_url
52+
else:
53+
raise Exception("hyper-sdk: script URL not found")
54+
55+
56+
def parse_pixel_script_var(src: str) -> str:
57+
"""
58+
Gets the dynamic value from the pixel script.
59+
60+
Args:
61+
src (str): HTML source code as a string.
62+
63+
Returns:
64+
str: The dynamic value extracted from the pixel script.
65+
66+
Raises:
67+
Exception: If the script variable is not found or if there are issues extracting it.
68+
"""
69+
index_match = pixel_script_var_expr.search(src)
70+
if not index_match:
71+
raise Exception("hyper-sdk: script var not found")
72+
string_index = int(index_match.group(1))
73+
74+
array_declaration_match = pixel_script_string_array_expr.search(src)
75+
if not array_declaration_match:
76+
raise Exception("hyper-sdk: script var not found")
77+
78+
raw_strings = pixel_script_strings_expr.findall(array_declaration_match.group(1))
79+
if string_index >= len(raw_strings):
80+
raise Exception("hyper-sdk: script var not found")
81+
82+
string_value = raw_strings[string_index].strip('"')
83+
return string_value

hyper_sdk/akamai/script_path.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import re
2+
3+
# Precompiled regular expressions
4+
script_path_expr = re.compile(r'<script type="text/javascript"\s+(?:nonce=".*?")?\s+src="([a-z\d/\-_]+)"></script>',
5+
re.IGNORECASE)
6+
7+
8+
def parse_script_path(src: str) -> str:
9+
"""
10+
Gets the Akamai Bot Manager web SDK path from the given HTML code src.
11+
12+
This function searches the provided HTML source code for the path of a JavaScript script tag that matches the
13+
specified regular expression pattern.
14+
15+
Args:
16+
src (str): The HTML source code as a string.
17+
18+
Returns:
19+
str: The path of the script extracted from the script tag.
20+
21+
Raises:
22+
Exception: If the script path is not found in the source.
23+
"""
24+
match = script_path_expr.search(src)
25+
if match:
26+
return match.group(1)
27+
else:
28+
raise "hyper-sdk: script path not found"

0 commit comments

Comments
 (0)