This series of tutorials is aimed to share the notes taken while I was learning python for cybersecurity with the book - Black Hat Python.
這系列教學文章為學習筆記+延伸資源,旨在分享學習書籍 Black Hat Python時所思所學,也希望能幫助想了解Python和資安的大大們入門。
This tutorial has also been written in English in Medium.
Let's get started! 開始吧!
# netcat.py
...
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='BHP Net Tool',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent('''Example:
netcat.py -t 192.16.1.108 -p 5555 -l -c #command shell
netcat.py -t 192.16.1.108 -p 5555 -l -u=mytest.text #upload file
netcat.py -t 192.16.1.108 -p 5555 -l -e=\"cat /etc/passwd\" #execute command
echo 'ABC' | ./netcat.py -t 192.168.1.108 -p 135 #echo text to server port 135
netcat.py -t 192.168.1.108 -p 5555 #connect to server
'''))
parser.add_argument('-c', '--command', action='store_true', help='command shell')
parser.add_argument('-e', '--execute', help='execute specified command')
parser.add_argument('-l', '--listen', action='store_true', help='listen')
parser.add_argument('-p', '--port', type=int, default=5555, help='specified port')
parser.add_argument('-t', '--target', default='192.168.1.203', help='specified IP')
parser.add_argument('-u', '--upload', help='upload file')
args = parser.parse_args()
if args.listen:
buffer = ''
else:
buffer = sys.stdin.read()
nc = NetCat(args, buffer.encode())
nc.run()
模組argparse讓我們更容易寫出對使用者友善的命令列介面(command-line interfaces)
epilog=textwrap.dedent('''Example:
netcat.py -t 192.16.1.108 -p 5555 -l -c #command shell
netcat.py -t 192.16.1.108 -p 5555 -l -u=mytest.text #upload file
netcat.py -t 192.16.1.108 -p 5555 -l -e=\"cat /etc/passwd\" #execute command
echo 'ABC' | ./netcat.py -t 192.168.1.108 -p 135 #echo text to server port 135
netcat.py -t 192.168.1.108 -p 5555 #connect to server
'''))
parser.add_argument('-c', '--command', action='store_true', help='command shell')
parser.add_argument('-e', '--execute', help='execute specified command')
parser.add_argument('-l', '--listen', action='store_true', help='listen')
parser.add_argument('-p', '--port', type=int, default=5555, help='specified port')
parser.add_argument('-t', '--target', default='192.168.1.203', help='specified IP')
parser.add_argument('-u', '--upload', help='upload file')
引數-c, -e & -u 指(imply) -l, 因為這些能應用在聆聽端listener side
-p & -t, 指傳送端(sender side),可用來代表目標聆聽者(target listener)
if args.listen:
buffer = ''
else:
buffer = sys.stdin.read()
Reference參考資料
推薦影片
絕讚! Youtube 教學影片 | Elevate Cyber
原始碼
Github - Python For Cybersecurity | Monles