👨‍💻
Hacking Notes
  • Hacking Notes
  • 💅One-Liners
  • ⚔️Offensive
    • Exploit Workflow
    • Recon
      • OSINT
      • DNS
        • Domain Discovery
      • Layer 2 Config and Analysis
      • Port Scanning and Discovery
      • Port Attacks
      • Link it all together
    • Payloads
      • MSFVenom
      • Reverse Shells
    • Websites
      • Enumeration
      • Injection/LFI
      • Session Management
      • Brute Forcing
      • JavaScript & XSS
      • SSRF
      • XXE
      • PHP
    • Password Attacks
      • Brute Forcing
      • Mimikatz
      • Password Cracking
      • Hash Extraction
      • Wordlist Generation
    • Databases
      • SQL
      • Mongodb
    • Microsoft Windows Exploits
      • Enumeration
      • Powershell
      • Cmd
      • Privilege Escalation
      • Active Directory
      • Bloodhound
    • Social Engineering
    • Netcat & Socat
    • File Transfers
    • Metasploit
      • Writing Modules
    • PS Empire
    • Priv Escalation
    • Post Exploitation
    • Pivoting
    • Certs and Secrets
    • NGROK
    • Misc.
  • 🛡️Defensive
    • Defensive Notes
    • Windows Forensics
      • Program Execution Artifacts
      • ASEP Locations
      • Event Logs
    • Linux Forensics
    • Network Forensics
      • tshark
      • Wireshark Filters
    • Memory Forensics
    • Stego
    • Malware Analysis
    • Volatility
  • 🌩️Cloud
    • Scope and Shared Responsibility
    • AWS CLI
    • Azure CLI
    • SaaS Attacks
    • PaaS
  • ⌨️Programming
    • Programming Notes
    • Examples and Quick Scripts
    • PowerShell
    • Pwn
      • Windows Pwn
    • Python
      • Basic Python
      • Modules
      • Working with Files
      • Networking
      • Attack Related
      • Scapy
        • Using Scapy
        • Reading PCAP
    • C
      • Code Examples
      • GDB
    • PHP
Powered by GitBook
On this page
  • Sockets
  • SSH Interaction
  • SFTP

Was this helpful?

  1. Programming
  2. Python

Networking

Collection of networking over sockets, using HTTP libraries, and networking

Sockets

Basic network communication over raw sockets

# Basic TCP connection to a server to send and receive data
import socket

clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('127.0.0.1', 9987))
clientsocket.send('hello'.encode())
data = clientsocket.recv(1024)
print(data)

SSH Interaction

Paramiko is a useful library which can be used to log into a host over SSH and execute commands as though it were an interactive session.

import paramiko

key = '/home/some/rsa/private/key'
hostname = '127.0.0.1'
user = 'm4lwhere'

k = paramiko.RSAKey.from_private_key_file(key)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=hostname, username=user, pkey=k)
stdin, stdout, stderr = client.exec_command('ls -l')
print(f'Received output:\n\n{stdout}')

# Don't forget to close out the object!
ssh.close()

SFTP

Paramiko can additionally support SFTP natively with a paramiko.SSHClient() object which is super cool.

import paramiko

key = '/home/some/rsa/private/key'
hostname = '127.0.0.1'
user = 'm4lwhere'

k = paramiko.RSAKey.from_private_key_file(key)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=hostname, username=user, pkey=k)

sftp = ssh.open_sftp()
srcFiles = sftp.listdir('/home/m4lwhere')

# Download each of the files in the srcFiles list
for i in srcFiles:
    download = f'/home/m4lwhere/{i}'
    sftp.get(download, '/opt/downloads')

# Upload a file from local machine to external one
sftp.put('/opt/upload.txt', '/home/m4lwhere/upload.txt')

ssh.close()
PreviousWorking with FilesNextAttack Related

Last updated 3 years ago

Was this helpful?

⌨️