setup-simple-detect2.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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-template2.c ] || [ ! -e detect-template2.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="detect-${LC}.c"
  72. FILE_H="detect-${LC}.h"
  73. cp detect-template2.c $FILE_C
  74. cp detect-template2.h $FILE_H
  75. # search and replaces
  76. sed -i "s/TEMPLATE2/${UC}/g" $FILE_C
  77. sed -i "s/TEMPLATE2/${UC}/g" $FILE_H
  78. sed -i "s/Template2/${NR}/g" $FILE_C
  79. sed -i "s/Template2/${NR}/g" $FILE_H
  80. sed -i "s/template2/${LC}/g" $FILE_C
  81. sed -i "s/template2/${LC}/g" $FILE_H
  82. # add to Makefile.am
  83. sed -i "s/detect-template2.c detect-template2.h \\\/detect-template2.c detect-template2.h \\\\\n${FILE_C} ${FILE_H} \\\/g" Makefile.am
  84. # update enum
  85. sed -i "s/DETECT_TEMPLATE2,/DETECT_TEMPLATE2,\\n DETECT_${UC},/g" detect-engine-register.h
  86. # add include to detect-engine-register.c
  87. sed -i "s/#include \"detect-template2.h\"/#include \"detect-template2.h\"\\n#include \"${FILE_H}\"/g" detect-engine-register.c
  88. # add reg func to detect-engine-register.c
  89. sed -i "s/DetectTemplate2Register();/DetectTemplate2Register();\\n Detect${NR}Register();/g" detect-engine-register.c
  90. Done
  91. exit 0