-
Notifications
You must be signed in to change notification settings - Fork 2
/
chepy_extract.py
120 lines (95 loc) · 3.55 KB
/
chepy_extract.py
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
import logging
import json
from lxml import etree
import lazy_import
try:
import regex
jsonpath_rw = lazy_import.lazy_module("jsonpath_rw")
except ImportError:
logging.warning("Could not import regex. Use pip install regex")
import chepy.core
class Chepy_Extract(chepy.core.ChepyCore):
"""Chepy plugin that handles extended secrets and extract operations"""
@chepy.core.ChepyDecorators.call_stack
def extract_common_secrets(self, case_insensitive: bool = True):
"""Checks for secrets
Checks ~1000 different secrets patterns. Returns a dict of partial
pattern name as the key, and and array of found matches as the value.
This mostly checks for common variable names that contains secrets.
Args:
case_insensitive (bool, optional): Case insensitive search. Defaults to True
Returns:
ChepyPlugin: The Chepy object.
"""
# Increase load speed
import pkg_resources
found = {}
secrets_path = pkg_resources.resource_filename(__name__, "data/secrets.txt")
with open(secrets_path, "r") as f:
for pattern in f:
matches = regex.findall(pattern.strip(), self._convert_to_str())
if matches:
found[regex.sub(r"[^a-zA-Z0-9_]", "", pattern[0:20])] = matches
self.state = found
return self
@chepy.core.ChepyDecorators.call_stack
def jpath_selector(self, query: str):
"""Query JSON with jpath query
`Reference <https://goessner.net/articles/JsonPath/index.html#e2>`__
Args:
query (str): Required. Query. For reference, see the help
Returns:
Chepy: The Chepy object.
Examples:
>>> c = Chepy("tests/files/test.json")
>>> c.load_file()
>>> c.jpath_selector("[*].name.first")
>>> c.get_by_index(2)
>>> c.o
"Long"
"""
self.state = list(
j.value
for j in jsonpath_rw.parse(query).find(json.loads(self._convert_to_str()))
)
return self
# @chepy.core.ChepyDecorators.call_stack
# def php_deserialize(self):
# """Deserialize php to dict
# Deserializes PHP serialized data, outputting keyed arrays as a python dict.
# Returns:
# Chepy: The Chepy object.
# Examples:
# >>> c = Chepy('a:3:{i:1;s:6:"elem 1";i:2;s:6:"elem 2";i:3;s:7:" elem 3";}')
# >>> c.php_deserialize()
# {1: b'elem 1', 2: b'elem 2', 3: b' elem 3'}
# """
# self.state = phpserialize.loads(self._convert_to_bytes())
# return self
@chepy.core.ChepyDecorators.call_stack
def minify_xml(self):
"""Minify XML string
Returns:
Chepy: The Chepy object.
Examples:
>>> c = Chepy("/path/to/file.xml").load_file()
>>> print(c.minify_xml())
"""
parser = etree.XMLParser(remove_blank_text=True)
self.state = etree.tostring(
etree.fromstring(self._convert_to_bytes(), parser=parser)
)
return self
@chepy.core.ChepyDecorators.call_stack
def beautify_xml(self):
"""Beautify compressed XML
Returns:
Chepy: The Chepy object.
Examples:
>>> c = Chepy("/path/to/file.xml").load_file()
>>> print(c.beautify_json())
"""
self.state = etree.tostring(
etree.fromstring(self._convert_to_bytes()), pretty_print=True
)
return self