Esempio n. 1
0
 private void Relocate(uint offest)
 {
     GruntyOS.IO.BinaryWriter bw = new GruntyOS.IO.BinaryWriter(new GruntyOS.IO.MemoryStream(code));
     GruntyOS.IO.BinaryReader br = new GruntyOS.IO.BinaryReader(new GruntyOS.IO.MemoryStream(code));
     for (int i = 0; i < symbolsToRelocate.Count; i++)
     {
         br.BaseStream.Position = (int)symbolsToRelocate[i].Offest;
         ushort val = BitConverter.ToUInt16(new byte[] { br.ReadByte(), br.ReadByte() }, 0);
     }
 }
Esempio n. 2
0
        public PE32(string path)
        {
            GruntyOS.IO.BinaryReader br = new GruntyOS.IO.BinaryReader(new GruntyOS.IO.FileStream(path, "r"));
            int p = 0;
            uint address = 0;
            uint data_addr = 0;
            uint ib = 0;
            for (int i = 0; i < (int)br.BaseStream.Data.Length; i++)
            {
                p = br.BaseStream.Position;
                if (br.ReadByte() == (byte)'P' && br.ReadByte() == (byte)'E')
                    break;
            }
            br.BaseStream.Position = p;
            Console.WriteLine("Start: " + p.ToString());
            byte[] hdr = new byte[(sizeof(PeHeader))];
            for (int i = 0; i < sizeof(PeHeader); i++)
            {
                hdr[i] = br.ReadByte();
            }
            fixed (byte* ptr = hdr)
            {
                PeHeader* header = (PeHeader*)ptr;
                Console.WriteLine(header->mMachine.ToString());
                byte[] ohdr = new byte[header->mSizeOfOptionalHeader];

                for (int i = 0; i < header->mSizeOfOptionalHeader; i++)
                {
                    ohdr[i] = br.ReadByte();
                }
                fixed (byte* ptr2 = ohdr)
                {
                    Pe32OptionalHeader* opt = (Pe32OptionalHeader*)ptr2;
                    Console.WriteLine(opt->mBaseOfCode.ToString());
                    byte[] tmp = new byte[40];
                    address = opt->mBaseOfCode;
                    data_addr = opt->mBaseOfData;
                    ib = opt->mImageBase;
                    for (int s = 0; s < header->mNumberOfSections; s++)
                    {

                        fixed (byte* ptr3 = tmp)
                        {
                            for (int i = 0; i < 40; i++)
                            {
                                tmp[i] = br.ReadByte();
                            }
                            SectionHeader* sec = (SectionHeader*)ptr3;
                            string name = "";
                            for (int c = 0; sec->Name[c] != 0; c++)
                                name += ((char)sec->Name[c]).ToString();
                            Section section = new Section();
                            section.Name = name;
                            section.Address = (uint)sec->PointerToRawData;
                            section.RelocationCount = (uint)sec->NumberOfRelocations;
                            section.RelocationPtr = (uint)sec->PointerToRelocations;
                            section.Size = (uint)sec->SizeOfRawData;
                            Console.WriteLine(((int)(uint)sec->VirtualAddress).ToString());
                            sections.Add(section);
                        }
                    }
                }
                for (int i = 0; i < sections.Count; i++)
                {
                    if (sections[i].Name == ".text")
                    {
                        text = new byte[sections[i].Size];
                        br.BaseStream.Position = (int)(uint)sections[i].Address;
                        for (int b = 0; b < (int)(uint)sections[i].Size; b++)
                        {
                            text[b] = br.ReadByte();
                        }
                    }
                    else if (sections[i].Name == ".data")
                    {
                        data = new byte[sections[i].Size];
                        br.BaseStream.Position = (int)(uint)sections[i].Address;
                        for (int b = 0; b < (int)(uint)sections[i].Size; b++)
                        {
                            data[b] = br.ReadByte();
                        }
                    }
                }
            }
            // We do not have paging working and I an to lazy to relocate this
            // so we are just loading this were the PE header tells us to
            // may be bad, because we 'could' be overwritting something
            // in RAM. Im not sure.... Lets hope not
            byte* dptr = (byte*)ib + address;
            for (int i = 0; i < text.Length; i++)
            {
                dptr[i] = text[i];
            }
            dptr = (byte*)ib + data_addr;
            for (int i = 0; i < data.Length; i++)
            {
                dptr[i] = data[i];
            }
            Caller cl = new Caller();
            cl.CallCode(ib + address); // Jump!!!!!
        }
        public PE32(string path)
        {
            GruntyOS.IO.BinaryReader br = new GruntyOS.IO.BinaryReader(new GruntyOS.IO.FileStream(path, "r"));
            int  p         = 0;
            uint address   = 0;
            uint data_addr = 0;
            uint ib        = 0;

            for (int i = 0; i < (int)br.BaseStream.Data.Length; i++)
            {
                p = br.BaseStream.Position;
                if (br.ReadByte() == (byte)'P' && br.ReadByte() == (byte)'E')
                {
                    break;
                }
            }
            br.BaseStream.Position = p;
            Console.WriteLine("Start: " + p.ToString());
            byte[] hdr = new byte[(sizeof(PeHeader))];
            for (int i = 0; i < sizeof(PeHeader); i++)
            {
                hdr[i] = br.ReadByte();
            }

            fixed(byte *ptr = hdr)
            {
                PeHeader *header = (PeHeader *)ptr;

                Console.WriteLine(header->mMachine.ToString());
                byte[] ohdr = new byte[header->mSizeOfOptionalHeader];

                for (int i = 0; i < header->mSizeOfOptionalHeader; i++)
                {
                    ohdr[i] = br.ReadByte();
                }

                fixed(byte *ptr2 = ohdr)
                {
                    Pe32OptionalHeader *opt = (Pe32OptionalHeader *)ptr2;

                    Console.WriteLine(opt->mBaseOfCode.ToString());
                    byte[] tmp = new byte[40];
                    address   = opt->mBaseOfCode;
                    data_addr = opt->mBaseOfData;
                    ib        = opt->mImageBase;
                    for (int s = 0; s < header->mNumberOfSections; s++)
                    {
                        fixed(byte *ptr3 = tmp)
                        {
                            for (int i = 0; i < 40; i++)
                            {
                                tmp[i] = br.ReadByte();
                            }
                            SectionHeader *sec  = (SectionHeader *)ptr3;
                            string         name = "";

                            for (int c = 0; sec->Name[c] != 0; c++)
                            {
                                name += ((char)sec->Name[c]).ToString();
                            }
                            Section section = new Section();

                            section.Name            = name;
                            section.Address         = (uint)sec->PointerToRawData;
                            section.RelocationCount = (uint)sec->NumberOfRelocations;
                            section.RelocationPtr   = (uint)sec->PointerToRelocations;
                            section.Size            = (uint)sec->SizeOfRawData;
                            Console.WriteLine(((int)(uint)sec->VirtualAddress).ToString());
                            sections.Add(section);
                        }
                    }
                }

                for (int i = 0; i < sections.Count; i++)
                {
                    if (sections[i].Name == ".text")
                    {
                        text = new byte[sections[i].Size];
                        br.BaseStream.Position = (int)(uint)sections[i].Address;
                        for (int b = 0; b < (int)(uint)sections[i].Size; b++)
                        {
                            text[b] = br.ReadByte();
                        }
                    }
                    else if (sections[i].Name == ".data")
                    {
                        data = new byte[sections[i].Size];
                        br.BaseStream.Position = (int)(uint)sections[i].Address;
                        for (int b = 0; b < (int)(uint)sections[i].Size; b++)
                        {
                            data[b] = br.ReadByte();
                        }
                    }
                }
            }

            // We do not have paging working and I an to lazy to relocate this
            // so we are just loading this were the PE header tells us to
            // may be bad, because we 'could' be overwritting something
            // in RAM. Im not sure.... Lets hope not
            byte *dptr = (byte *)ib + address;

            for (int i = 0; i < text.Length; i++)
            {
                dptr[i] = text[i];
            }
            dptr = (byte *)ib + data_addr;
            for (int i = 0; i < data.Length; i++)
            {
                dptr[i] = data[i];
            }
            Caller cl = new Caller();

            cl.CallCode(ib + address); // Jump!!!!!
        }
Esempio n. 4
0
 private void Relocate(uint offest)
 {
     GruntyOS.IO.BinaryWriter bw = new GruntyOS.IO.BinaryWriter(new GruntyOS.IO.MemoryStream(code));
     GruntyOS.IO.BinaryReader br = new GruntyOS.IO.BinaryReader(new GruntyOS.IO.MemoryStream(code));
     for (int i = 0; i < symbolsToRelocate.Count; i++)
     {
         br.BaseStream.Position = (int)symbolsToRelocate[i].Offest;
         ushort val = BitConverter.ToUInt16(new byte[] { br.ReadByte(), br.ReadByte() }, 0);
     }
 }