Skip to main content

A (relatively) secure and easy-to-use subprocess implementation for Python

Project description

BearShell

A (relatively) secure and easy-to-use subprocess implementation for Python

BearShell

BearShell is a Python class for executing shell commands securely with output streaming, injection risk protection, and support for command presets. This tool provides an easy-to-use interface while enforcing security policies and offering control over the commands executed.

Features

  • Secure Command Execution: Protects against common shell injection tactics.
  • Command Presets: Allows predefined command templates with substitution.
  • Output Streaming: Streams output line-by-line while capturing the entire result.
  • Allow-List and Block-List: Policies to restrict which commands can or cannot be executed.
  • Injection Risk Detection: Detects risky shell patterns like ;, &&, rm, and others.

Installation

  1. Clone the repository or install the class directly into your project.
  2. No dependencies outside the standard Python library.

Example Usage

from bearshell import BearShell

# Create BearShell object with a buffer limit of 5000 lines
shell = BearShell(max_buffer_lines=5000)

# Set allow-list of safe commands
shell.set_allow_list(["searchsploit", "nmap"])

# Set block-list of risky commands
shell.set_block_list(["rm", "shutdown"])

# Define a preset command for running searchsploit
shell.add_preset("search_exploit", ["searchsploit", "{query}"])

# Run the search_exploit preset with a query
response = shell.run_preset("search_exploit", query="apache")

# Output the response as a dictionary
print(response.to_dict())

# Run a custom command directly (this will be parsed and validated)
response = shell.run("nmap -p 80 192.168.1.1")

# Output the response
print(response.to_dict())

# Run a custom command directly (this will be parsed and validated)
response = shell.run("nmap -p 80 192.168.1.1")

# Output the response
print(response.to_dict())

Command Response

The BearResponse returned by the run and run_preset methods contains:

start_time Time when the command started.

end_time Time when the command finished.

stdout Standard output (captured during the command execution).

stderr Standard error output.

error_message An error message in case of a failure (e.g., injection risk or policy violation).

You can get the response in various formats:

Dictionary: response.to_dict()

JSON: response.to_json()

Policy Enforcement

Allow-List: Only allows commands listed in the allow-list to run.

Block-List: Blocks commands that are added to the block-list.

Injection Risk Detection: Automatically checks for dangerous patterns (e.g., rm, shutdown) and blocks commands with such patterns.

Example Response Object

{ "start_time": "2025-04-10T12:30:00", "end_time": "2025-04-10T12:30:05", "stdout": "Found exploit for Apache\n", "stderr": "", "error_message": null }

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bearshell-0.1.2.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bearshell-0.1.2-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file bearshell-0.1.2.tar.gz.

File metadata

  • Download URL: bearshell-0.1.2.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for bearshell-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a635e2e10dc2a22b9ae58029ba3e57cbcee41344299f48c421d4b3a9723d33cb
MD5 dcc8100e8561e0e4a555b3687678be9f
BLAKE2b-256 fbafdeff73921e870e956239e888b35f3cd1cceb9cc142b3dfae5d3c737ec46c

See more details on using hashes here.

Provenance

The following attestation bundles were made for bearshell-0.1.2.tar.gz:

Publisher: python-publish.yml on breimers/bear-shell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bearshell-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: bearshell-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for bearshell-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 45a469bd4028e77a599a6b289a11b6a0ac916bcf3ee7eee62e3123bc66e13c7a
MD5 f63a1c7b17855d1e39bf72db8e70fc08
BLAKE2b-256 afe0fb718a4b16a7fdf8c13d0a61421be9e7df46a706503fa204ee55800cc214

See more details on using hashes here.

Provenance

The following attestation bundles were made for bearshell-0.1.2-py3-none-any.whl:

Publisher: python-publish.yml on breimers/bear-shell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page