Skip to content

Commit

Permalink
Also attempt to php/unserialize the cookie for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
wdoekes committed Nov 10, 2022
1 parent 72bbbf0 commit d770d46
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ LABEL description="Scrape alertmobile.alert-group.nl and publish to Slack"
LABEL dockerfile-vcs="https://github.com/ossobv/alert-group-nl-log2slack"

ENV PYTHONUNBUFFERED=1
RUN pip install BeautifulSoup4 requests
RUN pip install BeautifulSoup4 requests phpserialize
COPY alert_group_nl_log2slack.py /srv/
CMD python3 -V && pip freeze && echo '.' && \
echo 'Starting publish-forever...' && \
Expand Down
45 changes: 33 additions & 12 deletions alert_group_nl_log2slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from traceback import print_exc

from bs4 import BeautifulSoup
import phpserialize
import requests


Expand Down Expand Up @@ -50,25 +51,45 @@ def send_slack_message(message):
assert ret.status_code == 200, (ret, ret.text)


def from_utf8(data):
if isinstance(data, bytes):
return data.decode('utf-8')
if isinstance(data, list):
return [from_utf8(i) for i in data]
if isinstance(data, dict):
return dict((from_utf8(k), from_utf8(v)) for k, v in data.items())
return data


def decode_cookie(val):
decoding = []

try:
base64_decoded = b64decode(val)
type_ = 'base64'
val = b64decode(val)
decoding.append('b64')
except ValueError:
try:
base64_decoded = b64decode(val.replace('%3D', '='))
type_ = 'base64pct'
val = b64decode(val.replace('%3D', '='))
decoding.append('b64pct')
except ValueError:
return 'raw', value
pass

try:
base64_decoded = base64_decoded.decode('utf-8')
except UnicodeDecodeError:
pass
else:
type_ = f'{type_};utf-8'

return type_, base64_decoded
val = json.dumps(
from_utf8(phpserialize.loads(
val, object_hook=(lambda k, v: {k: dict(v)}))),
separators=(', ', ':')) # semi-compact
decoding.append('phpserialize')
except Exception:
try:
val = val.decode('utf-8')
decoding.append('utf8')
except UnicodeDecodeError:
pass

if not decoding:
decoding = ['raw']
return ';'.join(decoding), val


def dump_cookies(session, where):
Expand Down

0 comments on commit d770d46

Please sign in to comment.