build.sh 4.6 KB

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