19

I have a Moto G3 Turbo with Android 6.0.1 Marshmallow. I use the phone to make WiFi hotspot so I can use internet on my laptop.

I was able to do a successful Metasploit over WAN/Internet, and for that I need to port-forward to my laptop. But the problem is, I don't have any modem or router to actually go to setting login with admin password or anything like that.

What can I do? Is there any app that can do this and whether it needs root permission?

3
  • 4
    Generally you can't, not because Android isn't capable, but because in general carriers don't allow inbound traffic of any kind to mobile devices. You may get an IP address from your carrier, but that is just for outbound traffic, unsolicited inbound traffic (the kind you need port forwarding for) is nearly always blocked by every carrier world-wide. Add to that most carriers don't give you a public IP anyway, so even if you did get port forwarding to work, you likely couldn't use it.
    – acejavelin
    Feb 4, 2017 at 14:46
  • That's not true, if your application could not receive inbound traffic how you getting message to your phone. Nov 2, 2017 at 2:35
  • 4
    @ShawnThomasRheal by opening an outbound connection and keep it open for replies. That's what most apps use GCM for – so not every app has to keep its own connection open.
    – Izzy
    Nov 2, 2017 at 7:18

1 Answer 1

14

In most cases you won't be able to make inbound connection when using Mobile Data because of CGNAT and firewalls at ISP level, as explained by acejavelin in comment. To know the problem in detail and how it can be worked around see How to connect to Android through SSH over 3G/4G public IP?

However if you can reach your phone from internet, extending this access to a connected host (PC in your case) is not a problem. Port forwarding is done with routers, and when being used as a Wi-Fi hotspot (or with USB tethering), your phone is in fact acting as a router.

ROOT SOLUTION:

In order to forward port, you need to do Destination Address Translation (DNAT) using Linux kernel's builtin firewall userspace tool iptables.

First make sure that kernel is configured to forward internet traffic (already set when hotspot is ON):

~# echo 1 >/proc/sys/net/ipv4/ip_forward

Let's say the IP address assigned by hotspot to PC is 192.168.43.10 and you want to forward SSH default port (22):

~# iptables -t nat -I PREROUTING -p tcp --dport 22 -j DNAT --to 192.168.43.10

This is the simplest form of Port Forwarding.

If default POLICY of FORWARD chain in FILTER table is not ACCEPT, define rules explicitly to accept the forwarded packets:

~# iptables -I FORWARD -p tcp -d 192.168.43.10 --dport 22 -j ACCEPT

Also accept outbound forwarded traffic if needed.

NON-ROOT SOLUTION:

On non-rooted devices you can use adb or some other minimal TCP/UDP server like netcat or socat for port forwarding. Note that this won't work for ports under 1024. To forward port 2222:

~$ adb reverse tcp:2222 tcp:2222

Or on phone:

~$ nc -lk -p 2222 -e nc 192.168.43.10 2222

* nc is a busybox applet. But make sure that you get the right binary, there are multiple implementations of netcat.
* Use -u option to forward UDP port.

However nc accepts only a single connection at a time. Use socat or ncat for more advanced options, e.g. from Termux:

~$ socat tcp-l:2222,fork,reuseaddr tcp:192.168.43.10:2222

See Using netcat for port forwarding.

There are also apps like Fwd (it's open-source, I have no affiliation) meant for port forwarding.


SOURCE:

3
  • However if you can reach your phone from internet. What do you mean by this line. How do I know whether I can reach internet or not? Jun 29, 2020 at 12:05
  • 1
    @AkashKarnatak see the line above: How to connect to Android through SSH over 3G/4G public IP? Jun 29, 2020 at 17:07
  • 1
    Wasn't able to do that through 'adb reverse' (although it should work from what I've read), but ncat worked beautifully right from the first time! I am already running multiple instances of it, for redirecting multiple ports, like RDP and SMB. May 26, 2021 at 16:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .