build.sh 5.0 KB

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