Compiling & Decompiling Python Scripts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back in today's little video i'm going to show you how you can easily compile and also decompile or uncompile python scripts and this is not the same as taking them and turning them into executable files on windows so not the same as pytoxa i have a video on that as well today we're just going to turn python scripts readable python script into compiled binary pyc files and the other way around so this is actually not an effective way to make the code unreadable to obfuscate it it's more like compiling and decompiling is easy so you can basically turn this binary file back into readable code it's not making your code in any way secret so what you need to do for that is quite simple you come up with a base a basic python script so for example x equals integer input enter the first number then we're copying that changing it to y changing that to second and all the script does is basically just printing x plus y very simple script and we can now run this like always in pycharm and you will see that i can enter 20 and 30 and i get 50. so what i can do now is i can open up my command line so cmd on windows or the terminal on linux and mac and i can navigate to the directory in this case it's uh this directory here so this path on my computer and here you can see that i have this main py file what i have to do now is i have to say python dash m and then pi underscore compile and for that you don't need to install anything this is part of the core python installation um and then i just pass main dot py so that's basically it and now you can see that we have this pi cache folder now by default i think in pycharm it's not displayed so the individual pyc files are not displayed you have to turn that on in the settings to see that here on the left but the file exists and all you need to do now for example in the command line here is to go into that directory and to say python main dot c python 3 9 pyc and you can see that i can do the same thing here now in order to turn this back now or first of all let me show you that you can open this and this is basically a binary file you can read a couple of things for example the strings here but you cannot really see you know you have a bunch of bytes that you cannot really read here um so if you want to to uncompile that you need to install a tool called uncompile actually i think it's called uncompile six so you need to go to your command line and you need to say pip install uncompiled like that py like python not like it's actually written um six pip install on compile six and if you now want to uncompile something you just type uncompile six and then in this case main.c python39 pyc and then uh this greater than uh symbol and then uh uncompiled dot py now in this case however at this point in time it's not gonna work because this tool does not support python 3.9 yet which does not mean that you can use this technique to obfuscate your code with python 3.9 it's just a matter of time sooner or later it will be possible or it is already possible but you just need to do it in a different way you can't always decompile it so it's not like you're making your code unreadable and there's no way to reverse this it's just that this tool does not support python 3.9 yet so if i run this you're going to see unsupported python version doesn't mean that python 3.9 uh is now a version where you can do that and you don't have to worry about anyone reading your code what you can do however is you can also do this with python 3.8 and i'm going to do this on the windows subsystem for linux now here because there i have python 3.8 you don't have to be on linux though you can do that on windows as well you just navigate to the neural directory and or to whatever uh directory you are working in and then you type python3 dash m pi underscore compile main dot p y same thing uh the difference here of course is that python three in this case means python three point eight point ten um come on uh and now i can go to the directory here pi cache and you can see here that i now have this c python three eight this was three nine this was the one on windows this is python three eight now so i can also just go ahead python 3 main.c python38pyc i can run the same thing here you can see it works and now actually if i go ahead here and i say uncompile 6 main c python 3 python38 so the version that is supported and now i'm going to call this decompiled dot py there you go you see it worked so now i have this decompiled py here and you can actually see that we have a bunch of comments here but at the end of the day this is the code uh actually also with the same with the same parameters with the same variable names um and actually this can be done with way more complicated script so for example let me just go to my prepared folder here um this here is one of the videos that i already made the paint clone in python so i can just copy paste this here there you go paste main two this is basically the neural nine paint clone i have a video on that you can check that out if you want to if i just run the python script you're going to see that this is the only file we need for that i can draw here i can save i can create increase brush size i can choose colors and all that so all this works and i can now compile this and you can see that i have very specific names paint gui and i have saved btn clear btn and so on and i can go ahead now and compile this here on linux with python 3.8 so basically just python3 dash m pi underscore compile main2.py then i go to the pi cache and you can see that i have this now here i'm going to run this on windows because otherwise i'm going to need the gui support and i don't have my windows server running at the moment um we need main main two what's that now python main two okay we have a problem yesterday it worked so let me just fix that i'm going to come back to you all right now i'm not sure why exactly this is the case but if i type python main to uh it says bad magic number but if i type main two directly it runs the compiled version so you can see it actually works and we can also go into oh i closed this actually we can also go into the subsystem and see that the generated um the generated file is basically binary and unreadable at least for us we can see a couple of strings here but basically a lot of bytes that we cannot really decipher as a human and what i can do now is i can just uncompile this so i can say uncompile six main to whatever and then pane clone decompiled dot py take some time there you go and now i have this paint clone decompile py with a couple of comments but you can see that i have the exact same code now maybe some formatting issues but we have the same names we have the same functions we have the same strings so it's not really about making your code unreadable or safe or secret or anything it's just compiling uh for whatever reason you want to compile your files but keep in mind that when you compile a python file like that you can also decompile it and again this is not the same as turning a python file into an executable file it's a whole different thing that you're doing i have a video on that as well if you want to know how to convert python files into executable windows files check out my video on that this is how you do basic python compilations and or compilation and how you also decompile or uncompile back into readable source code so that's it for today's video hope you enjoyed it i hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you so much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 48,905
Rating: undefined out of 5
Keywords: compile python, python compilation, python compiler, python decompile, compile, decompile, uncompile, compyile, decompyle, uncompyle, python decompiler, python pyc files, python byte code
Id: UFz5kNTRk3g
Channel Id: undefined
Length: 8min 53sec (533 seconds)
Published: Fri Feb 04 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.