So I'm trying to send multiple packets over and over again to my VM, but after one attempt, I get the error:
Traceback (most recent call last):
File "SMB_Test2.py", line 157, in <module>
s.sendall(SMB_COM_NEGOTIATE)
File "C:\Python27\Lib\socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10054] An existing connection was forcibly closed by the remote host
which I presume is due to repeated malformed data being sent (on purpose), but I want to know if and how there is a way around that. I'm essentially looking to repeatedly send that SMB_COM_NEGOTIATE many times. Thanks in advance.
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((addr, port))
s.settimeout(2)
print '[*] Connected to "%s:%d".' % (addr, port)
s.sendall(SMB_COM_NEGOTIATE)
a = 0
while a != 50000:
print a
a = a + 1
s.sendall(SMB_COM_NEGOTIATE)
print '[*] Sent to "%s:%d".' % (addr, port)
EDIT (off Jame's suggestion) - Still jumps right to an error:
a = 0
try:
print "The value of 'a' is %r." % a
s.connect((addr, port))
print '[*] Connected to "%s:%d".' % (addr, port)
while a != 50000:
a = a + 1
s.sendall(SMB_COM_NEGOTIATE)
print '[*] Sent to "%s:%d".' % (addr, port)
print "The value 'a' is %r." % a
except:
print "[-] An error occured!!!"
s.close()
exit()
Output:
The value of 'a' is 0.
[*] Connected to "192.168.xxx.xxx:xxx".
[*] Sent to "192.168.xxx.xxx:xxx".
The value 'a' is 1.
[-] An error occured!!!
Also tried this (almost identical):
a = 0
print "The value of 'a' is %r." % a
s.connect((addr, port))
print '[*] Connected to "%s:%d".' % (addr, port)
def ok():
try:
while a != 50000:
a = a + 1
s.sendall(SMB_COM_NEGOTIATE)
print '[*] Sent to "%s:%d".' % (addr, port)
print "The value 'a' is %r." % a
except:
print "[-] An error occured!!!"
sleep(0)
s.close()
Which had an output (not even sending anything):
The value of 'a' is 0.
[*] Connected to "192.168.xxx.xxx:xxx".
[-] An error occurred!!!
Here is a code fragment to illustrate my comment.
import socket
def try_connect():
"""Tries to connect and send the SMB_COM_NEGOTIATE bytes.
Returns the socket object on success and None on failure.
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
try:
s.connect((addr, port))
s.sendall(SMB_COM_NEGOTIATE)
except socket.timeout as e:
# got a socket timeout
return None
except OSError as e:
# got some other socket error
return None
return s
def try_connect_n_times(n):
"""Try up to n times to connect"""
for attempt in range(n):
s = try_connect()
if s:
return s
return None
try_connect_n_times(5000)