# Code Protection in Standalone Binary during Deployment

**URL:** <https://swi-prolog.discourse.group/t/code-protection-in-standalone-binary-during-deployment/1127>\
**Category:** Help!\
**Created:** [August 28, 2019, 2:19pm UTC](https://swi-prolog.discourse.group/t/code-protection-in-standalone-binary-during-deployment/1127 "2019-08-28T14:19:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aminemarref](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@aminemarref](https://swi-prolog.discourse.group/u/aminemarref)\
**Post date:** [August 28, 2019, 2:19pm UTC](https://swi-prolog.discourse.group/t/code-protection-in-standalone-binary-during-deployment/1127/1 "2019-08-28T14:19:52Z")

</div>

Hello,

I am experimenting with the most suitable way for deploying my Prolog tool. So far, I want to host it somewhere in the cloud as a standalone executable with obfuscated code.

From the documentation ([Creating executables on Unix/Linux](https://www.swi-prolog.org/FAQ/UnixExe.html)), I used the following to create the executable.  
`$ swipl --stand_alone=true -o my_tool -g main -c my_tool.pl`

My understanding is that the knowledge base contained in `my_tool.pl` is attached to the `swipl` binary as some sort of saved state.

But I still do not know how to obfuscate the code prior to that.

From the documentation ([Protecting your code](https://www.swi-prolog.org/pldoc/man?section=protect-code)), the option `obfuscate(true)` is used with [qsave\_program/2](https://www.swi-prolog.org/pldoc/man?predicate=qsave_program/2) to make the code harder to reverse-engineer.

My question is: how do I create a saved stated for the program? Where do I set the obfuscation flag? and how do I instruct the binary-creation process to use a specific saved state (one with the code obfuscated)?

Thank you.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [August 28, 2019, 3:12pm UTC](https://swi-prolog.discourse.group/t/code-protection-in-standalone-binary-during-deployment/1127/3 "2019-08-28T15:12:29Z")

</div>

```prolog
41_> swipl -o lazy --stand_alone --obfuscate -c lazy.pl 
% Obfuscating predicates names (conservative) ...
% Obfuscated 13 predicate names

```

All qsave\_program/2 options are simply mapped to `--option[=value]`, where the default value for boolean options is `true`.

Note that the obfuscation is extremely conservative. In practice it mostly applies to predicates that are local to a module and whose name does not appear as an atom anywhere else in the program.

---

<div class="post-metadata">

**Author:** ![aminemarref](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@aminemarref](https://swi-prolog.discourse.group/u/aminemarref)\
**Post date:** [August 28, 2019, 3:22pm UTC](https://swi-prolog.discourse.group/t/code-protection-in-standalone-binary-during-deployment/1127/4 "2019-08-28T15:22:06Z")

</div>

Thank you very much,

It managed to obfuscate 3 predicates. That’s really conservative.

Saying that obfuscation mostly applies to predicates that are local to a module, what is a “module” in this context? A Prolog file? In my case, I have a main file that loads tens of other files. Does this mean that obfuscation only processes predicates in the main file?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [August 29, 2019, 7:29am UTC](https://swi-prolog.discourse.group/t/code-protection-in-standalone-binary-during-deployment/1127/5 "2019-08-29T07:29:14Z")

</div>

It obfuscates predicates where the sole usage of an atom is to name this one and only predicate. That is really conservative, but if the atom is used elsewhere anyone may use call/N to call the predicate. It was a short exercise, getting the infrastructure for obfuscating in place. Proper obfuscation should do much more code analysis to find a much larger approximation of what can be obfuscated. It has not my priority though. I’d expect someone will sooner or later do it or pay someone to do it.

---

<div class="post-metadata">

**Author:** ![aminemarref](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@aminemarref](https://swi-prolog.discourse.group/u/aminemarref)\
**Post date:** [August 29, 2019, 7:42am UTC](https://swi-prolog.discourse.group/t/code-protection-in-standalone-binary-during-deployment/1127/6 "2019-08-29T07:42:30Z")

</div>

Thank you 🙂
