---
- name: Auditoría forense con CSV consolidado y legible
hosts: all
gather_facts: false
vars:
fecha_inicio: "2025-08-01 21:00:00"
fecha_fin: "2025-08-02 02:30:00"
tasks:
- name: Obtener hostname
command: hostname
register: hostname
- name: Obtener historial de root
shell: |
cat /root/.bash_history 2>/dev/null | grep -Ei 'rm|unlink|mv|truncate'
register: root_history
ignore_errors: true
- name: Obtener historial de ansible
shell: |
cat /home/ansible/.bash_history 2>/dev/null | grep -Ei 'rm|unlink|mv|truncate'
register: ansible_history
ignore_errors: true
- name: Buscar uso de sudo por ansible en ventana temporal
shell: |
awk '
BEGIN {
start = mktime("2025 08 01 21 00 00")
end = mktime("2025 08 02 02 30 00")
}
{
cmd = "date -d \""$1" "$2" "$3" "$4"\" +%s"
cmd | getline t
close(cmd)
if (t >= start && t <= end) print $0
}
' /var/log/secure | grep 'sudo'
register: sudo_usage
ignore_errors: true
- name: Verificar si auditd está instalado
shell: rpm -q audit
register: auditd_installed
ignore_errors: true
- name: Buscar comandos sospechosos con auditd
when: auditd_installed.rc == 0
shell: |
ausearch -x rm -x unlink -x rmdir -ts "{{ fecha_inicio }}" -te "{{ fecha_fin }}"
register: audit_logs
ignore_errors: true
- name: Preparar línea CSV por host (con campos entre comillas dobles)
set_fact:
audit_line: |
"{{ hostname.stdout }}","{{ root_history.stdout | default('') | replace('\n', ' | ') }}","{{ ansible_history.stdout | default('') | replace('\n', ' | ') }}","{{ sudo_usage.stdout | default('') | replace('\n', ' | ') }}","{{ audit_logs.stdout | default('') | replace('\n', ' | ') }}"
- name: Agregar cabecera si no existe (solo una vez)
local_action:
module: lineinfile
path: ./auditoria_resultados.csv
line: '"Hostname","Root History","Ansible History","Sudo Usage","Auditd Events"'
create: yes
insertafter: BOF
delegate_to: localhost
run_once: true
- name: Agregar línea por host al CSV
local_action:
module: lineinfile
path: ./auditoria_resultados.csv
line: "{{ audit_line }}"
create: yes
insertafter: EOF
delegate_to: localhost
No hay comentarios:
Publicar un comentario
Comentarios