build.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/bin/bash
  2. FULLBUILD=false
  3. NONINTERACTIVE=false
  4. while test $# -gt 0
  5. do
  6. case "$1" in
  7. --non-interactive)
  8. NONINTERACTIVE=true
  9. ;;
  10. --full)
  11. FULLBUILD=true
  12. ;;
  13. esac
  14. shift
  15. done
  16. # Install required packages
  17. if [ ! ${NONINTERACTIVE} = true ]; then
  18. ./resources/install_dependencies.sh
  19. fi
  20. # Fullbuild or nonexistent venv
  21. if [ ${FULLBUILD} = true -o ! -d .venv ]; then
  22. rm -Rf .venv
  23. python3 -m venv .venv
  24. if [ $? != 0 ]; then
  25. echo "Error: Could not create the venv. Please make sure the 'venv' Python-module is installed."
  26. exit
  27. fi
  28. fi
  29. # Activate the venv
  30. source .venv/bin/activate
  31. # Install python packages
  32. pip3 install -r resources/requirements.txt
  33. # Deactivate the venv
  34. deactivate
  35. # Create the Makefile using cmake, from a clean build directory
  36. cd code_boost/src/build/
  37. if [ ${PWD##*/} = 'build' ]; then
  38. if [ ${FULLBUILD} = true ]; then
  39. # Only delete everything if we are in a folder called 'build'.
  40. rm -rf ./*
  41. fi
  42. else
  43. echo "Error: The 'build' directory was not found."
  44. exit
  45. fi
  46. which ninja &>/dev/null
  47. if [ $? != 0 ]; then
  48. cmake ..
  49. # Make sure we're able to get the number of cores
  50. if [ $(uname) = 'Darwin' ]; then
  51. NUMCORES=$(sysctl -n hw.logicalcpu)
  52. else
  53. NUMCORES=$(nproc)
  54. fi
  55. if [ -f Makefile ]; then
  56. make -j$NUMCORES
  57. else
  58. echo "Error: 'cmake' did not finish successfully."
  59. exit
  60. fi
  61. else
  62. cmake .. -G Ninja
  63. if [ -f build.ninja ]; then
  64. ninja
  65. else
  66. echo "Error: 'cmake' did not finish successfully."
  67. exit
  68. fi
  69. fi
  70. if [ $? -eq 0 ]; then
  71. cp libpcapreader.so ../../../code/ID2TLib/
  72. cp libbotnetcomm.so ../../../code/ID2TLib/Botnet
  73. else
  74. echo "Error: 'make' did not finish successfully."
  75. exit
  76. fi
  77. cd ../../../
  78. # Create the ID2T script
  79. cat >./id2t <<EOF
  80. #!/bin/bash
  81. # Find the executable
  82. if [ $(uname) = 'Darwin' ]; then
  83. ID2T_DIR=\$(greadlink -f \$0)
  84. else
  85. ID2T_DIR=\$(readlink -f \$0)
  86. fi
  87. SCRIPT_PATH=\${ID2T_DIR%/*}
  88. cd \$SCRIPT_PATH
  89. # Execute ID2T
  90. source .venv/bin/activate
  91. exec ./code/CLI.py "\$@"
  92. deactivate
  93. EOF
  94. # Create the test script
  95. cat >./run_tests <<EOF
  96. #!/bin/bash
  97. # Find the executable
  98. if [ $(uname) = 'Darwin' ]; then
  99. ID2T_DIR=\$(greadlink -f \$0)
  100. else
  101. ID2T_DIR=\$(readlink -f \$0)
  102. fi
  103. SCRIPT_PATH=\${ID2T_DIR%/*}
  104. cd \$SCRIPT_PATH
  105. source .venv/bin/activate
  106. # Regenerate the statistics DB
  107. ./id2t -i resources/test/reference_1998.pcap -r >/dev/null
  108. cd code
  109. # Execute tests
  110. set -e
  111. PRINT_COV=true
  112. testpath="discover -s Test/"
  113. if [ -e "Test/test_\$1.py" ]; then
  114. testpath="Test/test_\$1.py"
  115. PRINT_COV=false
  116. fi
  117. PYTHONWARNINGS="ignore" python3 -m coverage run --source=. -m unittest \$testpath >/dev/null
  118. if \$PRINT_COV ; then
  119. python3 -m coverage html
  120. python3 -m coverage report -m
  121. fi
  122. deactivate
  123. EOF
  124. # Create the test script
  125. cat >./test_efficiency <<EOF
  126. #!/bin/bash
  127. # Find the executable
  128. if [ $(uname) = 'Darwin' ]; then
  129. ID2T_DIR=\$(greadlink -f \$0)
  130. else
  131. ID2T_DIR=\$(readlink -f \$0)
  132. fi
  133. SCRIPT_PATH=\${ID2T_DIR%/*}
  134. TEST_DIR=\${SCRIPT_PATH}/resources/test/
  135. TEST_PCAP=\${TEST_DIR}reference_1998.pcap
  136. PLOT_DIR=\${TEST_DIR}/plot/
  137. cd \${SCRIPT_PATH}/code
  138. error=0
  139. # Execute tests
  140. set +e
  141. python3 -m unittest Test/efficiency_testing.py
  142. error=\$?
  143. cd \$SCRIPT_PATH
  144. source .venv/bin/activate
  145. mkdir \$PLOT_DIR
  146. smbloris="SMBLorisAttack attackers.count=4 packets.per-second=8.0"
  147. smbscan1="SMBScanAttack ip.src=192.168.178.1 ip.dst=192.168.178.10-192.168.179.253"
  148. smbscan2="SMBScanAttack ip.src=192.168.178.1 ip.dst=192.168.178.10-192.168.178.109 hosting.ip=192.168.178.10-192.168.178.109"
  149. ftp="FTPWinaXeExploit ip.src=192.168.178.1 ip.dst=192.168.178.10"
  150. porto="PortscanAttack ip.src=192.168.178.1 port.open=80"
  151. portc="PortscanAttack ip.src=192.168.178.1 port.open=20"
  152. sqli="SQLiAttack ip.dst=192.168.0.1"
  153. joomla="JoomlaRegPrivExploit ip.src=192.168.178.1"
  154. sality="SalityBotnet"
  155. ddos="DDoSAttack attackers.count=10 packets.per-second=95 attack.duration=10"
  156. ms17="MS17Scan ip.src=192.168.178.1"
  157. eb="EternalBlue"
  158. for i in "\$smbloris" "\$smbscan1" "\$smbscan2" "\$ftp" "\$porto" "\$portc" "\$sqli" "\$joomla" "\$sality" "\$ddos" "\$ms17" "\$eb"; do
  159. mprof run ./id2t -i \${TEST_PCAP} -a \${i}
  160. mprof plot -t "\${i}" -o "\${PLOT_DIR}\${i}.png"
  161. mv mprofile_* "\${PLOT_DIR}\${i}.dat"
  162. done
  163. echo "\nPlotted images can be found in \"\${TEST_DIR}\"."
  164. echo "By executing \"mprof plot <file>.dat\" you can get a more detailed look."
  165. deactivate
  166. exit \$error
  167. EOF
  168. chmod +x ./code/CLI.py
  169. chmod +x ./id2t
  170. chmod +x ./run_tests
  171. chmod +x ./test_efficiency
  172. echo -e "\n\nAll is set. ID2T is ready."
  173. echo -e "\nRun efficiency tests with the command './test_efficiency'"
  174. echo -e "Run unit tests with the command './run_tests'"
  175. echo -e "Run ID2T with the command './id2t'"