Closed ghost closed 3 years ago
here's a simple script that should hopefully help:
import sys
import hyperscan
def parse_patterns(filename):
patterns = []
with open(filename) as f:
for line in f:
if line.startswith('#'):
continue
columns = [chunk.strip() for chunk in line.split('\t')]
if len(columns) != 3:
continue
columns[0] = int(columns[0])
columns[1] = columns[1].encode()
patterns.append(columns)
return patterns
def match_handler(pattern_id, start, end, flags, context):
pattern_name = context['names'][pattern_id]
substr = context['input'][start:end]
print(f'[match] {pattern_name}: `{substr}`')
def main(args):
if len(args) != 2:
print(f'usage: {sys.argv[0]} [patterns file] [input]')
sys.exit(1)
pattern_filename, input_str = args
patterns = parse_patterns(pattern_filename)
db = hyperscan.Database()
pattern_ids, expressions, names = zip(*patterns)
db.compile(expressions=expressions, ids=pattern_ids)
context = {
'input': input_str,
'names': {
pattern_id: names[i]
for i, pattern_id in enumerate(pattern_ids)
},
}
db.scan(
input_str,
match_event_handler=match_handler,
context=context,
)
if __name__ == '__main__':
main(sys.argv[1:])
python-hyperscan on ī master [$?] is š¦ v0.1.5 via š v3.8.2rc2+ (hyperscan-nTr-HRfE-py3.8)
āÆ python hsmatch.py patterns.txt git://foo.git
[match] GitRepoPattern: `git://foo.git`
python-hyperscan on ī master [$?] is š¦ v0.1.5 via š v3.8.2rc2+ (hyperscan-nTr-HRfE-py3.8)
āÆ python hsmatch.py patterns.txt foo.onion
[match] OnionDomain: `foo.onion`
The unit tests should be another resource you can use, as they cover most of the Python interface.
Awesome, I ll try it now, keep u updated.
Thanks again
I tried with the following input
python main.py patterns.txt "foo.onion git://foo.git"
and it returned me
[match] OnionDomain: `foo.onion`
[match] GitRepoPattern: `foo.onion git://foo.git`
Is there an easy way to substr only the matched pattern ?
see Start of Match
replace the db.compile
lines with this:
db.compile(
expressions=expressions,
ids=pattern_ids,
flags=hyperscan.HS_FLAG_SOM_LEFTMOST,
)
It works better.
python main.py patterns.txt "foo.onion git://foo.git"
[match] OnionDomain: `foo.onion`
[match] GitRepoPattern: `git://foo.git`
But I tried the following:
python main.py patterns.txt "x0rzkov@protonmail.com 1HB5XMLmzFVj8ALj6mfBsbifRoD4miY36v https://twitter.com/x0rxkov random test sentence https://twitter.com/twitter"
and got this output:
[match] BtcAddressPattern: `1HB5XMLmzFVj8ALj6mfBsbifRo`
[match] BtcAddressPattern: `1HB5XMLmzFVj8ALj6mfBsbifRoD`
[match] BtcAddressPattern: `1HB5XMLmzFVj8ALj6mfBsbifRoD4`
[match] BtcAddressPattern: `1HB5XMLmzFVj8ALj6mfBsbifRoD4m`
[match] BtcAddressPattern: `1HB5XMLmzFVj8ALj6mfBsbifRoD4mi`
[match] BtcAddressPattern: `1HB5XMLmzFVj8ALj6mfBsbifRoD4miY`
[match] BtcAddressPattern: `1HB5XMLmzFVj8ALj6mfBsbifRoD4miY3`
[match] BtcAddressPattern: `1HB5XMLmzFVj8ALj6mfBsbifRoD4miY36`
[match] BtcAddressPattern: `1HB5XMLmzFVj8ALj6mfBsbifRoD4miY36v`
Is my regex for bitcoin not properly written ?
Cheers, X
try adding a \b
at the end of the btc pattern
Hi,
Hope you are all well !
Can you provide more example for matching multiple patterns from a simple text ?
More other, I am gopher rather a pythonista but I am trying to learn python.
For now, in golang, I wrote the following script to create a database or patterns and to scan a text input.
patterns.txt
main.go
Is it possible to create a small example, not restful if too complicated, for loading a text file of patterns and process a simple text input ?
Thanks in advance for any insights or inputs on that.
Cheers, X