summaryrefslogtreecommitdiff
path: root/README.rst
blob: a9573fccee50626fabc69cd694ce3f9a265bc9ee (plain)
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
.NET Viewstate Decoder
======================

A small Python 3.5+ library for decoding .NET viewstate. Can be used in various scraping scenarios.

.. image:: https://travis-ci.org/yuvadm/viewstate.svg?branch=master
    :target: https://travis-ci.org/yuvadm/viewstate

Install
-------

.. code-block:: shell

   $ pip install viewstate

Usage
-----

The Viewstate decoder accepts Base64 encoded .NET viewstate data and returns the decoded output in the form of plain Python objects.

There are two main ways to use this package. First, it can be used as an imported library with the following typical use case:

.. code-block:: python

  >>> from viewstate import ViewState
  >>> base64_encoded_viewstate = '/wEPBQVhYmNkZQ9nAgE='
  >>> vs = ViewState(base64_encoded_viewstate)
  >>> vs.decode()
  ('abcde', (True, 1))

It is also possible to feed the raw bytes directly:

.. code-block:: python

  >>> vs = ViewState(raw=b'\xff\x01....')

Alternatively, the library can be used via command line by directly executing the module:

.. code-block:: shell

  $ cat data.base64 | python -m viewstate

Which will pretty-print the decoded data structure.

Viewstate HMAC signatures are also supported. In case there are any remaining bytes after parsing, they are assumed to be HMAC signatures, with the types estimated according to signature length.

.. code-block:: python

   >>> vs = ViewState(signed_view_state)
   >>> vs.decode()
   >>> vs.mac
   'hmac_sha256'
   >>> vs.signature
   b'....'

Development
-----------

.. code-block:: shell

  $ pytest

References
----------

Since there is no publically available specification of how .NET viewstate is encoded, reverse engineering was based on prior work:

- https://github.com/mutantzombie/JavaScript-ViewState-Parser
- http://viewstatedecoder.azurewebsites.net/

Any official documents would be gladly accepted to help improve the parsing logic.

License
-------
MIT