Code Injection works in C# too!

15

4

Given the following C# program outputting False, inject a 'malicious' line of code such that the program outputs True.

class Program
{
    static void Main()
    {
        System.Console.Write("False");
        ;
    }
}

Your answer should consist of a string that replaces the second semicolon, and causes the program to output True, the whole True and nothing but True (not even a newline). It must do this if stdout is printed to the console, and if stdout is redirected to a file.

The shortest answer wins.

Kendall Frey

Posted 2014-01-03T14:04:36.190

Reputation: 2 384

What's your objective winning criterion? – J B – 2014-01-03T14:14:21.787

@JB Do puzzles need a winning criterion? Will edit. – Kendall Frey – 2014-01-03T14:15:13.773

I'm mostly reacting to the code-golf remark, but the point stands: which will be the accepted answer? – J B – 2014-01-03T14:16:16.580

@JB Shortest answer is best answer. – Kendall Frey – 2014-01-03T14:16:57.437

Well it is code-golf, then, isn't it? – J B – 2014-01-03T14:18:23.903

@JB That's kind of a secondary goal, but I suppose I should add the tag. – Kendall Frey – 2014-01-03T14:19:16.240

I've added an additional criterion and changed it to Write instead of WriteLine – Kendall Frey – 2014-01-03T15:09:51.823

Answers

17

52 characters

}static Program(){System.Console.Write(0<1);for(;;);

so the whole thing becomes:

class Program
{
    static void Main()
    {
        System.Console.Write( "False" );
    }
    static Program()
    {
        System.Console.Write( 0 < 1 ); 
        for ( ; ; ) ;
    }
}

Danko Durbić

Posted 2014-01-03T14:04:36.190

Reputation: 10 241

Sorry for editing the question in the middle of your answer. Newlines are no longer allowed, you'll have to use Write. – Kendall Frey – 2014-01-03T15:16:11.377

Ok, I've changed WriteLine to Write – Danko Durbić – 2014-01-03T15:18:56.227

1You could probably save some bytes by replacing your exit with for(;;);, nothing I've seen requires that the program terminates...? – Joachim Isaksson – 2014-01-03T16:19:40.517

@JoachimIsaksson Yes, that meets the requirements as specified. – Kendall Frey – 2014-01-03T16:28:50.233

4}static Program(){System.Console.Write(0<1);for(;;); could always become }static Program(){for(System.Console.Write(0<1);;); – NPSF3000 – 2014-02-18T05:33:53.287

7

C#, 51 characters

Console.Write("\b\b\b\b\b");Console.Write("True ");

Will only work on standard output

RamonBoza

Posted 2014-01-03T14:04:36.190

Reputation: 171

First thing I tried, unfortunately it won't work because of the initial WriteLine. – Vereos – 2014-01-03T15:09:37.453

3Now it is Write :/ I don't get how we would manage to solve this if it keeps changing... – Vereos – 2014-01-03T15:12:05.213

@Vereos I wanted to remove the ambiguity of 'newline or no newline'. – Kendall Frey – 2014-01-03T15:13:03.913

3You don't need 2 instances of Console.Write, and you could use \r instead of 5 \bs. Console.Write("\rTrue "); – Brian S – 2014-01-03T21:10:01.203

I don't think it meets the criteria. It actually outputs False first, which is then overwtitten by True later. – microbian – 2014-02-04T07:44:22.490

1

You can generalize RamonBoza's answer to still work if stdout is redirected to a file.

Assuming the program runs under Mono on Linux, with the assembly Mono.Posix.dll loaded:

if(Mono.Unix.Native.Syscall.isatty(1))
{
    Console.Write("\rTrue ");
}
else
{
    // Truncate the output file first
    Console.OpenStandardOutput().SetLength(0);
    Console.Write("True");
}

I doubt this works on Windows, but there's probably an equivalent to isatty().

Mechanical snail

Posted 2014-01-03T14:04:36.190

Reputation: 2 213

1

83... WIP

I was hoping this'd work, but apparently "False" isn't being interned as I'd hoped in my compiler

//}unsafe static Program(){fixed(char*f="False"){*(long*)f=0x65007500720054;*(f+4)=' ';}
}unsafe static Program(){fixed(char*f="False")for(int i=5;i-->0;)*(f+i)="True "[i];

ungolfed

class Program
{
    static void Main(string[] args)
    {
        Console.Write("False");
    }

    unsafe static Program()
    {
        fixed (char* f = "False") for (int i = 5; i-- > 0; ) *(f + i) = "True "[i];
    }
}

NPSF3000

Posted 2014-01-03T14:04:36.190

Reputation: 374

1

class Program
{
    static void Main()
    {
        System.Console.Write("False");

        System.Console.Clear(); System.Console.Write("True");

        Console.ReadLine();
    }
}

satarupa

Posted 2014-01-03T14:04:36.190

Reputation: 41

This won't work if stdout is redirected. – Kendall Frey – 2014-02-19T13:03:48.323

0

Not sure if this is accepted.
The question doesn't say anything about compiler options so I assume I can change the entry point of the app.
55 characters ,3 more than the accepted answer

}class P{static void Main(){System.Console.Write(1>0);}

Full

class Program
{
    static void Main()
    {
        System.Console.Write("False");
    }
    class P
    {
        static void Main() { System.Console.Write(1 > 0); }
    }
}

George Vovos

Posted 2014-01-03T14:04:36.190

Reputation: 101

Welcome to the community! – Erik the Outgolfer – 2016-07-22T09:48:14.597

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Thanks. This is a fantastic site!!! – George Vovos – 2016-07-22T09:49:08.457