Professional Documents
Culture Documents
com
Marc Bilodeau
I love Perl. It’s my rst real language as a developer, and I’ve done very well with it over
the past 19 years writing code much of those days. However, not all code can be open
source and freely available. That’s why compiling perl scripts into binary les is a great
way to share software without sharing the source code.
However, running perl scripts requires the interpreter and appropriate modules on the
system. Thankfully, most Linux distributions come with Perl. This certainly saves time
setting up the environment. Then, users can run programs by sharing perl scripts and
modules.
Yet, the source code for those scripts is visible to anyone. Developers sometimes don’t
want to share how they write an application. The developer can decide to release the
code as open source, or compile the source code to protect their intellectual property.
Open source software is wonderful, but developers do like to eat. Unfortunately, free
code doesn’t always exchange well for food and essential services. Although, there are
certainly real world exceptions through a service model.
Another reason to compile source code is portability. It simpli es the requirements for
the end user by providing a binary executable le instead of copying les to a system
with the interpreter.
Compiling allows developers to turn their source code into binary les. Unfortunately, it
is possible to decompile executables and look at the original source. Thankfully,
compiling does add a layer of defense. It is possible to further protect the source code
depending on the features of the compiler.
https://www.marcbilodeau.com/compiling-perl/ 1/14
1/15/2020 A Beginner's Guide to Compiling Perl Scripts - marcbilodeau.com
In Perl, the le size for compiled binaries is much larger than the script’s le size. That’s
MarctheBilodeau
because scripts, any dependencies, and a copy of the perl interpreter are in the
binary.
There are several options available for compiling perl scripts. However, many of them
are abandoned projects or discontinued products. Regardless, it demonstrates
historical interest to share perl scripts in binary form.
ActiveState_PDK Commercial Available for Windows and Linux. This compiler requires ActiveState Perl and the Perl Dev Kit.
However, in 2017 ActiveState will no longer develop its Perl Development Kit. Licenses range
from $145 to $395 USD.
PAR Free Perl Archive Toolkit. Well maintained and available through CPAN.
Perl2Exe Commercial Available for Windows, Linux, Solaris, AIX, and HP-UX. Licenses range from $49 to $5000 USD.
Actively maintained.
PerlBin Free Like App::Packer and PAR with similar features and options. However, PerlBin has not been
updated in many years.
perlcc Free Included in Perl 5.10 and older. In newer versions, the underlying modules are no longer
packaged with the main distribution of Perl. Perlcc is no longer maintained.
Currently, I use ActiveState’s PDK on the Microsoft Windows and Linux platforms.
Unfortunately, in 2017 ActiveState announced that it will no longer develop its Perl
Development Kit. It works very well, and I would continue to use it if it wasn’t
discontinued. Alternatively, Perl2Exe is also an excellent compiler for those whom
require a commercial product or technical support.
On the other hand, PAR is free and actively maintained. The documentation can be
overwhelming when digging in for the rst time. Also, support is restricted to forums
and user groups. Regardless, PAR has all the necessary features, the right price tag, a
community, and it’s well maintained. Now, let’s take a deeper dive into PAR.
After months of development and weeks of QA testing, The Best Perl Program in the
World is ready for release. However, this code is so brilliant and the environment so
complex to deploy, that the program should be compiled before releasing it to the
masses.
https://www.marcbilodeau.com/compiling-perl/ 2/14
1/15/2020 A Beginner's Guide to Compiling Perl Scripts - marcbilodeau.com
Marc Bilodeau
The Best Perl Program in the World is ready for release! Shh… Don’t tell anyone how it works.
Before the world can bene t from this life changing experience, the perl script must be
compiled within a Build Environment.
A Build Environment contains the essential perl scripts, libraries, and perl modules
necessary when compiling the binaries. Secondly, there will be a di erent Build
Environment for each operating system and processor architecture. For example, a
Microsoft Windows Build Environment is necessary for compiling Windows
executables. However, those binaries will not work on a Linux system. Hence, a Linux
Build Environment is necessary to compile Linux binaries. Yet, those x64 binaries will
not work on ARM devices like a Raspberry Pi with the same Linux OS. Therefore, a
Build Environment is necessary for compiling those perl scripts.
https://www.marcbilodeau.com/compiling-perl/ 3/14
1/15/2020 A Beginner's Guide to Compiling Perl Scripts - marcbilodeau.com
Marc Bilodeau
C:\Users\dev>cpanm PAR
PAR is up to date. (1.015)
C:\User\dev>cpanm PAR::Packer
--> Working on PAR::Packer
Fetching http://www.cpan.org/authors/id/R/RS/RSCHUPP/PAR-Packer-1.043.tar.gz ... OK
Configuring PAR-Packer-1.043 ... OK
==> Found dependencies: Module::ScanDeps, Getopt::ArgvFile
--> Working on Module::ScanDeps
Fetching http://www.cpan.org/authors/id/R/RS/RSCHUPP/Module-ScanDeps-1.24.tar.gz ... OK
Configuring Module-ScanDeps-1.24 ... OK
Building and testing Module-ScanDeps-1.24 ... OK
Successfully installed Module-ScanDeps-1.24
--> Working on Getopt::ArgvFile
Fetching http://www.cpan.org/authors/id/J/JS/JSTENZEL/Getopt-ArgvFile-1.11.tar.gz ... OK
Configuring Getopt-ArgvFile-1.11 ... OK
Building and testing Getopt-ArgvFile-1.11 ... OK
Successfully installed Getopt-ArgvFile-1.11
Building and testing PAR-Packer-1.043 ... OK
Successfully installed PAR-Packer-1.043
3 distributions installed
C:\User\dev>cpanm PAR::Packer
PAR::Packer is up to date. (1.043)
C:\User\dev>
At the time of this writing, Ubuntu 18.04 just released as the latest (LTS) Long Term
Supported version. Thankfully, both 16.04 and 18.04 use the same steps to install the
necessary software packages. Between the two version, Ubuntu 16.04 has an older
version of perl (5.22.1) and Ubuntu 18.04 has 5.26.1. Regardless, executing sudo apt
Like Ubuntu, di erent versions of macOS have di erent versions of Perl pre-installed.
Unfortunately, Perl 5.18.2 is pre-installed with macOS 10.13. This version of perl is
currently end-of-life. Although, xes for urgent issues like severe security problems
https://www.marcbilodeau.com/compiling-perl/ 4/14
1/15/2020 A Beginner's Guide to Compiling Perl Scripts - marcbilodeau.com
may still be released. Regardless, all packages for compiling perl scripts are already installed with the
Marc Bilodeau
Nevertheless, a developer can upgrade to 5.26.1 if needed.
operating system.
Neither this program nor the associated "parl" program impose any
licensing restrictions on files generated by their execution, in
accordance with the 8th article of the Artistic License:
Therefore, you are absolutely free to place any license on the resulting
executable, as long as the packed 3rd-party libraries are also available
under the Artistic License.
MacBookAir:~ marc$
As shown above, the steps to con gure Build Environments are very similar. This
means they could work with a little tinkering on other operating systems. Personally,
I’m experimenting with Android since my Google Pixel XL is my PC. Also, Docker is
showing promise as a portable Build Environment across di erent operating systems
including binaries that can run on a Raspberry Pi.
With the Build Environments ready, a developer can now build binaries.
The pp command from the PAR Packager creates standalone executables from perl
scripts. Remember The Best Perl Program in the World ? The following demonstrates
compiling perl on Microsoft Windows 10, Ubuntu 16.04, and macOS 10.13 using the pp
command.
https://www.marcbilodeau.com/compiling-perl/ 5/14
1/15/2020 A Beginner's Guide to Compiling Perl Scripts - marcbilodeau.com
Marc Bilodeau
https://www.marcbilodeau.com/compiling-perl/ 6/14
1/15/2020 A Beginner's Guide to Compiling Perl Scripts - marcbilodeau.com
The command line arguments are the same across the three di erent platforms. Most
Marc
likely Bilodeau
the arguments will be similar on any system that can install Perl and PAR Packer.
However, there may be di erences depending on the operating system, the program’s
command line options, and processor architecture.
The -M option adds modules and their dependencies. Furthermore, the more complex
an application, the more modules are likely in use. For this reason, there are a few
useful shortcuts to include multiple modules without having to list them separately.
-M Foo::** adds every module in the namespace except Foo. It will include Foo::Bar
and Foo:Bar::More up to any depth except for Foo itself.
-M Foo::* adds every module in the rst level. This include Foo::Bar and Foo::It but
not Foo::Bar::More nor Foo.
-M Foo:: adds every module from Foo to any depth. This includes Foo, Foo::Bar,
Foo::It, and Foo::Bar::More.
The -x option may not be necessary in some cases. However, it may nd additional
run-time dependencies which can’t be detected by static analysis. If the perl script
uses arguments, use the xargs option to execute di erent code paths since detecting
modules depends on which part of the code path runs. For more information, see the
pp documentation.
The -o option is the name of the output binary le. It can be any name, and it doesn’t
have to match the input le.
The last parameter, bestProgram.pl, is the input le. It is where pp starts the compiling
process.
Although the source is nicely compiled into a binary, it isn’t secure. Someone can still
easily extract the source code from the executable les. That’s because the binary is a
well-organized zip le right now.
https://www.marcbilodeau.com/compiling-perl/ 7/14
1/15/2020 A Beginner's Guide to Compiling Perl Scripts - marcbilodeau.com
Marc Bilodeau
dev@virtualbox:~/Desktop/decompile$ ll
total 3436
-rwxr-xr-x 1 dev dev 3506937 Apr 14 17:52 bestProgram*
dev@virtualbox:~/Desktop/decompile$ ll
total 3452
-rwxr-xr-x 1 dev dev 3506937 Apr 14 17:52 bestProgram*
drwxr-xr-x 14 dev dev 4096 Apr 8 01:37 lib/
-rw-r--r-- 1 dev dev 1282 Apr 8 01:37 MANIFEST
-rw-r--r-- 1 dev dev 219 Apr 8 01:37 META.yml
drwxr-xr-x 2 dev dev 4096 Apr 8 01:37 script/
dev@virtualbox:~/Desktop/decompile/script$ ll script/
total 16
-rw-r--r-- 1 dev dev 138 Apr 8 01:37 bestProgram.pl
-rw-r--r-- 1 dev dev 698 Apr 8 01:37 main.pl
use strict;
use Data::Dumper;
my %test =
(
first => 1,
second => 2,
third => 3
);
dev@virtualbox:~/Desktop/decompile/script$
Oh no! The source code for The Best Perl Program in the World is exposed! However,
there are ways to protect the source code to a degree. Unfortunately, there is no
bulletproof way to 100% protect source code. In fact, it’s not possible with any
language. The best protection other than providing a comprehensive End User License
Agreement, is to apply lters to the compile process.
PAR has the ability to apply lters to perl scripts and modules. However, the most
common lter to hide source code from the casual eye is Bleach.
There are two additional command parameters relating to the Bleach lter. First, -f Bleach
applies a lter to the input le, bestProgram.pl. Secondly, the -F Bleach option applies the
lter to the perl modules. PAR provides exibility to perform the lter on one or the
other or both.
https://www.marcbilodeau.com/compiling-perl/ 8/14
1/15/2020 A Beginner's Guide to Compiling Perl Scripts - marcbilodeau.com
One caveat when compiling perl binaries is that it will increase the overall executable
Marc
size. In someBilodeau
cases, it’s quite a bit. It will vary depending on the operating system and
the number of modules passing through the lter during compile time. Below is an
example of the size di erences on Microsoft Windows 10.
Now when attempting to unzip the binary le after compiling perl to examine the
source code, it looks hideous and unreadable. The same is true for the modules.
Directory of C:\Users\dev\decompile\bestProgram\script
C:\Users\dev\decompile\bestProgram\script>more bestProgram.pl
$_=<<'';y;\r\n;;d;y; \t;01;;$_=pack'b*',$_;$_=eval;$@&&die$@;$_
This is certainly a step in the right direction. Yet, this isn’t the only method when
compiling perl scripts. There is another lter that encrypts the source code.
The compiler can encrypt les using the Crypto lter from
the PAR::Filter::Crypto module. Like Bleach, the lter can take care of both the perl
scripts and perl modules.
However, there are some important caveats concerning compiling perl scripts. Most
noteworthy is both Bleach and Crypto lters will protect the source code from the causal
curious user. Unfortunately, someone with more skills in hacking and debugging could
obtain a copy of the source code.
Regardless, there are additional modules to install in the Build Environment. Although,
the version of Perl does matter. Unfortunately, the Filter::Crypto module does not
successfully install on all versions of Perl.
Microsoft Windows
The only requirement when compiling perl using the Crypto lter is to install
the Filter::Crypto module.
cpan Filter::Crypto
However, the version of Strawberry Perl matters. At the time of this writing, the latest
version (5.26.2.1) fails to install the Filter::Crypto module. Although, Strawberry Perl
5.24.4.1 successfully installs it. If 5.26.2.1 is absolutely necessary, then the Bleach lter is
https://www.marcbilodeau.com/compiling-perl/ 9/14
1/15/2020 A Beginner's Guide to Compiling Perl Scripts - marcbilodeau.com
By default, essential libraries and header les aren’t available for the Filter::Crypto
module. Therefore, the libssl-dev package is necessary to install rst. The libssl-dev
package is part of the OpenSSL project’s implementation of the SSL and TLS
cryptographic protocols.
Ubuntu 18.04
At the time of this writing, the version of Perl that is included with Ubuntu 18.04 (5.26.1)
fails to install the Filter::Crypto module. Fortunately, a compiled encrypted binary from
Ubuntu 16.04 will work on an Ubuntu 18.04 system. If Ubuntu 18.04 is absolutely
necessary, either downgrade to a version of Perl that can successfully install the
Filter::Crypto module or use the Bleach lter.
macOS 10.13
Although Perl 5.18.2 is available with this version of macOS, it can install the
Filter::Crypto module. Furthermore, it is more manual and does involve answering
some questions. During the process of installing the module, the location of OpenSSL
is di erent than the default setting. However, the remaining questions use the defaults.
https://www.marcbilodeau.com/compiling-perl/ 10/14
1/15/2020 A Beginner's Guide to Compiling Perl Scripts - marcbilodeau.com
Marc Bilodeau
MacBookAir:~ marc$ sudo cpan Filter::Crypto
CPAN: Storable loaded ok (v2.41)
Reading '/Users/marc/.cpan/Metadata'
Database was generated on Sat, 05 May 2018 11:54:10 GMT
Running install for module 'Filter::Crypto'
Running make for S/SH/SHAY/Filter-Crypto-2.07.tar.gz
CPAN: Digest::SHA loaded ok (v5.84_01)
CPAN: Compress::Zlib loaded ok (v2.06)
Checksum for /Users/marc/.cpan/sources/authors/id/S/SH/SHAY/Filter-Crypto-2.07.tar.gz ok
CPAN: File::Temp loaded ok (v0.23)
CPAN: Parse::CPAN::Meta loaded ok (v1.4404)
CPAN: CPAN::Meta loaded ok (v2.133380)
CPAN: Module::CoreList loaded ok (v3.03)
You can either specify a password from which the key to be used for
encryption/decryption will be derived using a PKCS#5 key derivation
algorithm, or you can directly specify the key to use.
You can also have a password or key randomly generated for you.
Build options:
[1] Build both components
[2] Build CryptFile component only
https://www.marcbilodeau.com/compiling-perl/ 11/14
1/15/2020 A Beginner's Guide to Compiling Perl Scripts - marcbilodeau.com
Marc Bilodeau
Which component(s) do you want to build? [1] 1
The pp command uses the Crypto lter like the Bleach lter. One other di erence is the
Filter::Crypto::Decrypt module must be packaged with the binary. Otherwise, the binary
won’t know how to decrypt the les.
The le size of the binary is comparable to Bleach. However, both are still larger than the
unprotected binary. Below is an example of the le sizes from an Ubuntu 16.04 Build
Environment.
What would the casual curious user see when attempting to peel back the layers?
https://www.marcbilodeau.com/compiling-perl/ 12/14
1/15/2020 A Beginner's Guide to Compiling Perl Scripts - marcbilodeau.com
Marc Bilodeau
dev@virtualbox:~/Desktop/decompile$ unzip bestProgram_Encrypted.exe
Archive: bestProgram_Encrypted
creating: lib/
creating: script/
inflating: MANIFEST
... output truncated on purpose ...
inflating: script/bestProgram.pl
inflating: script/main.pl
dev@virtualbox:~/Desktop/decompile$ ll
total 3936
drwxrwxr-x 4 dev dev 4096 May 5 14:52 ./
drwxr-xr-x 3 dev dev 4096 May 5 14:52 ../
drwxr-xr-x 15 dev dev 4096 May 5 14:42 lib/
-rw-r--r-- 1 dev dev 1353 May 5 14:42 MANIFEST
-rw-r--r-- 1 dev dev 233 May 5 14:42 META.yml
drwxr-xr-x 2 dev dev 4096 May 5 14:42 script/
use Filter::Crypto::Decrypt;
e6ad69e3dd1e9901ccf6ba701ff66dfb09ba6b2f6c3b872e33e1929e56298f9861777c6a21255012
c658fa873214cd41071f6915ef594ee5447ae02afc1e2d726333fb855148920518e827fbb0990053
73eddabe4e608e15fcc54008c659f218ac32d56ac8d05a78cfd446ae05cf6c19f7e3c1b30fab747f
c0b0017243f764b3d3db0ef081bbf245478bd5a6af4c361b22e488edf203d91d4018d930fe4d2c1b
8d4d103810eb0603
dev@virtualbox:~/Desktop/decompile$ ../bestProgram_Encrypted.exe
$VAR1 = {
'second' => 2,
'first' => 1,
'third' => 3
};
dev@virtualbox:~/Desktop$
Distributing Binaries
Finally, The Best Perl Program in the World is ready to release. Not so fast!
There is one very important step before distributing binaries to the masses. In fact, it’s
so important it should happen before any code is written using a perl module, library
le, or SDK. If not, it could lead to legal issues. Yikes!
Usually, the license details are part of the package either in a LICENSE le, on a
website, or in the documentation. However, there may be no license terms.
Unfortunately, that doesn’t mean it’s ok to use it. In this case, it’s best to contact the
author to either ask for permission or to clarify any licensing requirements.
Generally, it comes down to the license and how the application uses it. There are a lot
of open source licenses and each has di erent rules. Also, many libraries and SDK
have their own terms and conditions. Furthermore, some may require purchasing a
license to distribute. Unfortunately, licensing is a complex issue. Therefore, it may be
necessary to speak with a lawyer.
For anyone that develops applications today, it’s likely that 3rd party libraries and
modules are involved. Also, each of them requires careful examination of their use
cases within an application. This can be a daunting task. Thankfully, there are
https://www.marcbilodeau.com/compiling-perl/ 13/14
1/15/2020 A Beginner's Guide to Compiling Perl Scripts - marcbilodeau.com
companies out there that specialize in source code scanning to identify libraries, SDKs,
Marc
and Bilodeau
open source software licenses. However, it can be pricey depending on the size
and scale of the application.
Conclusion
Interpreted Languages like Perl require the interpreter and other modules on the
system to run the program. Thankfully, there are options available for compiling perl
scripts into binaries.
PAR is an excellent cross-platform package that can accomplish this goal. Additionally,
PAR Filters make it easy to obscure source code and modules. However, it is important
to comply with the licenses and terms of use for any third party component before
releasing any software.
Share this:
Related
Termux, the best linux terminal for Learning a Language All By What Co ee has taught me about
Android Yourself Moderation
https://www.marcbilodeau.com/compiling-perl/ 14/14