Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- TOS=2s
- PING_TOS=4
- BYPASS_DIR='/home/cs168/public'
- STARTER_DIR='Starter_files'
- # full log file
- LOG_FILE="Students.log"
- > ${LOG_FILE}
- MAX_SCORE=10
- test_firewall(){
- SCORE=0
- RES_TXT='test_tmp.txt'
- #launch python script
- chmod +x main.py
- sudo ./main.py --mode firewall --rule rules.conf &
- sleep 1
- ### DNS
- # test 1 : shoudl pass
- > ${RES_TXT}
- echo 'test 1: dig @8.8.8.8 freeuni.edu.ge -- PASS' | tee -a ${TMP}
- timeout ${TOS} dig @8.8.8.8 freeuni.edu.ge >> ${RES_TXT}
- echo 'Should Pass : ' >> ${TMP}
- if grep -q 'server found' "${RES_TXT}"; then
- SCORE=$((SCORE+1))
- echo 'True'>> ${TMP}
- else
- echo 'False'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- # test 2: should drop
- > ${RES_TXT}
- echo 'test 2: dig @8.8.8.8 mail.google.com -- DROP' | tee -a ${TMP}
- timeout ${TOS} dig @8.8.8.8 mail.google.com >> ${RES_TXT}
- LC="$(wc -l ${RES_TXT})"
- echo 'Should Drop : ' >> ${TMP}
- if [ "${LC}" == "0 ${RES_TXT}" ]; then
- SCORE=$((SCORE+1))
- echo 'True'>> ${TMP}
- else
- echo 'False'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- # test 3: should drop
- > ${RES_TXT}
- echo 'test 3: dig mail.ru -- DROP' | tee -a ${TMP}
- timeout ${TOS} dig mail.ru >> ${RES_TXT}
- LC="$(wc -l ${RES_TXT})"
- echo 'Should Drop : ' >> ${TMP}
- if [ "${LC}" == "0 ${RES_TXT}" ]; then
- SCORE=$((SCORE+1))
- echo 'True'>> ${TMP}
- else
- echo 'False'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- ### ICMP
- # test 4: should drop
- > ${RES_TXT}
- echo 'test 4: ping 8.8.8.8 -- DROP' | tee -a ${TMP}
- ping 8.8.8.8 >> ${RES_TXT} &
- PING_PID=$!
- sleep ${PING_TOS}
- sudo kill -2 ${PING_PID}
- echo 'Should Drop : ' >> ${TMP}
- if grep -q '0 received' "${RES_TXT}"; then
- SCORE=$((SCORE+1))
- echo 'True'>> ${TMP}
- else
- echo 'False'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- # test 5: should pass
- > ${RES_TXT}
- echo 'test 5: ping 185.179.191.81 -- PASS' | tee -a ${TMP}
- ping 185.179.191.81 >> ${RES_TXT} &
- PING_PID=$!
- sleep ${PING_TOS}
- sudo kill -2 ${PING_PID}
- echo 'Should pass : ' >> ${TMP}
- if grep -q '0 received' "${RES_TXT}"; then
- echo 'False'>> ${TMP}
- else
- SCORE=$((SCORE+1))
- echo 'True'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- # test 6: should pass
- > ${RES_TXT}
- echo 'test 6: ping localhost -- PASS' | tee -a ${TMP}
- ping localhost >> ${RES_TXT} &
- PING_PID=$!
- sleep 2
- sudo kill -2 ${PING_PID}
- echo 'Should pass : ' >> ${TMP}
- if grep -q '0 received' "${RES_TXT}"; then
- echo 'False'>> ${TMP}
- else
- SCORE=$((SCORE+1))
- echo 'True'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- ### TCP
- # test 7: should pass
- > ${RES_TXT}
- echo "test 7: echo 'GET / HTTP/1.1\r\n' | nc 185.179.191.81 80 -- PASS" | tee -a ${TMP}
- echo 'GET / HTTP/1.1\r\n' | timeout ${TOS} nc 185.179.191.81 80 >> ${RES_TXT}
- LC="$(wc -l ${RES_TXT})"
- echo 'Should Pass : ' >> ${TMP}
- if [ "${LC}" == "0 ${RES_TXT}" ]; then
- echo 'False'>> ${TMP}
- else
- SCORE=$((SCORE+1))
- echo 'True'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- # test 8: should drop
- > ${RES_TXT}
- echo "test 8: echo 'GET / HTTP/1.1\r\n' | nc 91.189.89.115 443 -- DROP" | tee -a ${TMP}
- echo 'GET / HTTP/1.1\r\n' | timeout ${TOS} nc 91.189.89.115 443 >> ${RES_TXT}
- LC="$(wc -l ${RES_TXT})"
- echo 'Should Drop : ' >> ${TMP}
- if [ "${LC}" == "0 ${RES_TXT}" ]; then
- SCORE=$((SCORE+1))
- echo 'True'>> ${TMP}
- else
- echo 'False'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- ### UDP
- # test 9: should drop
- > ${RES_TXT}
- echo 'test 9: dig example.com @199.7.91.13 -- DROP' | tee -a ${TMP}
- timeout ${TOS} dig example.com @199.7.91.13 >> ${RES_TXT}
- LC="$(wc -l ${RES_TXT})"
- echo 'Should Drop : ' >> ${TMP}
- if [ "${LC}" == "0 ${RES_TXT}" ]; then
- SCORE=$((SCORE+1))
- echo 'True'>> ${TMP}
- else
- echo 'False'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- # test 10: should pass
- > ${RES_TXT}
- echo 'test 10: dig example.com @192.58.128.30 -- DROP' | tee -a ${TMP}
- timeout ${TOS} dig example.com @192.58.128.30 >> ${RES_TXT}
- LC="$(wc -l ${RES_TXT})"
- echo 'Should Pass : ' >> ${TMP}
- if [ "${LC}" == "0 ${RES_TXT}" ]; then
- echo 'False'>> ${TMP}
- else
- SCORE=$((SCORE+1))
- echo 'True'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- # sleep 1
- sudo pkill python
- }
- prepare_workspace(){
- TAG=$1
- # empty temporary file
- > ${TMP}
- # cd to bypass mode dir
- cd ${BYPASS_DIR}
- # launch bypass mode to have git access
- sudo ./main.py --mode bypass > /dev/null &
- # save PID
- EX_ID=$!
- # cd to workspace
- cd -
- # checkout to stateless branch
- git checkout tags/${TAG} >> ${TMP} &>> ${TMP}
- # kill bypass mode
- sudo pkill python
- # check if exists
- GIT_ERR_MSG="error: pathspec 'tags/${TAG}' did not match any file(s) known to git."
- if grep -q "${GIT_ERR_MSG}" "${TMP}"; then
- HAS_DONE=0
- fi
- }
- for DIR in ./*/; # list directories in the form "/tmp/dirname/"
- do
- SCORE=0
- # evaluate dir name
- DIR=${DIR%*/} # remove the trailing "/"
- DIR=${DIR##*/}
- cd ${DIR}
- echo "Testing : ${DIR}"
- # Write student info into file
- echo "-----------------------------------" >> ../${LOG_FILE}
- echo -e "Student: ${DIR} \n" >> ../${LOG_FILE}
- # copy starter files
- cp ../../${STARTER_DIR}/* .
- # create temporary file for loging student's work
- TMP=../tmp.txt
- ### STATELESS ###
- HAS_DONE=1
- TAG='stateless'
- prepare_workspace ${TAG}
- if [ ${HAS_DONE} -eq 0 ]; then
- echo "${TAG} Homework Not Done..." >> ../${LOG_FILE}
- else
- test_firewall
- fi
- # Write temporary file to LOG file
- cat ${TMP} >> ../${LOG_FILE}
- # Write Results
- echo '' >> ../${LOG_FILE}
- echo "Score : ${SCORE}/${MAX_SCORE}" >> ../${LOG_FILE}
- echo "Total score: $(((SCORE*100 /MAX_SCORE )))" >> ../${LOG_FILE}
- echo ""
- cd ..
- done;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement