Skip to content

Commit 78b7391

Browse files
committed
Creating 'utils.get_socket' and 'utils.get_address' for reusing these common functions. Using them in rdp_check
1 parent 155558c commit 78b7391

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

examples/rdp_check.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from struct import pack, unpack
2626

2727
from impacket.examples import logger
28-
from impacket.examples.utils import parse_target
28+
from impacket.examples.utils import parse_target, get_socket
2929
from impacket.structure import Structure
3030
from impacket.spnego import GSSAPI, ASN1_SEQUENCE, ASN1_OCTET_STRING, asn1decode, asn1encode
3131

@@ -383,17 +383,7 @@ def check_rdp(host, username, password, domain, hashes=None, ipv6=False):
383383
tpdu['Code'] = TDPU_CONNECTION_REQUEST
384384
tpkt['TPDU'] = tpdu.getData()
385385

386-
address = (host, 3389)
387-
if ipv6:
388-
s = socket.socket(socket.AF_INET6)
389-
# scope_id (after %) can be present or not - if not, default: 0
390-
host_ipv6_parts = host.split('%')
391-
scope_id = int(host_ipv6_parts[1]) if len(host_ipv6_parts) == 2 else 0
392-
address = address + (0, scope_id)
393-
else:
394-
s = socket.socket()
395-
396-
s.connect(address)
386+
s = get_socket(host, 3389, ipv6)
397387
s.sendall(tpkt.getData())
398388
pkt = s.recv(8192)
399389
tpkt.fromString(pkt)

impacket/examples/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,21 @@ def parse_identity(credentials, hashes=None, no_pass=False, aesKey=None, k=False
317317
lmhash = EMPTY_LM_HASH
318318

319319
return domain, username, password, lmhash, nthash, k
320+
321+
# ----------
322+
323+
def get_address(ip, port, ipv6=False):
324+
address = (ip, port)
325+
if ipv6:
326+
# scope_id (after %) can be present or not - if not, default: 0
327+
ip_parts = ip.split('%')
328+
scope_id = int(ip_parts[1]) if len(ip_parts) == 2 else 0
329+
address = address + (0, scope_id)
330+
return address
331+
332+
import socket
333+
def get_socket(ip, port, ipv6=False):
334+
s = socket.socket(socket.AF_INET6 if ipv6 else socket.AF_INET)
335+
address = get_address(ip, port, ipv6)
336+
s.connect(address)
337+
return s

0 commit comments

Comments
 (0)