BaltCTF 2013 Binary 500

binary500 is .net reserve.

it Deserialize a class to filestream. which contain 625 neurons
neurons struct like this:
{
data,
weight
}
default data are all 0.
after the Deserialize ,those 625 neurons’s data is filled with user control data,actually data from our input image file.
all neurons’s weight is from pink.data
the programe try to divide 625 into 10 blocks,and calc sum of data*weight.
correct input will make the second block’s sum is the biggest,which is the program wanted.

namespace koh
{
    using AForge;
    using AForge.Imaging;
    using AForge.Imaging.Filters;
    //using koh;
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Formatters.Binary;

    internal class Program
    {
        private static bool CheckArg(string arg)
        {
            try
            {
                FileInfo info = new FileInfo(arg);
                return (info.Exists && (info.Length < 0x800000L));
            }
            catch (Exception)
            {
                return false;
            }
        }

        private static List ConvertImage(Bitmap img)
        {
            List list = new List();
            using (UnmanagedImage image = UnmanagedImage.FromManagedImage(img))
            {
                List points = new List();
                for (int i = 0; i < image.Width; i++)
                {
                    for (int j = 0; j < image.Height; j++)
                    {
                        points.Add(new IntPoint(i, j));
                        Console.Write(i+","+j+":");
                    }
                }
                byte[] collection = image.Collect8bppPixelValues(points);
                Console.Write(collection + ":");

                list.AddRange(collection);
            }
            return list;
        }

        private static Bitmap LoadImage(string arg)
        {
            return AForge.Imaging.Image.FromFile(arg);
        }

        private static void Main(string[] args)
        {
           /* if ((args.Length != 1) || !CheckArg(args[0]))
            {
                Usage();
            }
            else
            {
            * */
              //  string arg = args[0];
                string arg = "C:\\Users\\Administrator\\Desktop\\baltctf\\braaainz\\out.bmp";
                Bitmap img = ProcessImage(LoadImage(arg));
                List source = ConvertImage(img);
                img.Save("out.jpg");
                BinaryFormatter formatter = new BinaryFormatter();
                FileStream serializationStream = File.OpenRead("pinky.brain");
                KohonenLayer layer = (KohonenLayer) formatter.Deserialize(serializationStream);

                Console.WriteLine(layer.Neurons.Count);
                Console.WriteLine(layer.PrevLayer.Count);


            Console.WriteLine("######################################");
            Console.WriteLine(layer.PrevLayer.Count);

                for (int j = 0; j < layer.PrevLayer.Count; j++)
                {
                    //Console.Write(layer.PrevLayer[j]);

                    foreach (Link link in layer.PrevLayer[j].IngoingLinks)
                    {
                        //Console.Write(link.Data);
                      //  Console.WriteLine(link.Weight);
                    }

                    foreach (Link link in layer.PrevLayer[j].OutgoingLinks)
                    {
                        //Console.Write(link.Data);
                      //  Console.WriteLine(link.Weight);
                    }


                }
                double minValue = double.MinValue;
                int num2 = 0;//让1号神经元的SUM最大。625个点指向10个神经元

                List aa = source.Select(new Func(Convert.ToDouble)).ToList();
            /////////////////////////////////////////


                for (int i = 0; i < aa.Count; i++)
                {
                    foreach (Link link in layer.PrevLayer[i].OutgoingLinks)
                    {
                        link.Data = aa[i];
                    }
                }

                Console.WriteLine("layer.Neurons.Count:"+layer.Neurons.Count);
                for (int j = 0; j  minValue)
                    {
                        minValue = num5;
                        num2 = j;
                    }
                }
                Console.WriteLine("return:"+num2);



            //source: 255---->aa:255.0

                Console.WriteLine(layer.Activate(aa));

            //}
        }

        private static Bitmap ProcessImage(Bitmap img)
        {
            img = new ResizeBicubic(0x19, 0x19).Apply(img);
            //http://www.aforgenet.com/framework/docs/html/1371dd12-7dbd-2a0e-a0d7-32d8c28d73eb.htm
            img = new GrayscaleRMY().Apply(img);
            //http://www.aforgenet.com/framework/docs/html/1100c0be-7a9f-490e-b49c-a2f3cc681a01.htm
            img = new OtsuThreshold().Apply(img);
            //http://www.aforgenet.com/framework/docs/html/b2bd54da-46c2-cb64-3577-0962d8f56554.htm
            return img;
        }

        private static void Usage()
        {
            Console.WriteLine("Provide path to image");
        }
    }
}

send the image to organizer,you will get the flag: Braaaaaaaaaaainzeeeeeerule