Insert a simulated Ken Thompson virus into a program

3

A Ken Thompson virus is a virus that infects your compiler and other programs. Since it infects all your tools, you can not detect it without using tools from before the virus was introduced.

Your will create a prototype for inserting the Ken Thompson virus into code. The goal of this virus will be to allow you to (fake) destroy any computer running a program infected with it.

Inserting a Ken Thompson virus is defined as follows. Start with an input program P. Your program will output a modified program Q that works as follows:

  • If the input to Q is aNouIQ5xKvUfdZVzo0Xs, it will output BOOM (in a real Ken Thompson virus, this would actually do something bad, but since it this is a fake virus, outputing BOOM is done instead).
  • For any other input to Q, run program P with that input
    • If the output of P is valid source code in the language you are using, insert the Ken Thompson virus into it and then Q will output the resulting infected program.
    • Otherwise, the output of Q is just the output of P.

You will write a program that takes P's code as input and produces Q's code as output.

You can use either a currently (at the time of this post) existing programming language, or a subset of one, for this challenge. (The idea is that language you are using would be the target language of some self-compiling compiler, but this is not required.) Since you want to be as subtle as possible, the shortest code (in bytes) wins!

PyRulez

Posted 2017-06-04T22:09:48.197

Reputation: 6 547

Sandbox entry is here: https://codegolf.meta.stackexchange.com/a/12710/16842

– PyRulez – 2017-06-04T22:10:03.853

3

Voting to close as asking for malicous code is never a good idea. https://codegolf.meta.stackexchange.com/q/4829/15599

– Level River St – 2017-06-04T22:45:41.250

1I'm voting to close this question as off-topic because I agree with @LevelRiverSt, this is malicious. – NoOneIsHere – 2017-06-04T22:54:41.567

6@NoOneIsHere @LevelRiverSt This is clearly not malicious. Insert a simulated Ken Thompson virus into a program, The goal of this virus will be to allow you to (fake) destroy any computer. All the "virus" does is print BOOM. There is absolutely nothing malicious. – MD XF – 2017-06-05T03:22:15.923

@MDXF ok, VTRO-ing. I guess I didn't read the question well. – NoOneIsHere – 2017-06-05T05:03:42.623

The program P only ever has one input? – Jonathan Allan – 2017-06-05T06:33:08.930

6I don't understand why this is downvoted. Seems like a quine variant that may actually be interesting for once. – feersum – 2017-06-05T08:23:53.260

An answer with a compiler doesn't actually seem to be valid though, since a compiler's output is in a different language than the source code it gets as input. – feersum – 2017-06-05T08:25:19.543

Here i do not write virus – RosLuP – 2017-06-05T12:41:16.910

2@MDXF The OP's intention is not malicious, but in theory at least, code submitted here could be used to create a virus. The possibility of that happening is extremely remote, but if it did happen it would bring this site into serious disrepute. That's why I say: asking for malicious code is never a good idea. Per the question I linked, Martin, Peter and a majority of voters at the time tended to agree. – Level River St – 2017-06-05T20:27:55.730

@JonathanAllan yes – PyRulez – 2017-06-05T21:06:31.340

1@LevelRiverSt or they could just create a virus themselves. All code on this site is so obfuscated that it's easier to start from scratch. – PyRulez – 2017-06-05T21:08:40.133

@RosLuP it's a simulated virus, not a real one. – PyRulez – 2017-06-05T21:25:47.743

@LevelRiverSt if anything, the algorithm I posted is more dangerous than any code posted. – PyRulez – 2017-06-05T21:27:24.257

@Jonathan Allan, you could use a subset of Jelly – PyRulez – 2017-06-05T21:36:26.070

Must it be a program, or a function will work? Must it take the compiled in bytecode program? (I think) This is quite trivial in some programming language. – user202729 – 2017-06-06T14:54:02.633

@user202729 a function is fine. It must process source code in it's only language (so if you inserting the virus into assembly, you must publish your program in assembly). – PyRulez – 2017-06-06T19:24:29.873

@feersum this challenge "assumes" that you are writing the code in the target language of the compiler – PyRulez – 2017-06-06T19:25:38.643

1I'd argue the question is asking for such a minor component of a malicious task, that it's not malicious in itself. We're tasked with writing a separate pre-compiler that injects source code. The challenging parts. infecting a machine, injecting this pre-compiler into an existing compiled compiler, and circumventing OS-level security controls, far outweigh this task in complexity. – Hand-E-Food – 2017-06-09T00:42:48.337

How is the input P provided to the program and how should the output Q be delivered? – Hand-E-Food – 2017-06-09T00:45:13.880

Answers

2

For some other languages, determining If the output of P is valid source code in the language you are using is almost impossible, unless you use/rewrite a compiler. For some other languages you can eval a string and catch for error, if any. However, for Mathematica, it is just check for if the head of the result is a function - Mathematica doesn't allow output invalid function, like 1!-!&.

So,

Mathematica, 209 bytes

(obviously can be golfed more, for example by assign long names to global variables)

With[{p=#,c=With[{p=#,c=6},If[#==="aNouIQ5xKvUfdZVzo0Xs","BOOM",With[{r=p@#},If[Head@r===Head@p,(c/.3!->c)[r],r]]]&]&},If[#==="aNouIQ5xKvUfdZVzo0Xs","BOOM",With[{r=p@#},If[Head@r===Head@p,(c/.3!->c)[r],r]]]&]&

The function takes a function as input, and output a function according to the specification.

Note that the input must be a pure function, not something like f[x_] := .... So all Mathematica built-in functions fail the criteria.

Although Mathematica have #0 parameter, I explicitly disallow that in my answer because a quine is not allowed to read the code of itself.

(I thought that question is trivial until I read the third rule)

user202729

Posted 2017-06-04T22:09:48.197

Reputation: 14 620