Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test credential access functionality and package structure refactor #856

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sample_packages/sample_python_package/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
## Sample Python package

This package will simulate different scenarios to test package analysis on.
This package will simulate different scenarios to test package analysis on. While this package will attempt to revert any modifications it makes, it is not recommended to install, import, or use this package in any way outside of a sandboxed setting.

To use this package for local analysis, build this package by running
`python3 -m build` in this directory. The package will be located in the dist/
folder.

Developers can modify the parameters to the run_selected_functions calls. Note, however, that at this time package analysis does not output logs from all phases.

The same license for the rest of the package analysis project applies to this package.
2 changes: 1 addition & 1 deletion sample_packages/sample_python_package/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
setup(name="sample_python_package",
packages=find_packages(),)

send_https_post_request("setup.py")
run_selected_functions([https_functions, access_credentials_functions], "setup.py")
2 changes: 1 addition & 1 deletion sample_packages/sample_python_package/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

from example import *

send_https_post_request("__init__.py")
run_selected_functions([https_functions, access_credentials_functions], "__init__.py")
67 changes: 62 additions & 5 deletions sample_packages/sample_python_package/src/example.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,76 @@
import http.client
import json
import os

# Pass these strings into run_selected_functions to select which behaviors to simulate.
https_functions = "https_functions"
access_credentials_functions = "access_credentials_functions"

# Sends an HTTPS post request and prints out the response.
def send_https_post_request(location: str) -> None:
def send_https_post_request(called_from: str, print_logs: bool) -> None:
host = "www.httpbin.org"
conn = http.client.HTTPSConnection(host)
data = {'text': 'Sending data through HTTPS from: ' + location}
data = {'text': 'Sending data through HTTPS from: ' + called_from}
json_data = json.dumps(data)
conn.request("POST", "/post", json_data, headers={"Host": host})
response = conn.getresponse()
print(response.read().decode())
if(print_logs):
elainechien marked this conversation as resolved.
Show resolved Hide resolved
print(response.read().decode())

def main():
send_https_post_request("main function")

# Access ssh keys and attempts to read and write to them.
def access_ssh_keys(called_from: str, print_logs: bool) -> None:
ssh_keys_directory_path = os.path.join(os.path.expanduser('~'), ".ssh")
if os.path.isdir(ssh_keys_directory_path):
try:
files_in_ssh_keys_directory = os.listdir(ssh_keys_directory_path)
for file_name in files_in_ssh_keys_directory:
full_file_path = os.path.join(ssh_keys_directory_path, file_name)
original_file_lines = []
with open(full_file_path, "a+") as f:
elainechien marked this conversation as resolved.
Show resolved Hide resolved
f.seek(0)
original_file_lines.extend(f.readlines())
f.write("\nWriting to files in ~/.ssh from: " + called_from)
# Reset the original state of the files.
with open(full_file_path, "w") as f:
for line in original_file_lines:
f.write(line)
if(print_logs):
print("Files in ssh keys directory", files_in_ssh_keys_directory)
except Exception as e:
# Fail gracefully to allow execution to continue.
if(print_logs):
print(f"An exception occurred when calling access_ssh_keys: {str(e)}")

def read_file_and_log(file_to_read: str, called_from: str, print_logs: bool) -> None:
if os.path.isfile(file_to_read):
try:
with open(file_to_read, "r") as f:
f.seek(0)
elainechien marked this conversation as resolved.
Show resolved Hide resolved
file_lines = f.readlines()
if(print_logs):
print("Read " + file_to_read + " from: " + called_from + ". Lines: " + str(len(file_lines)))
except Exception as e:
# Fail gracefully to allow execution to continue.
if(print_logs):
print(f"An exception occurred when calling read_file_and_log: {str(e)}")

def access_passwords(called_from: str, print_logs: bool) -> None:
password_file = os.path.join(os.path.abspath(os.sep), "etc", "passwd")
shadow_password_file = os.path.join(os.path.abspath(os.sep), "etc", "shadow")
read_file_and_log(password_file, called_from, print_logs)
# Requires root to read.
read_file_and_log(shadow_password_file, called_from, print_logs)

def run_selected_functions(functions: list[str], called_from: str, print_logs: bool = True) -> None:
elainechien marked this conversation as resolved.
Show resolved Hide resolved
if https_functions in functions:
send_https_post_request(called_from, print_logs)
if access_credentials_functions in functions:
access_ssh_keys(called_from, print_logs)
access_passwords(called_from, print_logs)

def main():
run_selected_functions([https_functions, access_credentials_functions], "main function")

if __name__ == "__main__":
main()