setup-simple-detect.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env bash
  2. #
  3. # Script to setup a new 'simple' detect module.
  4. # Written by Victor Julien <victor@inliniac.net>
  5. #
  6. set -e
  7. #set -x
  8. function Usage {
  9. echo
  10. echo "$(basename $0) -- script to provision a detect module. The script"
  11. echo "makes a copy of detect-template, sets the name and updates"
  12. echo "the build system."
  13. echo
  14. echo "Call from the 'src' directory, with one argument: the detect module"
  15. echo "name."
  16. echo
  17. echo "E.g. inside 'src': ../scripts/$(basename $0) helloworld"
  18. echo
  19. }
  20. function Done {
  21. echo
  22. echo "Detect module $NR has been set up in $FILE_C and $FILE_H"
  23. echo "and the build system has been updated."
  24. echo
  25. echo "The detect module should now compile cleanly. Try running 'make'."
  26. echo
  27. echo "Next steps are to edit the files to implement the actual"
  28. echo "detection logic of $NR."
  29. echo
  30. }
  31. # Make sure we are running from the correct directory.
  32. set_dir() {
  33. if [ -e ./suricata.c ]; then
  34. # Do nothing.
  35. true
  36. elif [ -e ./src/suricata.c ]; then
  37. cd src
  38. else
  39. echo "error: this does not appear to be a suricata source directory."
  40. exit 1
  41. fi
  42. }
  43. if [ $# -ne "1" ]; then
  44. Usage
  45. echo "ERROR: call with one argument"
  46. exit 1
  47. fi
  48. INPUT=$1
  49. # lowercase
  50. LC=${INPUT,,}
  51. #echo $LC
  52. # UPPERCASE
  53. UC=${LC^^}
  54. #echo $UC
  55. # Normal
  56. NR=${LC^}
  57. #echo $NR
  58. FILE_C="detect-${LC}.c"
  59. FILE_H="detect-${LC}.h"
  60. set_dir
  61. if [ ! -e detect-template.c ] || [ ! -e detect-template.h ]; then
  62. Usage
  63. echo "ERROR: input files detect-template.c and/or detect-template.h are missing"
  64. exit 1
  65. fi
  66. if [ -e $FILE_C ] || [ -e $FILE_H ]; then
  67. Usage
  68. echo "ERROR: file(s) $FILE_C and/or $FILE_H already exist, won't overwrite"
  69. exit 1
  70. fi
  71. FILE_C="tests/detect-${LC}.c"
  72. if [ ! -e tests/detect-template.c ]; then
  73. Usage
  74. echo "ERROR: input file tests/detect-template.c is missing"
  75. exit 1
  76. fi
  77. if [ -e $FILE_C ]; then
  78. Usage
  79. echo "ERROR: file $FILE_C already exist, won't overwrite"
  80. exit 1
  81. fi
  82. FILE_C="detect-${LC}.c"
  83. FILE_H="detect-${LC}.h"
  84. cp detect-template.c $FILE_C
  85. cp detect-template.h $FILE_H
  86. # search and replaces
  87. sed -i "s/TEMPLATE/${UC}/g" $FILE_C
  88. sed -i "s/TEMPLATE/${UC}/g" $FILE_H
  89. sed -i "s/Template/${NR}/g" $FILE_C
  90. sed -i "s/Template/${NR}/g" $FILE_H
  91. sed -i "s/template/${LC}/g" $FILE_C
  92. sed -i "s/template/${LC}/g" $FILE_H
  93. # add to Makefile.am
  94. sed -i "s/detect-template.c detect-template.h \\\/detect-template.c detect-template.h \\\\\n${FILE_C} ${FILE_H} \\\/g" Makefile.am
  95. # update enum
  96. sed -i "s/DETECT_TEMPLATE,/DETECT_TEMPLATE,\\n DETECT_${UC},/g" detect-engine-register.h
  97. # add include to detect-engine-register.c
  98. sed -i "s/#include \"detect-template.h\"/#include \"detect-template.h\"\\n#include \"${FILE_H}\"/g" detect-engine-register.c
  99. # add reg func to detect-engine-register.c
  100. sed -i "s/DetectTemplateRegister();/DetectTemplateRegister();\\n Detect${NR}Register();/g" detect-engine-register.c
  101. # tests file
  102. FILE_C="tests/detect-${LC}.c"
  103. cp tests/detect-template.c $FILE_C
  104. # search and replaces
  105. sed -i "s/TEMPLATE/${UC}/g" $FILE_C
  106. sed -i "s/Template/${NR}/g" $FILE_C
  107. sed -i "s/template/${LC}/g" $FILE_C
  108. Done
  109. exit 0