├── MANIFEST.in ├── requirements.txt ├── .gitattributes ├── xontrib └── history_encrypt │ ├── base64.py │ ├── fernet.py │ └── __init__.py ├── .github ├── workflows │ ├── push_test.yml │ └── python-publish.yml └── FUNDING.yml ├── LICENSE ├── setup.py ├── .gitignore └── README.md /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | xonsh 2 | cryptography 3 | ujson -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.xsh text linguist-language=Python 2 | -------------------------------------------------------------------------------- /xontrib/history_encrypt/base64.py: -------------------------------------------------------------------------------- 1 | from base64 import b64decode, b64encode 2 | 3 | base64_key = None 4 | 5 | def base64_encode(message: bytes) -> bytes: 6 | return b64encode(message) 7 | 8 | def base64_decode(token: bytes) -> bytes: 9 | return b64decode(token) -------------------------------------------------------------------------------- /.github/workflows/push_test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: push 4 | 5 | jobs: 6 | deploy: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v2 10 | - name: Set up Python 11 | uses: actions/setup-python@v2 12 | with: 13 | python-version: '3.8' 14 | - name: Install xonsh 15 | run: pip install xonsh 16 | - name: Install xontrib 17 | run: pip install . 18 | - name: Test 19 | run: xonsh -c 'xontrib load history_encrypt' 20 | -------------------------------------------------------------------------------- /xontrib/history_encrypt/fernet.py: -------------------------------------------------------------------------------- 1 | from cryptography.fernet import Fernet 2 | 3 | def fernet_key(): 4 | print('[xontrib-history-encrypt] Enter the key or press enter to create new: ', end='') 5 | key = input() 6 | if not key.strip(): 7 | key = Fernet.generate_key() 8 | print('[xontrib-history-encrypt] Save the key and use it next time:', key.decode()) 9 | return key 10 | 11 | def fernet_encrypt(message: bytes, key: bytes) -> bytes: 12 | return Fernet(key).encrypt(message) 13 | 14 | def fernet_decrypt(token: bytes, key: bytes) -> bytes: 15 | return Fernet(key).decrypt(token) 16 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | 2 | # These are supported funding model platforms 3 | 4 | #github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 5 | #patreon: xonssh # Replace with a single Patreon username 6 | #open_collective: # Replace with a single Open Collective username 7 | #ko_fi: # Replace with a single Ko-fi username 8 | #tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 9 | #community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 10 | #liberapay: # Replace with a single Liberapay username 11 | #issuehunt: # Replace with a single IssueHunt username 12 | #otechie: # Replace with a single Otechie username 13 | custom: ['https://github.com/anki-code', 'https://www.buymeacoffee.com/xxh', 'https://github.com/xonsh/xonsh#the-xonsh-shell-community'] 14 | 15 | -------------------------------------------------------------------------------- /.github/workflows/python-publish.yml: -------------------------------------------------------------------------------- 1 | # This workflow will upload a Python Package using Twine when a release is created 2 | # For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries 3 | 4 | name: Upload Python Package 5 | 6 | on: 7 | release: 8 | types: [created] 9 | 10 | jobs: 11 | deploy: 12 | 13 | runs-on: ubuntu-latest 14 | 15 | steps: 16 | - uses: actions/checkout@v2 17 | - name: Set up Python 18 | uses: actions/setup-python@v2 19 | with: 20 | python-version: '3.x' 21 | - name: Install dependencies 22 | run: | 23 | python -m pip install --upgrade pip 24 | pip install setuptools wheel twine 25 | - name: Build and publish 26 | env: 27 | TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} 28 | TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} 29 | run: | 30 | python setup.py sdist bdist_wheel 31 | twine upload dist/* 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021, anki-code 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. 22 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import setuptools 3 | 4 | try: 5 | with open('README.md', 'r', encoding='utf-8') as fh: 6 | long_description = fh.read() 7 | except (IOError, OSError): 8 | long_description = '' 9 | 10 | setuptools.setup( 11 | name='xontrib-history-encrypt', 12 | version='0.0.9', 13 | license='MIT', 14 | author='anki-code', 15 | author_email='no@no.no', 16 | description="History backend that can encrypt the xonsh shell commands history.", 17 | long_description=long_description, 18 | long_description_content_type='text/markdown', 19 | python_requires='>=3.6', 20 | install_requires=['xonsh', 'cryptography', 'ujson'], 21 | packages=['xontrib', 'xontrib.history_encrypt'], 22 | package_dir={'xontrib': 'xontrib'}, 23 | package_data={'xontrib': ['*.py']}, 24 | platforms='any', 25 | url='https://github.com/anki-code/xontrib-history-encrypt', 26 | project_urls={ 27 | "Documentation": "https://github.com/anki-code/xontrib-history-encrypt/blob/master/README.md", 28 | "Code": "https://github.com/anki-code/xontrib-history-encrypt", 29 | "Issue tracker": "https://github.com/anki-code/xontrib-history-encrypt/issues", 30 | }, 31 | classifiers=[ 32 | "Programming Language :: Python :: 3", 33 | "Programming Language :: Python :: 3.6", 34 | "Programming Language :: Python :: 3.7", 35 | "Programming Language :: Python :: 3.8", 36 | "Programming Language :: Python :: 3.9", 37 | "License :: OSI Approved :: MIT License", 38 | "Operating System :: OS Independent", 39 | "Topic :: System :: Shells", 40 | "Topic :: System :: System Shells", 41 | "Topic :: Terminals", 42 | ] 43 | ) 44 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | The xonsh shell history backend that encrypt the commands history file
to prevent leaking sensitive data.
3 |
4 |
5 |
8 | If you like the idea click ⭐ on the repo and tweet. 9 |
10 | 11 | 12 | ## Installation 13 | 14 | To install use pip: 15 | 16 | ```bash 17 | xpip install xontrib-history-encrypt 18 | # or: xpip install -U git+https://github.com/anki-code/xontrib-history-encrypt 19 | ``` 20 | 21 | ## Usage: supported encryption 22 | 23 | ### Base64 (default) 24 | 25 | *Protection level: no protection.* 26 | 27 | Base64 is not the real encrypter and implemented as fast way to encode history file and for education reasons. 28 | It can save you from the massive scanning the file system for keywords (i.e. password, key) 29 | as well as reading the history file by not experienced user. But it can be decoded in five minutes by the professional. 30 | 31 | ```python 32 | # Add to xonsh RC file 33 | $XONSH_HISTORY_ENCRYPTOR = 'base64' 34 | xontrib load history_encrypt 35 | ``` 36 | 37 | ### Fernet 38 | 39 | *Protection level: high.* 40 | 41 | The implementation of [Fernet](https://cryptography.io/en/latest/fernet.html) (AES CBC + HMAC) that was strongly 42 | recommended on [stackoverflow](https://stackoverflow.com/a/55147077). On first start it generates a key that you 43 | should save in secure place. Than you can use this key to decrypt the history. 44 | 45 | ```python 46 | # Add to xonsh RC file 47 | $XONSH_HISTORY_ENCRYPTOR = 'fernet' 48 | xontrib load history_encrypt 49 | ``` 50 | 51 | ### Dummy 52 | 53 | *Protection level: super high.* 54 | 55 | The best encryption of the data when there is no the data. The dummy encryptor stores command only in the memory during 56 | the session without saving it on the disk. After the end of the session the commands will be lost. 57 | 58 | ```python 59 | # Add to xonsh RC file 60 | $XONSH_HISTORY_ENCRYPTOR = 'dummy' 61 | xontrib load history_encrypt 62 | ``` 63 | 64 | ### Custom 65 | 66 | *Protection level: all in your hands.* 67 | 68 | To create custom encryptor you should implement three functions: key getter function, encryptor and decryptor. 69 | 70 | ```python 71 | # Add to xonsh RC file 72 | $XONSH_HISTORY_ENCRYPTOR = { 73 | 'key': lambda: input('[xontrib-history-encrypt] Enter any key just for fun: '), 74 | 'enc': lambda data, key=None: data[::-1], # just flip the string 75 | 'dec': lambda data, key=None: data[::-1] # flip the string back 76 | } 77 | xontrib load history_encrypt 78 | ``` 79 | 80 | After debugging you can add your encryptor to the `history_encrypt` directory of the xontrib by PR. 81 | 82 | ## Common use case 83 | 84 | 1. You're on the public/shared/opened server where you have xonsh and bash. 85 | 2. Install the xontrib and create your RC-file from bash: 86 | ```python 87 | pip install xontrib-history-encrypt 88 | mkdir -p ~/.local/share/xonsh/ 89 | echo -e '$XONSH_HISTORY_ENCRYPTOR = "fernet"\nxontrib load history_encrypt' > ~/.local/share/xonsh/rc 90 | ``` 91 | 3. Run xonsh with RC-file then get the key and remember the key: 92 | ```python 93 | xonsh --rc ~/.local/share/xonsh/rc 94 | # Enter the key or press enter to create new: