Furari
Posts: 11
Joined: Fri Oct 05, 2018 12:59 pm

Tracking outsider's IP Address

Fri Oct 05, 2018 1:25 pm

I've successfully set up a L.A.M.P. on my RPi 3B+ for my final's requirement but my professor tasked me to add more features to my project.

One such is to make a log of visitors of the Raspberry Pi, and send an SMS notification whenever a outsider (Outside the Raspberry Pi's home network) enters. The user should also be able to extract that log whenever he/she wishes so.

Is it possible? If so, can anyone point me to the right direction? Thanks in advance :D

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Tracking outsider's IP Address

Fri Oct 05, 2018 1:53 pm

It is possible. I did this for a customer several years ago. He wanted to know the same thing about his Apache server. The first day wasn't bad, but by the end of the week, he called and told me to "shut that f**king thing off!". He was getting dozens of emails (sms for you) every hour. Just FYI.

Are you using php? That is what I used as I recall.

Andyroo

Re: Tracking outsider's IP Address

Fri Oct 05, 2018 2:06 pm

Good luck with your exams.

One simple way is to use the Apache logs - the access log records the source IP and you can have conditional logging to avoid local IPs else a bit of php to find the source IP on each page (header / footer) would do it.

SMS means either using a GSM card (SIM800 or SIM900 modules pop up on here - see other posts here for them) or a service such as https://www.textanywhere.net (used them for over 10 years now). Note being a poor student, you may want to look at https://www.sendatext.co who accept donations.

A few other add ons you could consider:
a) Send a report each day given visitor / hit counts by mail
b) Link to io.adafruit to give graphical hit rates per hour
c) Send errors 404 / 500 etc by text / mail

I’ll leave it up to you to work out how :D :!: :D though one clue - $_SERVER['REMOTE_ADDR'] not what the user sends...

Andyroo

Re: Tracking outsider's IP Address

Fri Oct 05, 2018 2:12 pm

SurferTim wrote:
Fri Oct 05, 2018 1:53 pm
It is possible. I did this for a customer several years ago. He wanted to know the same thing about his Apache server. The first day wasn't bad, but by the end of the week, he called and told me to "shut that f**king thing off!". He was getting dozens of emails (sms for you) every hour. Just FYI.

Are you using php? That is what I used as I recall.
Been there on both sides of the conversation. Once put a monitoring system where 404s where notified and turned it off after been hit by a bot following dead links :oops:

User avatar
DougieLawson
Posts: 36570
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Tracking outsider's IP Address

Fri Oct 05, 2018 3:00 pm

Furari wrote:
Fri Oct 05, 2018 1:25 pm
One such is to make a log of visitors of the Raspberry Pi, and send an SMS notification whenever a outsider (Outside the Raspberry Pi's home network) enters. The user should also be able to extract that log whenever he/she wishes so.
Start up a web server with port 80 open to the internet wait for a day then run Webalizer(*) on the logs. Post the public URL on here, wait for another day and analyse again. I think you'll be surprised how much traffic arrives even when you haven't published the URL.

Perhaps have a look at how Fail2ban reads the apache access.log and error.log to detect the bad actors. There's also Apache2 mod_security which can monitor things that are happening on your web server.

You probably don't want anything for normal regular visitors. You may want to see the folks who trip over Fail2ban or Mod_Security but even that happens too often to worry about.

My Fail2ban running since Thu 2018-10-04 08:33:57 has already blocked 21 banal attacks. On my system they get one shot at attacking then they are blocked forever (or until I clean the Fail2ban database of junk). I don't care what they were attempting to do, I don't care that they're now blocked forever.

(*) Other log analysis tools are available. Webalizer is just the one I played with years ago when I was almost interested in this stuff.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Furari
Posts: 11
Joined: Fri Oct 05, 2018 12:59 pm

Re: Tracking outsider's IP Address

Sat Oct 06, 2018 2:25 pm

SurferTim wrote:
Fri Oct 05, 2018 1:53 pm
It is possible. I did this for a customer several years ago. He wanted to know the same thing about his Apache server. The first day wasn't bad, but by the end of the week, he called and told me to "shut that f**king thing off!". He was getting dozens of emails (sms for you) every hour. Just FYI.

Are you using php? That is what I used as I recall.
Yes, I am using PHP.

Furari
Posts: 11
Joined: Fri Oct 05, 2018 12:59 pm

Re: Tracking outsider's IP Address

Sat Oct 06, 2018 2:31 pm

Andyroo wrote:
Fri Oct 05, 2018 2:06 pm
Good luck with your exams.

One simple way is to use the Apache logs - the access log records the source IP and you can have conditional logging to avoid local IPs else a bit of php to find the source IP on each page (header / footer) would do it.

SMS means either using a GSM card (SIM800 or SIM900 modules pop up on here - see other posts here for them) or a service such as https://www.textanywhere.net (used them for over 10 years now). Note being a poor student, you may want to look at https://www.sendatext.co who accept donations.

A few other add ons you could consider:
a) Send a report each day given visitor / hit counts by mail
b) Link to io.adafruit to give graphical hit rates per hour
c) Send errors 404 / 500 etc by text / mail

I’ll leave it up to you to work out how :D :!: :D though one clue - $_SERVER['REMOTE_ADDR'] not what the user sends...
Thanks!

I've been planning to use a mobile broadband for the SMS functionality, would that be possible?

Andyroo

Re: Tracking outsider's IP Address

Sat Oct 06, 2018 8:20 pm

Not sure on the mobile broadband SMS as I’ve done it under Windows but that used a program from Vodaphone to do it.

SMS is really a phone ability not data (unlike MMS that just makes a mess of both) but if you have a data link then you could always send emails.

Furari
Posts: 11
Joined: Fri Oct 05, 2018 12:59 pm

Re: Tracking outsider's IP Address

Sun Oct 07, 2018 4:28 am

Andyroo wrote:
Sat Oct 06, 2018 8:20 pm
Not sure on the mobile broadband SMS as I’ve done it under Windows but that used a program from Vodaphone to do it.

SMS is really a phone ability not data (unlike MMS that just makes a mess of both) but if you have a data link then you could always send emails.
Can you teach me how you did it? My professor surely wouldn't mind if I used anything other than a mobile broadband.

Andyroo

Re: Tracking outsider's IP Address

Sun Oct 07, 2018 3:45 pm

To send SMS you could use the card covered here https://www.modmypi.com/blog/how-to-con ... 3g-network though there is both the cost of the card and the cellular service to run it.

I used https://www.textanywhere.net for many years in a commercial environment and you get a few text messages FOC for trying them :lol: and all you need to do is send an email to a specific address with the text of the SMS as the email body.

A more 'complex' solution is https://www.twilio.com but they support Python and have documentation with a rough example. I did not go this way as the email function was available from more devices / monitoring solutions and got a good deal from Text Anywhere in both companies.

I may be able to get to a free Pi this week - I hoped to get free before now but Windows 10 ( :evil: spit :evil: ) has just stuffed me well and truly and I'm having to use my own Mac to pick the bits up :cry:

Have a read of both services and let me know if they fit.

Furari
Posts: 11
Joined: Fri Oct 05, 2018 12:59 pm

Re: Tracking outsider's IP Address

Sun Oct 07, 2018 4:12 pm

Andyroo wrote:
Sun Oct 07, 2018 3:45 pm
To send SMS you could use the card covered here https://www.modmypi.com/blog/how-to-con ... 3g-network though there is both the cost of the card and the cellular service to run it.

I used https://www.textanywhere.net for many years in a commercial environment and you get a few text messages FOC for trying them :lol: and all you need to do is send an email to a specific address with the text of the SMS as the email body.

A more 'complex' solution is https://www.twilio.com but they support Python and have documentation with a rough example. I did not go this way as the email function was available from more devices / monitoring solutions and got a good deal from Text Anywhere in both companies.

I may be able to get to a free Pi this week - I hoped to get free before now but Windows 10 ( :evil: spit :evil: ) has just stuffed me well and truly and I'm having to use my own Mac to pick the bits up :cry:

Have a read of both services and let me know if they fit.
Twilio would be a better fit for me, I think. One of my colleagues made a two-factor authenticator (RPi too) using it.

I'm sorry I am really new to this so I have no idea what to do next. How am going to do my task with Twilio?

Andyroo

Re: Tracking outsider's IP Address

Sun Oct 07, 2018 7:42 pm

You will need to create a PHP script for your web page that does the following:

When loaded retrieve the requesters IP address

Check if its on the local network (you will have to agree what range this is with your tutor - possibly 192.168.x.x or 10.x.x.x or 172.16.x.x - 172.31.x.x)

If its an outsider you then need to send the message PHP example is at https://www.twilio.com/docs/sms/send-messages

Things to think about:
1) Web pages can create many requests when loading so you may need to think about where the code is and how to run it only once
2) If the requester looks at multiple pages - do you want to send a message for each page?
3) If the requester returns - do you want to send another message?

My PHP is not hot (normally limited to hacking the odd WordPress script) - maybe if you get stuck at that stage one of the programming forums here will be able to help

Return to “Networking and servers”