build.sh 4.8 KB

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