Connection Refused

Discussions about using Linux guests in VirtualBox.
Post Reply
OmShiva
Posts: 5
Joined: 12. Dec 2021, 16:04

Connection Refused

Post by OmShiva »

I have Virtual Box on Windows and have a kali OS runing with VBox.

I keep getting connection refused when I try to use telnet or ssh or any other means.

I wrote a python to open a socket and the socket cant be created as its says connection refused


┌──(kali㉿kali)-[~]
└─$ ping localhost -c 2 1 ⨯
PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.018 ms
64 bytes from localhost (::1): icmp_seq=2 ttl=64 time=0.036 ms

--- localhost ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1015ms
rtt min/avg/max/mdev = 0.018/0.027/0.036/0.009 ms

┌──(kali㉿kali)-[~]
└─$ telnet localhost
Trying ::1...
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused




Any ideas anyone ?
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Connection Refused

Post by scottgus1 »

I assume you are telnetting or SSHing from the host OS into the VM OS. It is possible, I think, to do these within the VM OS too, so you should clarify where you are telnetting or SSHing from.

If you're trying these from the host OS, your VM needs a network between the host OS and the VM OS. See Virtualbox Networks: In Pictures. Possible network options could be Bridged, NAT or NAT Network, and Host-Only.

Bridged and Host-Only make an open network between host and VM. Bridged also requires an active network device connected to the network adapter being Bridged to. Bridged does not always work with Wi-Fi.NAT and NAT Network act as a router between host and VM, so would need port forwarding. The VM also requires these services to be open in its firewall.
OmShiva
Posts: 5
Joined: 12. Dec 2021, 16:04

Re: Connection Refused

Post by OmShiva »

My Host VM is Windows 11 my VM OS is Kali Linux.
I am using a Bridged Connection and not NAT Connection

I am only using ssh and telnet to test for connection on my localhost (127.0.0.1)

I wrote this Python script that open a Socket on localhost port 8080 and this script should work but keep getting connection refused.

┌──(kali㉿kali)-[~/Downloads/Python_progs]
└─$ python3 client.py
kali
127.0.0.1 8080
Traceback (most recent call last):
File "/home/kali/Downloads/Python_progs/client.py", line 15, in <module>
client.connect((host, port))
ConnectionRefusedError: [Errno 111] Connection refused

┌──(kali㉿kali)-[~/Downloads/Python_progs]
└─$ 1 ⨯

┌──(kali㉿kali)-[~/Downloads/Python_progs]
└─$ cat client.py 1 ⨯
#!/usr/bin/python3
#client.py

import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#host = socket.gethostname()
host = "127.0.0.1"
print(socket.gethostname())

port = 8080

print(host,port)
client.connect((host, port))
msg = client.recv(1024)
client.close()
print(msg.decode('ascii'))


┌──(kali㉿kali)-[~/Downloads/Python_progs]
└─$
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Connection Refused

Post by scottgus1 »

Right-click the VM in the main Virtualbox window's VM list, choose Show in Explorer/Finder/File Manager. Zip the VM's .vbox file (not the .vbox-prev file), and post the zip file, using the forum's Upload Attachment tab. (Configure your host OS to show all extensions if the folder that opens does not show a .vbox file.)

In the Windows OS, open a Command Prompt and run ipconfig /all. Post the command output.

In the Linux OS, open a Terminal and run ifconfig or ip address. Post the command output.

In the Linux OS, please ping 8.8.8.8 and ping your network's gateway IP address. Post the outputs.

Finally, please be sure Kali's firewall allows ping, then ping your VM's Bridged IP address from the host. Post the output.
OmShiva wrote:I am only using ssh and telnet to test for connection on my localhost (127.0.0.1)
Are you in the host OS or the VM OS when you are running these commands? Localhost and 127.0.0.1 only work on the OS you are running these commands on. They will not go across the bridged connection.

If these commands are running in the VM OS then Virtualbox's network is not involved and the problem is inside Kali, not Virtualbox.
OmShiva
Posts: 5
Joined: 12. Dec 2021, 16:04

Re: Connection Refused

Post by OmShiva »

Windows Hots ipconfig /all


C:\Users\nmist>ipconfig /all

Windows IP Configuration

Host Name . . . . . . . . . . . . : Shiva
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter Ethernet 2:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : ExpressVPN TAP Adapter
Physical Address. . . . . . . . . : 00-FF-2C-AD-66-E9
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes

Unknown adapter Local Area Connection:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : ExpressVPN TUN Driver
Physical Address. . . . . . . . . :
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes

Ethernet adapter Ethernet:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Intel(R) Ethernet Controller (2) I225-V
Physical Address. . . . . . . . . : F0-2F-74-22-CD-EC
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : fe80::53:d286:3f96:c43b%21(Preferred)
IPv4 Address. . . . . . . . . . . : 192.168.42.124(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : 10 December 2021 10:51:17
Lease Expires . . . . . . . . . . : 13 December 2021 09:28:59
Default Gateway . . . . . . . . . : 192.168.42.1
DHCP Server . . . . . . . . . . . : 192.168.42.1
DHCPv6 IAID . . . . . . . . . . . : 217067380
DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-29-2E-83-06-F0-2F-74-22-CD-EC
DNS Servers . . . . . . . . . . . : 192.168.42.1
NetBIOS over Tcpip. . . . . . . . : Enabled

Ethernet adapter VirtualBox Host-Only Network:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : VirtualBox Host-Only Ethernet Adapter
Physical Address. . . . . . . . . : 0A-00-27-00-00-0F
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : fe80::91f8:9bab:79f5:f567%15(Preferred)
IPv4 Address. . . . . . . . . . . : 192.168.56.1(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
DHCPv6 IAID . . . . . . . . . . . : 688521255
DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-29-2E-83-06-F0-2F-74-22-CD-EC
NetBIOS over Tcpip. . . . . . . . : Enabled

Wireless LAN adapter Wi-Fi:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Intel(R) Wi-Fi 6 AX200 160MHz
Physical Address. . . . . . . . . : E0-D4-64-02-9C-93
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes

Wireless LAN adapter Local Area Connection* 1:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual Adapter
Physical Address. . . . . . . . . : E0-D4-64-02-9C-94
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes

Wireless LAN adapter Local Area Connection* 2:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual Adapter #2
Physical Address. . . . . . . . . : E2-D4-64-02-9C-93
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes

Ethernet adapter Bluetooth Network Connection:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Bluetooth Device (Personal Area Network)
Physical Address. . . . . . . . . : E0-D4-64-02-9C-97
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes

C:\Users\nmist>


VM Host - Kali Ifconfig

┌──(kali㉿kali)-[~]
└─$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.42.186 netmask 255.255.255.0 broadcast 192.168.42.255
inet6 fe80::a00:27ff:feb9:bdd1 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:b9:bd:d1 txqueuelen 1000 (Ethernet)
RX packets 4559 bytes 792570 (773.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 445 bytes 47390 (46.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 8 bytes 400 (400.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 400 (400.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


┌──(kali㉿kali)-[~]
└─$


ping 8.8.8.8

┌──(kali㉿kali)-[~]
└─$ ping 8.8.8.8 -c 5
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=117 time=16.0 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=117 time=18.9 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=117 time=17.6 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=117 time=16.2 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=117 time=16.3 ms

--- 8.8.8.8 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4066ms
rtt min/avg/max/mdev = 15.952/16.996/18.910/1.107 ms

┌──(kali㉿kali)-[~]
└─$

Ping Kali host VM IP address from Windows Host

C:\Users\nmist>ping 192.168.42.186

Pinging 192.168.42.186 with 32 bytes of data:
Reply from 192.168.42.186: bytes=32 time<1ms TTL=64
Reply from 192.168.42.186: bytes=32 time<1ms TTL=64
Reply from 192.168.42.186: bytes=32 time<1ms TTL=64

Ping statistics for 192.168.42.186:
Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
Control-C
^C
C:\Users\nmist>

In regards to your question, I am running the commands from Kali
I have tried this on my Parrot OS VM and I have the same problem there also wherby I get connection refused
Attachments
OSCP - Linux 2021.3.zip
(2.45 KiB) Downloaded 7 times
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Connection Refused

Post by scottgus1 »

Thanks for all the info. Your VM is properly set up to Bridged, and a full connected network is in place.

However, since your telnet/ssh commands/script are being run inside the VM pointed at the VM OS's localhost 127.0.0.1, nothing in Virtualbox is causing the 'connection refused'. Kali and Parrot are not allowing the connection.

Virtualbox is not causing this problem. Look at Kali/Parrot/Debian/Python help channels to find out what is wrong.
OmShiva
Posts: 5
Joined: 12. Dec 2021, 16:04

Re: Connection Refused

Post by OmShiva »

I have just installed VMWare and installed kali OS..

I am having the same issue there also, I think its something to do with Windows 11.

Thank You for taking time to heklp me out.
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Connection Refused

Post by scottgus1 »

OmShiva wrote:I think its something to do with Windows 11.
Ok, but personally, I think you're looking in the wrong place:
OmShiva wrote:I am running the commands from Kali
OmShiva wrote:┌──(kali㉿kali)-[~/Downloads/Python_progs]
└─$ python3 client.py
kali
127.0.0.1 8080
Traceback (most recent call last):
File "/home/kali/Downloads/Python_progs/client.py", line 15, in <module>
client.connect((host, port))
ConnectionRefusedError: [Errno 111] Connection refused
scottgus1 wrote:since your telnet/ssh commands/script are being run inside the VM pointed at the VM OS's localhost 127.0.0.1, ... Kali and Parrot are not allowing the connection.... Look at Kali/Parrot/Debian/Python help channels to find out what is wrong.
Your script is working only in the VM and never leaves the VM.
fth0
Volunteer
Posts: 5668
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: Connection Refused

Post by fth0 »

Your problem is that you simply have no SSH or telnet server running, at least not listening on port 8080, where your Python script tries to connect to.

Take a look at socket — Low-level networking interface — Example for a simple example.
OmShiva
Posts: 5
Joined: 12. Dec 2021, 16:04

Re: Connection Refused

Post by OmShiva »

This problem is now solved.

What I had done wrong was that I did not open a listner on port 8080

I createed a new python file names server.py which set up a python listner on port 8080

Then I executed my original client.py

and now it all works

This was not a fault with VM

Code: Select all

#!/usr/bin/python3
#server.py

import socket 

host = socket.gethostname()
port = 8080
     
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host,port))
server.listen(2)
print('Server is listening for incoming connections')
     
while True:
    conn,addr = server.accept()
    print("Connection Received from %s" % str(addr))
    msg = 'Connection Established'+ "\r\n"
    conn.send(msg.encode('ascii'))
    conn.close()
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Connection Refused

Post by scottgus1 »

Great! Glad you're up and running.
Post Reply