SHARE
TWEET

tcp_udp_payload_sender

DeaD_EyE May 25th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2.  
  3. """
  4. This program connects to a ip and port via udp or tcp
  5. and sends the datafiles to the destination.
  6. """
  7.  
  8. import socket
  9. import sys
  10. from pathlib import Path
  11.  
  12.  
  13. class ArgumentError(Exception):
  14.     pass
  15.  
  16.  
  17. def usage():
  18.     exe = Path(sys.executable).name
  19.     prg = sys.argv[0]
  20.     descr = __doc__
  21.     cmd_usage = f'Usage: {exe} {prg} <ip> <port> <udp|tcp> <datafile> <...>'
  22.     print(descr)
  23.     print()
  24.     print(cmd_usage)
  25.  
  26.  
  27. def get_payloads(datafiles):
  28.     return [Path(file).read_bytes() for file in datafiles]
  29.  
  30.  
  31. def get_arguments():
  32.     if len(sys.argv) < 5:
  33.         usage()
  34.         raise ArgumentError('Wrong number of arguments')
  35.     ip, port, proto, *datafiles = sys.argv[1:]
  36.     if proto == 'udp':
  37.         sock_args = (socket.AF_INET, socket.SOCK_DGRAM)
  38.     elif proto == 'tcp':
  39.         sock_args = (socket.AF_INET, socket.SOCK_STREAM)
  40.     else:
  41.         usage()
  42.         raise ArgumentError(f'Wrong protocol: {proto}')
  43.     try:
  44.         addr = (ip, int(port))
  45.     except ValueError:
  46.         usage()
  47.         raise ArgumentError('Port must be a valid number')
  48.     return addr, proto, sock_args, datafiles
  49.  
  50.  
  51. def send_payloads_udp(connection, dest, payloads):
  52.     for n, payload in enumerate(payloads, start=1):
  53.         connection.sendto(payload, dest)
  54.         print(f'[*] Payload {n} sent')
  55.  
  56.  
  57. def send_payloads_tcp(connection, payloads):
  58.     for n, payload in enumerate(payloads, start=1):
  59.         connection.sendall(payload)
  60.         print(f'[*] Payload {n} sent')
  61.  
  62.  
  63. def main():
  64.     try:
  65.         addr, proto, sock_args, datafiles = get_arguments()
  66.     except ArgumentError as e:
  67.         print('Error:', e)
  68.         return 1
  69.     try:
  70.         payloads = get_payloads(datafiles)
  71.     except Exception as e:
  72.         print('Error:', e)
  73.         return 10
  74.     with socket.socket(*sock_args) as sock:
  75.         if proto == 'tcp':
  76.             try:
  77.                 sock.connect(addr)
  78.             except Exception as e:
  79.                 print('Error:', e)
  80.                 return 2
  81.             print("[*] Connected")
  82.             try:
  83.                 send_payloads_tcp(sock, payloads)
  84.             except Exception as e:
  85.                 print('Error:', e)
  86.                 return 20
  87.         elif proto == 'udp':
  88.             try:
  89.                 send_payloads_udp(sock, addr, payloads)
  90.             except Exception as e:
  91.                 print('Error:', e)
  92.                 return 10
  93.  
  94.  
  95. if __name__ == '__main__':
  96.     retval = main()
  97.     sys.exit(retval)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top