mirror of
https://github.com/usmannasir/cyberpanel.git
synced 2025-11-15 17:56:12 +01:00
bug fix: potential issue with sftp destination add https://github.com/usmannasir/cyberpanel/issues/1210;
;
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import paramiko
|
||||||
|
|
||||||
sys.path.append('/usr/local/CyberCP')
|
sys.path.append('/usr/local/CyberCP')
|
||||||
import django
|
import django
|
||||||
|
|
||||||
@@ -9,7 +11,7 @@ try:
|
|||||||
django.setup()
|
django.setup()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
import pysftp
|
||||||
from plogical.randomPassword import generate_pass
|
from plogical.randomPassword import generate_pass
|
||||||
import pexpect
|
import pexpect
|
||||||
from plogical import CyberCPLogFileWriter as logging
|
from plogical import CyberCPLogFileWriter as logging
|
||||||
@@ -1147,143 +1149,222 @@ class backupUtilities:
|
|||||||
except BaseException as msg:
|
except BaseException as msg:
|
||||||
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [initiateRestore]")
|
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [initiateRestore]")
|
||||||
|
|
||||||
|
# @staticmethod
|
||||||
|
# def sendKey(IPAddress, password, port='22', user='root'):
|
||||||
|
# try:
|
||||||
|
#
|
||||||
|
# expectation = []
|
||||||
|
# expectation.append("password:")
|
||||||
|
# expectation.append("Password:")
|
||||||
|
# expectation.append("Permission denied")
|
||||||
|
# expectation.append("100%")
|
||||||
|
#
|
||||||
|
# ## Temp changes
|
||||||
|
#
|
||||||
|
# command = 'chmod 600 %s' % ('/root/.ssh/cyberpanel.pub')
|
||||||
|
# ProcessUtilities.executioner(command)
|
||||||
|
#
|
||||||
|
# command = "scp -o StrictHostKeyChecking=no -P " + port + " /root/.ssh/cyberpanel.pub " + user + "@" + IPAddress + ":~/.ssh/authorized_keys"
|
||||||
|
# setupKeys = pexpect.spawn(command, timeout=3)
|
||||||
|
#
|
||||||
|
# if os.path.exists(ProcessUtilities.debugPath):
|
||||||
|
# logging.CyberCPLogFileWriter.writeToFile(command)
|
||||||
|
#
|
||||||
|
# index = setupKeys.expect(expectation)
|
||||||
|
#
|
||||||
|
# ## on first login attempt send password
|
||||||
|
#
|
||||||
|
# if index == 0:
|
||||||
|
# setupKeys.sendline(password)
|
||||||
|
# setupKeys.expect("100%")
|
||||||
|
# setupKeys.wait()
|
||||||
|
# elif index == 1:
|
||||||
|
# setupKeys.sendline(password)
|
||||||
|
# setupKeys.expect("100%")
|
||||||
|
# setupKeys.wait()
|
||||||
|
# elif index == 2:
|
||||||
|
# return [0, 'Please enable password authentication on your remote server.']
|
||||||
|
# elif index == 3:
|
||||||
|
# pass
|
||||||
|
# else:
|
||||||
|
# raise BaseException
|
||||||
|
#
|
||||||
|
# ## Temp changes
|
||||||
|
#
|
||||||
|
# command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
|
||||||
|
# ProcessUtilities.executioner(command)
|
||||||
|
#
|
||||||
|
# return [1, "None"]
|
||||||
|
#
|
||||||
|
# except pexpect.TIMEOUT as msg:
|
||||||
|
#
|
||||||
|
# command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
|
||||||
|
# ProcessUtilities.executioner(command)
|
||||||
|
#
|
||||||
|
# logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
|
||||||
|
# return [0, "TIMEOUT [sendKey]"]
|
||||||
|
# except pexpect.EOF as msg:
|
||||||
|
#
|
||||||
|
# command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
|
||||||
|
# ProcessUtilities.executioner(command)
|
||||||
|
#
|
||||||
|
# logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
|
||||||
|
# return [0, "EOF [sendKey]"]
|
||||||
|
# except BaseException as msg:
|
||||||
|
#
|
||||||
|
# command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
|
||||||
|
# ProcessUtilities.executioner(command)
|
||||||
|
#
|
||||||
|
# logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
|
||||||
|
# return [0, str(msg) + " [sendKey]"]
|
||||||
|
|
||||||
|
# @staticmethod
|
||||||
|
# def setupSSHKeys(IPAddress, password, port='22', user='root'):
|
||||||
|
# try:
|
||||||
|
# ## Checking for host verification
|
||||||
|
#
|
||||||
|
# backupUtilities.host_key_verification(IPAddress)
|
||||||
|
#
|
||||||
|
# if backupUtilities.checkIfHostIsUp(IPAddress) == 1:
|
||||||
|
# pass
|
||||||
|
# else:
|
||||||
|
# logging.CyberCPLogFileWriter.writeToFile("Host is Down.")
|
||||||
|
# # return [0,"Host is Down."]
|
||||||
|
#
|
||||||
|
# expectation = []
|
||||||
|
# expectation.append("password:")
|
||||||
|
# expectation.append("Password:")
|
||||||
|
# expectation.append("Permission denied")
|
||||||
|
# expectation.append("File exists")
|
||||||
|
#
|
||||||
|
# command = "ssh -o StrictHostKeyChecking=no -p " + port + ' ' + user + "@" + IPAddress + ' "mkdir ~/.ssh || rm -f ~/.ssh/temp && rm -f ~/.ssh/authorized_temp && cp ~/.ssh/authorized_keys ~/.ssh/temp || chmod 700 ~/.ssh || chmod g-w ~"'
|
||||||
|
# setupKeys = pexpect.spawn(command, timeout=3)
|
||||||
|
#
|
||||||
|
# if os.path.exists(ProcessUtilities.debugPath):
|
||||||
|
# logging.CyberCPLogFileWriter.writeToFile(command)
|
||||||
|
#
|
||||||
|
# index = setupKeys.expect(expectation)
|
||||||
|
#
|
||||||
|
# ## on first login attempt send password
|
||||||
|
#
|
||||||
|
# if index == 0:
|
||||||
|
# setupKeys.sendline(password)
|
||||||
|
# elif index == 1:
|
||||||
|
# setupKeys.sendline(password)
|
||||||
|
# elif index == 2:
|
||||||
|
# return [0, 'Please enable password authentication on your remote server.']
|
||||||
|
# elif index == 3:
|
||||||
|
# pass
|
||||||
|
# else:
|
||||||
|
# raise BaseException
|
||||||
|
#
|
||||||
|
# ## if it again give you password, than provided password is wrong
|
||||||
|
#
|
||||||
|
# expectation = []
|
||||||
|
# expectation.append("please try again.")
|
||||||
|
# expectation.append("Password:")
|
||||||
|
# expectation.append(pexpect.EOF)
|
||||||
|
#
|
||||||
|
# index = setupKeys.expect(expectation)
|
||||||
|
#
|
||||||
|
# if index == 0:
|
||||||
|
# return [0, "Wrong Password!"]
|
||||||
|
# elif index == 1:
|
||||||
|
# return [0, "Wrong Password!"]
|
||||||
|
# elif index == 2:
|
||||||
|
# setupKeys.wait()
|
||||||
|
#
|
||||||
|
# sendKey = backupUtilities.sendKey(IPAddress, password, port, user)
|
||||||
|
#
|
||||||
|
# if sendKey[0] == 1:
|
||||||
|
# return [1, "None"]
|
||||||
|
# else:
|
||||||
|
# return [0, sendKey[1]]
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# except pexpect.TIMEOUT as msg:
|
||||||
|
# return [0, str(msg) + " [TIMEOUT setupSSHKeys]"]
|
||||||
|
# except BaseException as msg:
|
||||||
|
# return [0, str(msg) + " [setupSSHKeys]"]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def sendKey(IPAddress, password, port='22', user='root'):
|
def sendKey(IPAddress, password, port='22', user='root'):
|
||||||
try:
|
try:
|
||||||
|
ssh = paramiko.SSHClient()
|
||||||
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||||
|
ssh.connect(IPAddress, port=int(port), username=user, password=password)
|
||||||
|
|
||||||
expectation = []
|
if os.path.exists('/root/.ssh/cyberpanel.pub'):
|
||||||
expectation.append("password:")
|
pass
|
||||||
expectation.append("Password:")
|
else:
|
||||||
expectation.append("Permission denied")
|
command = "ssh-keygen -f /root/.ssh/cyberpanel -t rsa -N ''"
|
||||||
expectation.append("100%")
|
ProcessUtilities.executioner(command, 'root', True)
|
||||||
|
|
||||||
## Temp changes
|
|
||||||
|
|
||||||
command = 'chmod 600 %s' % ('/root/.ssh/cyberpanel.pub')
|
command = 'chmod 600 %s' % ('/root/.ssh/cyberpanel.pub')
|
||||||
ProcessUtilities.executioner(command)
|
ProcessUtilities.executioner(command)
|
||||||
|
|
||||||
command = "scp -o StrictHostKeyChecking=no -P " + port + " /root/.ssh/cyberpanel.pub " + user + "@" + IPAddress + ":~/.ssh/authorized_keys"
|
sftp = ssh.open_sftp()
|
||||||
setupKeys = pexpect.spawn(command, timeout=3)
|
sftp.put('/root/.ssh/cyberpanel.pub', '.ssh/authorized_keys')
|
||||||
|
sftp.close()
|
||||||
|
|
||||||
if os.path.exists(ProcessUtilities.debugPath):
|
ssh.exec_command('chmod 600 .ssh/authorized_keys')
|
||||||
logging.CyberCPLogFileWriter.writeToFile(command)
|
|
||||||
|
|
||||||
index = setupKeys.expect(expectation)
|
ssh.close()
|
||||||
|
|
||||||
## on first login attempt send password
|
|
||||||
|
|
||||||
if index == 0:
|
|
||||||
setupKeys.sendline(password)
|
|
||||||
setupKeys.expect("100%")
|
|
||||||
setupKeys.wait()
|
|
||||||
elif index == 1:
|
|
||||||
setupKeys.sendline(password)
|
|
||||||
setupKeys.expect("100%")
|
|
||||||
setupKeys.wait()
|
|
||||||
elif index == 2:
|
|
||||||
return [0, 'Please enable password authentication on your remote server.']
|
|
||||||
elif index == 3:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise BaseException
|
|
||||||
|
|
||||||
## Temp changes
|
|
||||||
|
|
||||||
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
|
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
|
||||||
ProcessUtilities.executioner(command)
|
ProcessUtilities.executioner(command)
|
||||||
|
|
||||||
return [1, "None"]
|
return [1, "None"]
|
||||||
|
|
||||||
except pexpect.TIMEOUT as msg:
|
except paramiko.AuthenticationException:
|
||||||
|
return [0, 'Authentication failed. [sendKey]']
|
||||||
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
|
except paramiko.SSHException as e:
|
||||||
ProcessUtilities.executioner(command)
|
return [0, f'SSH error: {str(e)} [sendKey]']
|
||||||
|
except Exception as e:
|
||||||
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
|
return [0, f'General Error: {str(e)} [sendKey]']
|
||||||
return [0, "TIMEOUT [sendKey]"]
|
|
||||||
except pexpect.EOF as msg:
|
|
||||||
|
|
||||||
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
|
|
||||||
ProcessUtilities.executioner(command)
|
|
||||||
|
|
||||||
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
|
|
||||||
return [0, "EOF [sendKey]"]
|
|
||||||
except BaseException as msg:
|
|
||||||
|
|
||||||
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
|
|
||||||
ProcessUtilities.executioner(command)
|
|
||||||
|
|
||||||
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
|
|
||||||
return [0, str(msg) + " [sendKey]"]
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def setupSSHKeys(IPAddress, password, port='22', user='root'):
|
def setupSSHKeys(IPAddress, password, port='22', user='root'):
|
||||||
try:
|
try:
|
||||||
## Checking for host verification
|
ssh = paramiko.SSHClient()
|
||||||
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||||
|
ssh.connect(IPAddress, port=int(port), username=user, password=password)
|
||||||
|
|
||||||
backupUtilities.host_key_verification(IPAddress)
|
commands = [
|
||||||
|
"mkdir -p .ssh",
|
||||||
|
"rm -f .ssh/temp",
|
||||||
|
"rm -f .ssh/authorized_temp",
|
||||||
|
"cp .ssh/authorized_keys .ssh/temp",
|
||||||
|
"chmod 700 .ssh",
|
||||||
|
"chmod g-w ~",
|
||||||
|
]
|
||||||
|
|
||||||
if backupUtilities.checkIfHostIsUp(IPAddress) == 1:
|
for command in commands:
|
||||||
pass
|
try:
|
||||||
|
ssh.exec_command(command)
|
||||||
|
except BaseException as msg:
|
||||||
|
logging.CyberCPLogFileWriter.writeToFile(f'Error executing remote command {command}. Error {str(msg)}')
|
||||||
|
|
||||||
|
ssh.close()
|
||||||
|
|
||||||
|
sendKey = backupUtilities.sendKey(IPAddress, password, port, user)
|
||||||
|
|
||||||
|
if sendKey[0] == 1:
|
||||||
|
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
|
||||||
|
ProcessUtilities.executioner(command)
|
||||||
|
return [1, "None"]
|
||||||
else:
|
else:
|
||||||
logging.CyberCPLogFileWriter.writeToFile("Host is Down.")
|
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
|
||||||
# return [0,"Host is Down."]
|
ProcessUtilities.executioner(command)
|
||||||
|
return [0, sendKey[1]]
|
||||||
|
|
||||||
expectation = []
|
except paramiko.AuthenticationException:
|
||||||
expectation.append("password:")
|
return [0, 'Authentication failed. [setupSSHKeys]']
|
||||||
expectation.append("Password:")
|
except paramiko.SSHException as e:
|
||||||
expectation.append("Permission denied")
|
return [0, f'SSH error: {str(e)} [setupSSHKeys]']
|
||||||
expectation.append("File exists")
|
except Exception as e:
|
||||||
|
return [0, f'General Error: {str(e)} [setupSSHKeys]']
|
||||||
|
|
||||||
command = "ssh -o StrictHostKeyChecking=no -p " + port + ' ' + user + "@" + IPAddress + ' "mkdir ~/.ssh || rm -f ~/.ssh/temp && rm -f ~/.ssh/authorized_temp && cp ~/.ssh/authorized_keys ~/.ssh/temp || chmod 700 ~/.ssh || chmod g-w ~"'
|
|
||||||
setupKeys = pexpect.spawn(command, timeout=3)
|
|
||||||
|
|
||||||
if os.path.exists(ProcessUtilities.debugPath):
|
|
||||||
logging.CyberCPLogFileWriter.writeToFile(command)
|
|
||||||
|
|
||||||
index = setupKeys.expect(expectation)
|
|
||||||
|
|
||||||
## on first login attempt send password
|
|
||||||
|
|
||||||
if index == 0:
|
|
||||||
setupKeys.sendline(password)
|
|
||||||
elif index == 1:
|
|
||||||
setupKeys.sendline(password)
|
|
||||||
elif index == 2:
|
|
||||||
return [0, 'Please enable password authentication on your remote server.']
|
|
||||||
elif index == 3:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise BaseException
|
|
||||||
|
|
||||||
## if it again give you password, than provided password is wrong
|
|
||||||
|
|
||||||
expectation = []
|
|
||||||
expectation.append("please try again.")
|
|
||||||
expectation.append("Password:")
|
|
||||||
expectation.append(pexpect.EOF)
|
|
||||||
|
|
||||||
index = setupKeys.expect(expectation)
|
|
||||||
|
|
||||||
if index == 0:
|
|
||||||
return [0, "Wrong Password!"]
|
|
||||||
elif index == 1:
|
|
||||||
return [0, "Wrong Password!"]
|
|
||||||
elif index == 2:
|
|
||||||
setupKeys.wait()
|
|
||||||
|
|
||||||
sendKey = backupUtilities.sendKey(IPAddress, password, port, user)
|
|
||||||
|
|
||||||
if sendKey[0] == 1:
|
|
||||||
return [1, "None"]
|
|
||||||
else:
|
|
||||||
return [0, sendKey[1]]
|
|
||||||
|
|
||||||
|
|
||||||
except pexpect.TIMEOUT as msg:
|
|
||||||
return [0, str(msg) + " [TIMEOUT setupSSHKeys]"]
|
|
||||||
except BaseException as msg:
|
|
||||||
return [0, str(msg) + " [setupSSHKeys]"]
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def checkIfHostIsUp(IPAddress):
|
def checkIfHostIsUp(IPAddress):
|
||||||
|
|||||||
Reference in New Issue
Block a user