Teste de penetração do Python: executando vários scripts em vários dispositivos simultaneamente
R. Eric Kiser
Pode ser uma surpresa, mas a emoção do teste de penetração pode perder seu brilho, principalmente ao realizar tarefas repetitivas durante cada engajamento. Pode começar a parecer um trabalho rotineiro de fábrica, do qual não gosto muito. A ideia de ter que executar tarefas idênticas repetidamente em todos os sistemas comprometidos torna-se tediosa e monótona. Portanto, prefiro criar ferramentas que possam lidar com essas tarefas para mim, liberando tempo para focar em atividades mais envolventes. Suponho que poderia ser considerado um viciado em conhecimento, sempre procurando expandir minha compreensão além do que sei atualmente.
Uma maneira de executar vários scripts python em vários sistemas Linux é utilizando Ansible e Python. Para realizar essa tarefa, o pacote ansible-runner pode ser utilizado, que oferece um método direto para executar playbooks Ansible programaticamente. Neste artigo, desenvolveremos um programa que solicita ao usuário as localizações de vários scripts python e sistemas Linux de destino, divide as strings de entrada em listas, define um playbook Ansible que executa os scripts python nos sistemas designados, gera uma instância de o ansible-runner e inicia o playbook.
Antes de executar este script, verifique se o Ansible e o ansible-runner estão instalados em seu sistema. Além disso, verifique se você tem acesso SSH aos sistemas Linux de destino e se eles estão configurados corretamente para serem gerenciados pelo Ansible. Para obter mais informações sobre como fazer isso, consulte meus artigos anteriores, Python Penetration Testing: Building Multi-Option Tools and Python Penetration Testing: How I control multiple Linux servers from one device .
Primeiro precisamos importar o ansible_runnerpacote. Este pacote fornece uma interface simples para executar playbooks Ansible programaticamente.
import ansible_runner
# Prompt user for the location of the python scripts and the target systems
script_location = input("Enter the location of the python scripts (comma-separated): ")
target_systems = input("Enter the target Linux systems (comma-separated): ")
# Split the input strings into lists
scripts = [s.strip() for s in script_location.split(",")]
hosts = [h.strip() for h in target_systems.split(",")]
# Define the Ansible playbook that runs the python scripts
playbook = {
'name': 'Run Python Scripts on Linux Systems',
'hosts': hosts,
'tasks': [
{
'name': f'Run script {s}',
'command': f'python {s}'
} for s in scripts
]
}
# Create an instance of the ansible-runner
runner = ansible_runner.Runner(playbook=playbook, inventory=None)
# Run the playbook
runner.run()
Eu utilizei esta ferramenta em várias ocasiões. Por exemplo, se eu encontrar uma conta de serviço com privilégios de administrador de domínio, posso usá-la para executar scripts de empacote em vários sistemas, permitindo movimento lateral em vários segmentos de rede. Bata palmas e siga se quiser e Boa Caça!
import ansible_runner
# Prompt user for the location of the python scripts and the target systems
script_location = input("Enter the location of the python scripts (comma-separated): ")
target_systems = input("Enter the target Linux systems (comma-separated): ")
# Split the input strings into lists
scripts = [s.strip() for s in script_location.split(",")]
hosts = [h.strip() for h in target_systems.split(",")]
# Define the Ansible playbook that runs the python scripts
playbook = {
'name': 'Run Python Scripts on Linux Systems',
'hosts': hosts,
'tasks': [
{
'name': f'Run script {s}',
'command': f'python {s}'
} for s in scripts
]
}
# Create an instance of the ansible-runner
runner = ansible_runner.Runner(playbook=playbook, inventory=None)
# Run the playbook
runner.run()




































![O que é uma lista vinculada, afinal? [Parte 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)