Mailing List Archive

restore sources from PYC [Q]
Hi!

By mistake I am unable to find the PY files (source) from a project I was
working on. I only have the PYC files... Is there a way to recover the
sources from the PYC? I only need my sources, I don't give a d... for the
comments...

TIA

/B

Bruno Mattarollo <bruno@gaiasur.com.ar>
... proud to be a PSA member <http://www.python.org/psa>
restore sources from PYC [Q] [ In reply to ]
"Bruno Mattarollo" <brunomadv@ciudad.com.ar> writes:
> Hi!
>
> By mistake I am unable to find the PY files (source) from a project I was
> working on. I only have the PYC files... Is there a way to recover the
> sources from the PYC? I only need my sources, I don't give a d... for the
> comments...

Well.... The pyc files are marshalled code objects (after an eight
byte header), and there's the standard module dis which can
disassemble code objects, from which you could have a stab at
rewriting your code. You certainly wouldn't *want* to do it this way.

It would probably be within the bounds of possibility to write a
decompiler, though unless you've lost a vast heap of code this is
going to be a much bigger project than rewriting your code.

Anyway, if it helps, here's a function that'll pull the code out of a
.pyc file and disassemble that code:

def dis_pyc(pyc_name):
import marshal,dis
file=open(pyc_name,'rb')
file.read(8)
code=marshal.load(file)
did.dis(code)

(NB: This isn't much use because defined functions and classes are in
their own code objects stored as constants - they're still accessible,
in code.co_consts, but this code doesn't go looking for them)

> TIA
>
> /B
>
> Bruno Mattarollo <bruno@gaiasur.com.ar>
> ... proud to be a PSA member <http://www.python.org/psa>

Good luck!

Michael
restore sources from PYC [Q] [ In reply to ]
This is a multi-part message in MIME format.
--------------AAA55AF0D846E12491FB6BF8
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hi,

What happens if you do NOT want your pyc codes to be reverse engineered?
Can you prevent someone from doing this type of rev eng?
Some members in my IT committee always have a problem with this ability
and often used as excuse to kill any new tech adoption.

Michael Hudson wrote:
>
> "Bruno Mattarollo" <brunomadv@ciudad.com.ar> writes:
> > Hi!
> >
> > By mistake I am unable to find the PY files (source) from a project I was
> > working on. I only have the PYC files... Is there a way to recover the
> > sources from the PYC? I only need my sources, I don't give a d... for the
> > comments...
>
> Well.... The pyc files are marshalled code objects (after an eight
> byte header), and there's the standard module dis which can
> disassemble code objects, from which you could have a stab at
> rewriting your code. You certainly wouldn't *want* to do it this way.
>
> It would probably be within the bounds of possibility to write a
> decompiler, though unless you've lost a vast heap of code this is
> going to be a much bigger project than rewriting your code.
>
> Anyway, if it helps, here's a function that'll pull the code out of a
> .pyc file and disassemble that code:
>
> def dis_pyc(pyc_name):
> import marshal,dis
> file=open(pyc_name,'rb')
> file.read(8)
> code=marshal.load(file)
> did.dis(code)
>
> (NB: This isn't much use because defined functions and classes are in
> their own code objects stored as constants - they're still accessible,
> in code.co_consts, but this code doesn't go looking for them)
>
> > TIA
> >
> > /B
> >
> > Bruno Mattarollo <bruno@gaiasur.com.ar>
> > ... proud to be a PSA member <http://www.python.org/psa>
>
> Good luck!
>
> Michael

--
*****************************************************************************
S. Hoon Yoon (Quant) Merrill Lynch Equity Trading,
yelled@yahoo.com hoon@bigfoot.com(w)
"Miracle is always only few standard deviations away, but so is
catastrophe."
* Expressed opinions are often my own, but NOT my employer's.
"I feel like a fugitive from the law of averages." Mauldin
*****************************************************************************
--------------AAA55AF0D846E12491FB6BF8
Content-Type: text/x-vcard; charset=us-ascii; name="vcard.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Hoon Yoon
Content-Disposition: attachment; filename="vcard.vcf"

begin: vcard
fn: Hoon Yoon
n: ;Hoon Yoon
email;internet: hyoon@bigfoot.com
x-mozilla-cpt: ;0
x-mozilla-html: FALSE
version: 2.1
end: vcard


--------------AAA55AF0D846E12491FB6BF8--
restore sources from PYC [Q] [ In reply to ]
Hoon Yoon <hyoon@bigfoot.com> wrote:

> What happens if you do NOT want your pyc codes to be reverse engineered?
> Can you prevent someone from doing this type of rev eng?

You can obscure it, but never more than that... you can reverse out C
code too... at some point it must be executable, that's the point. Maybe
you won't get the exact code back, but you will get something close.

> Some members in my IT committee always have a problem with this ability
> and often used as excuse to kill any new tech adoption.

Then you better throw out all your compilres :-) Oh, and Java too :-)

Seriously, this is a non-event that people use to spread FUD, but it
exists in all languages. The simplicity with which it can be done
changes, but it's never more than a freshman college project.

Chris
--
| Christopher Petrilli ``Television is bubble-gum for
| petrilli@amber.org the mind.''-Frank Lloyd Wright
restore sources from PYC [Q] [ In reply to ]
>>>>> "CP" == Christopher Petrilli <petrilli@trump.amber.org> writes:

CP> Seriously, this is a non-event that people use to spread FUD,
CP> but it exists in all languages. The simplicity with which it
CP> can be done changes, but it's never more than a freshman
CP> college project.

Which is why almost every binary license I've ever stayed awake to
fully read includes text like "you may not decompile, disassemble,
reverse-engineer" blah, blah, blah! :-)
restore sources from PYC [Q] [ In reply to ]
Barry A. Warsaw <bwarsaw@cnri.reston.va.us> wrote:

>>>>>> "CP" == Christopher Petrilli <petrilli@trump.amber.org> writes:

> CP> Seriously, this is a non-event that people use to spread FUD,
> CP> but it exists in all languages. The simplicity with which it
> CP> can be done changes, but it's never more than a freshman
> CP> college project.

> Which is why almost every binary license I've ever stayed awake to
> fully read includes text like "you may not decompile, disassemble,
> reverse-engineer" blah, blah, blah! :-)

Get a life, Barry ;-)

Seriously, this is exactly the situation... in fact many companies put
some bizarre constructions in their code so they can prove in court that
someone "stole" the code...

"Judge, no sane person would use a bubble sort!" ;-)

Chris
--
| Christopher Petrilli ``Television is bubble-gum for
| petrilli@amber.org the mind.''-Frank Lloyd Wright
restore sources from PYC [Q] [ In reply to ]
>>>>> "CP" == Christopher Petrilli <petrilli@trump.amber.org> writes:

CP> Get a life, Barry ;-)

Been reading too much comp.emacs.xemacs lately!
restore sources from PYC [Q] [ In reply to ]
[Hoon Yoon]
> What happens if you do NOT want your pyc codes to be reverse
> engineered?

You would have to add a layer of encryption to the .pyc files, and
decryption to the interpreter. I'm sure the U.S. govt would be delighted to
help <wink>.

> Can you prevent someone from doing this type of rev eng?

Nope. You can strive to make it more difficult, but you can't stop it.

> Some members in my IT committee always have a problem with this
> ability and often used as excuse to kill any new tech adoption.

How do they feel about old tech? Like, say, C or C++? It's no great
achievement to reverse-engineer algorithms from a binary native machine-code
distribution either; in fact, there are a thousand people who can read Intel
machine code in their sleep for everyone who can reverse-engineer .pyc files
<wink>.

unless-you-can-hide-your-code-from-the-cpu-a-human-can-
watch-it-work-too-ly y'rs - tim
restore sources from PYC [Q] [ In reply to ]
[Christopher Petrilli]
> ... in fact many companies put some bizarre constructions in their code
> so they can prove in court that someone "stole" the code...

When I was in grade school, I noticed that a map of my neighborhood showed a
non-existent road going smack thru the middle of a large non-acknowledged
pond. Investigation revealed that map makers insert deliberate errors for
the same reason: to prove that someone else copied their work. That was
the day I decided to become a programmer <wink>.

all-of-the-intrigue-with-none-of-the-tedious-surveying-ly y'rs - tim
restore sources from PYC [Q] [ In reply to ]
This is a multi-part message in MIME format.
--------------75660EE4C08EDE6474DE0804
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Yep, just more defensive mech from the old school.

*****************************************************************************
S. Hoon Yoon (Quant) Merrill Lynch Equity Trading,
yelled@yahoo.com hoon@bigfoot.com(w)
"Miracle is always only few standard deviations away, but so is
catastrophe."
* Expressed opinions are often my own, but NOT my employer's.
"I feel like a fugitive from the law of averages." Mauldin
*****************************************************************************
--------------75660EE4C08EDE6474DE0804
Content-Type: text/x-vcard; charset=us-ascii; name="vcard.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Hoon Yoon
Content-Disposition: attachment; filename="vcard.vcf"

begin: vcard
fn: Hoon Yoon
n: ;Hoon Yoon
email;internet: hyoon@bigfoot.com
x-mozilla-cpt: ;0
x-mozilla-html: FALSE
version: 2.1
end: vcard


--------------75660EE4C08EDE6474DE0804--