#!/bin/sh

# $ sudo cp -r ~/dshield/srv/www /srv
# $ sudo vi /lib/systemd/system/webpy.service

<<comment

[Unit]
Description=DShield Web Honeypot
After=systemd-networkd-wait-online.service
Wants=systemd-networkd-wait-online.service

[Service]
Type=idle
WorkingDirectory=/srv/www/bin
User=cowrie
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=webpy
ExecStart=/usr/bin/python3 /srv/www/bin/web.py

[Install]
WantedBy=multi-user.target

comment

# $ sudo ln -s /lib/systemd/system/webpy.service /etc/systemd/system/multi-user.target.wants/webpy.service
# $ sudo chown -R cowrie:root /srv/www/DB
# $ sudo systemctl enable webpy.service
# $ sudo systemctl start webpy.service
# $ sudo systemctl status webpy.service

# Setup cronjob to run every hours

# Dump the cowrie web logs every hours
#0 * * * * /home/guy/scripts/sqlite.sh  > /dev/null 2>1&


STARTDATE=$(date -d "1 hour ago" '+%m/%d/%Y %H:00:00')
STARTEPOCH=$(date -d "$STARTDATE" +%s)
ENDDATE=$(date "+%m/%d/%Y %H:00:00")
ENDEPOCH=$(date -d "$ENDDATE" +%s)
QUOTE='"'

#STARTEPOCH=$QUOTE$STARTEPOCH$QUOTE
#ENDEPOCH=$QUOTE$ENDEPOCH$QUOTE

echo $STARTEPOCH $ENDEPOCH

# Output cowrite SQLite logs to file


sqlite3 /srv/www/DB/webserver.sqlite '.mode insert' 'SELECT date, headers,address,cmd,path, useragent,vers,targetip from REQUESTS WHERE date BETWEEN '$STARTEPOCH' AND '$ENDEPOCH''  | sed "s/..n',char(10)),//g" | awk '{gsub(/\\n/," ")}1' > ~/sqlite/sqlite.sql

sqlite3 /srv/www/DB/webserver.sqlite '.mode insert' 'SELECT date, headers,address,cmd,path,vers from POSTLOGS WHERE date BETWEEN '$STARTEPOCH' AND '$ENDEPOCH'' | sed "s/..n',char(10)),//g" | awk '{gsub(/\\n/," ")}1' >> ~/sqlite/sqlite.sql

