Basically I started to use prepared statements after I've read some articles on the internet which said that it's an easy way to prevent sql injection. Jumping to my scenario, I have a logExit function which inserts into the DB the page that the visitor came from on my website and the page where he is going after leaving my website.
public function logExit($url){
$HTTP_REFERER = ($_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : '');
$REMOTE_ADDR = ($_SERVER['REMOTE_ADDR'] ? $_SERVER['REMOTE_ADDR'] : '');
$params = array(
$REMOTE_ADDR,
$HTTP_REFERER,
$url
);
// custom query function ~ prepare/execute
$this->query("
INSERT INTO exits(ip, url_in, url_out) VALUES (?, ?, ?)
", "sss", $params);
}
The thing is that, today when I logged into the admin panel to check how many visitors did I had today and where did they left from my website, I got tons of alerts. When I checked my database I had like 13500 exits from the same IP to different pages but the issue came from the url_in
field, here are some examples:
ip | url_in | url_out |
-----------+-------------------------------------+---------------|
xx.xx.xx |'"></a><script>alert(11779)</script> | xxxxxxxxxxxxx |
-----------+-------------------------------------+---------------|
xx.xx.xx | ,alert(11774), | xxxxxxxxxxxxx |
-----------|-------------------------------------|---------------|
xx.xx.xx | %c0%a7 | xxxxxxxxxxxxx |
-----------|-------------------------------------|---------------|
xx.xx.xx | %2527 | xxxxxxxxxxxxx |
-----------+-------------------------------------+---------------|
HTTP_REFERER = url_in
$url = url_out
So this seem to be strange because as far as I know the HTTP_REFERER should be an URL. How did this mess get into my database and how to prevent it?
Thanks
PS. The hosting company showed me this log, what does it mean?
access-logs/my-example-website.com:xxx.xxx.xxx.xx - - [21/Dec/2017:18:59:59 +0200] "GET /mode.php?admin_mode=1&referer=javascript:alert(16555) HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"