4

I know that Android is based on Linux. Also, I know PHP fairly well and I think PHP for Linux is a suitable choice (correct me if I'm wrong).

Many days ago, I faced a problem. The problem was and is, how to control packets using a PHP code? Can I do that?

Let me say further. To connect to the network, we have network interfaces (see /sys/class/net). In Android, there are many interfaces, such as wlan0 (for my case). It is used for such wireless connections as WiFi and WiFi tethering.

After a lot of researching, I found a Linux command called tc (Traffic Control) that is used to control network packets. However, I don't really know how to use that command to control bandwidth of each connected device to my WiFi hotspot. I want to set quotas for each one and if they reach them, I stop them from using my internet. I want to use PHP for that purpose. How to do that? (Sorry, but I'm a little bit new to Linux!)

Note: I want to limit my users via MAC address, not IP address.

MAChitgarha
  • 2,189
  • 2
  • 19
  • 30
  • 1
    how is this related to android ? what to do if wifi abusers are connecting via a puter, or even ... ghast ... some iOS device ? – YvesLeBorg Jan 13 '18 at 10:51
  • @YvesLeBorg Isn't it related to Android? I want to know if it's possible to control my wireless connection in my Android device to prevent others (that I know who are them) from eating my data. And no worry about my users, I will secure it using a strong password, and nobody has an iOS device! – MAChitgarha Jan 13 '18 at 12:07

2 Answers2

4

You could try to sniff packages with

$socket = socket_create(AF_INET , SOCK_RAW , SOL_TCP);  

Here is a guide with more information: https://www.binarytides.com/code-a-packet-sniffer-in-php/

Or you could use the following PHP library: https://github.com/marcelog/SimplePcap

Here is an example from their github repository: https://github.com/marcelog/SimplePcap/blob/master/example/sniff.php

Gudgip
  • 825
  • 9
  • 19
  • Can I recognize users even by their MAC addresses using this way? If yes, how? And if no, could you please add an alternative way to do that? – MAChitgarha Apr 10 '18 at 14:17
  • 1
    2 ways to get the mac address: send it in the requests via a header or cookie. Or they have to be in thesame LAN (ethernet segment). See this question: https://stackoverflow.com/questions/1420381/how-can-i-get-the-mac-and-the-ip-address-of-a-connected-client-in-php – Gudgip Apr 10 '18 at 14:21
  • There's no exact answer in the link. They say you can't do that, however, I know that it's possible in my case. If you go to WiFi tethering configuration part, you'll see that you can restrict users by their MAC address. Also, about what you said, I don't know whether headers or cookies are reliable or not. From my knowledge, I think I should use tc command to get clients MAC addresses, but it would be a challenge to do that. – MAChitgarha Apr 13 '18 at 06:42
  • 1
    Yes, restricting MAC addresses IN the router. It's the router that handles those, not a server behind the router. You'd need to have a router API to achieve what you want :-) – Gudgip Apr 13 '18 at 07:51
  • Giving the bounty because of pointing to the 'sniff' keyword. Thanks! However, it's not exactly what I want. – MAChitgarha Apr 13 '18 at 08:58
  • 1
    You have helped me a lot by putting that GitHub link. Recently, I followed it and my problem is going to be solved. Thanks you very very much! – MAChitgarha Jun 11 '18 at 11:12
1

Thanks to @Gudgip, I've written a command line tool which runs on Linux (also, obviously Android) using the power of PHP that sniff packets over an interface and saves the results into files. For example, you can see how much data each user used from your WiFi. See:

https://github.com/machitgarha/dej

If you see issues, please open one. Hope it helps someone!

MAChitgarha
  • 2,189
  • 2
  • 19
  • 30