SHARE
TWEET

Shodan IPCam extractor

a guest Oct 31st, 2015 425 Never
  1. #!/bin/bash
  2. #
  3. # By Arsouill3 @ 8ch.net/ipcam/
  4. #
  5. # Shodan IPCam Extractor allows you to download IP (of IPCam) from Shodan.io thanks to its API. You'll need to suscribe either Developer or Freelancer plan
  6. # (https://developer.shodan.io/billing/signup) to use it.
  7. # cURL (http://curl.haxx.se/download.html) and jq (https://stedolan.github.io/jq/download/) are required.
  8. # The script has been written to get all IP (of IPCam) all over the world on a daily basis. If you want to do so, then edit crontab (crontab -e) as follow :
  9. #       * 20 * * * PATH/shodan_ipcam_extractor.sh $country_iso_2letters $date (script will be launched everyday at 8 p.m.)
  10. #
  11. # ./shodan_ipcam_extractor.sh $country_iso_2letters $date :
  12. #       * $country_iso_2letters : Alpha-2 code of a country (https://www.iso.org/obp/ui/#search) | all
  13. #       * $date : all | y (stands for yesterday)
  14.  
  15. api_key="" # Paste your Shodan's API key here
  16. country=$1
  17. date=$2
  18. today=$(date +'%s')
  19. shodan_today=$(date --date="@$today" "+%d/%m/%Y")
  20. yesterday=$(($today-24*3600))
  21. bfyesterday=$(($yesterday-24*3600))
  22. shodan_bfyesterday=$(date --date="@$bfyesterday" "+%d/%m/%Y")
  23. query1="netwave ip camera country:$country before:$shodan_today after:$shodan_bfyesterday" # Download all IPCam of $country posted on yesterday
  24. query2="netwave ip camera country:$country" # Download all IPCam of $country
  25. query3="netwave ip camera" # Download all IPCam
  26. query4="netwave ip camera before:$shodan_today after:$shodan_bfyesterday" # Download all IPCam posted yesterday
  27.  
  28. if [ ! -d "./JSON_files" ]
  29. then
  30.         mkdir "./JSON_files"
  31. fi
  32.  
  33. function collect_ip { # collect_ip $api_key $query
  34. i=1
  35. nbr_ip=1
  36. while [ "$nbr_ip" -gt "0" ]
  37. do
  38.         code_status=$(curl -L -w "%{http_code}" "https://api.shodan.io/shodan/host/search?key=$1&query=$2&page=$i" -o ./JSON_files/shodan_$i.json)
  39.         if [ "$code_status" -ne "200" ]
  40.         then
  41.                 echo "Error ! Maybe a lack of credit ? $(date "+%c")" >> ./error_shodan.log
  42.                 nbr_ip=0
  43.                 i=$(($i+1))
  44.         else
  45.                 nbr_ip=$(jq '.matches | length' < ./JSON_files/shodan_$i.json)
  46.                 i=$(($i+1))    
  47.         fi
  48. done
  49. i=$(($i-1))
  50. rm "./JSON_files/shodan_$i.json"
  51. }
  52.  
  53. function extract_ip {
  54. nbr_files=$(ls ./JSON_files/ | wc -l)
  55. if [ "$nbr_files" -gt "0" ]
  56. then
  57.         for (( j=1; $j<=$nbr_files; j++ ))
  58.         do
  59.                 nbr_ip=$(jq '.matches | length' < ./JSON_files/shodan_$j.json)
  60.                 for (( i=0; $i<=$nbr_ip-1; i++ ))
  61.                 do
  62.                         ip=$(jq --raw-output .matches[$i].ip_str < ./JSON_files/shodan_$j.json)
  63.                         printf "%s\n" "$ip" >> ./shodan_ip.txt
  64.                 done
  65.         rm "./JSON_files/shodan_$j.json"
  66.         echo "File $j processed."
  67.         done
  68. else
  69.         echo "JSON_files directory is empty !"
  70.         exit
  71. fi
  72.  
  73. # Split shodan_ip.txt into files of 4000 lines max each
  74. if [ -f "./shodan_ip.txt" ]
  75. then
  76.         nbr_line_ip=$(wc -l ./shodan_ip.txt | sed 's/^\(.*\) \..*/\1/')
  77.         split=$(($nbr_line_ip/4000)) # 4000 is nearly the max quantity of IP to prevent Joe's FileDownloader of crashing...
  78.         for (( i=1; $i<=$split; i++ ))
  79.         do
  80.                 j=$((4000*$i-3999))
  81.                 k=$((4000*$i))
  82.                 sed -n -e $j,$k'p' ./shodan_ip.txt > ./shodan_ip_$i.txt
  83.         done
  84.         sed -n -e $(($k+1)),'$p' ./shodan_ip.txt > ./shodan_ip_$i.txt
  85. else
  86. touch ./shodan_ip.txt
  87. fi
  88. }
  89.  
  90. # Beigining of "main"
  91. if [ "$country" = "all" ]
  92. then
  93.         if [ "$date" = "all" ]
  94.         then
  95.                 collect_ip $api_key $query3
  96.                 extract_ip
  97.         else
  98.                 collect_ip $api_key $query4
  99.                 extract_ip
  100.         fi
  101. else
  102.         if [ "$date" = "y" ]
  103.         then
  104.                 collect_ip $api_key $query1
  105.                 extract_ip
  106.         else
  107.                 collect_ip $api_key $query2
  108.                 extract_ip
  109.         fi     
  110. fi
RAW Paste Data
Top