示例#1
0
    // This method writes out a PE file.
    internal static void PersistPEFile(ref BinaryWriter writer, ref byte[] buf)
    {
        uint i, chunkSize, fileOffset;

        MemoryStream memStream = new MemoryStream(buf);
        BinaryReader memReader = new BinaryReader(memStream);

        // Crack the headers.
        AllHeaders all = new AllHeaders(ref memReader);
        NTHeader   nth = all.m_ntHeader;

        SectionHeader[] sh = all.m_sectionHeaders;

        // Write the headers to the file.
        writer.Write(buf, 0, (int)nth.m_HeaderSize);

        // Write each section to the file.
        for (i = 0; i < nth.m_NumberOfSections; i++)
        {
            fileOffset = sh[i].m_PointerToRawData;
            if (fileOffset != 0)
            {
                chunkSize = Roundup(sh[i].m_SizeOfRawData, nth.m_FileAlignment);

                writer.BaseStream.Seek(fileOffset, SeekOrigin.Begin);
                writer.Write(buf, (int)sh[i].m_VirtualAddress, (int)chunkSize);
            }
        }
        writer.Flush();
    }
        /// <summary>
        /// Deflate the token
        /// </summary>
        /// <param name="destination">Stream to deflate token to</param>
        public override void Deflate(Stream destination)
        {
            // Check if we have all headers
            if (AllHeaders != null)
            {
                // Deflate all headers
                AllHeaders.Deflate(destination);
            }

            // Convert text to stream
            byte[] textBytes = Encoding.Unicode.GetBytes(Text);

            // Write to the destination
            destination.Write(textBytes, 0, textBytes.Length);
        }
示例#3
0
    // This method reads a PE file, and then returns a byte array representation of the file.
    internal static byte[] ParsePEFile(ref BinaryReader reader)
    {
        uint i, fileOffset;

        byte[] buf;

        AllHeaders all = new AllHeaders(ref reader);
        NTHeader   nth = all.m_ntHeader;

        SectionHeader[] sh = all.m_sectionHeaders;

        // "ImageSize" and "FileAlignment" come from Optional header - NT-specific fields.
        buf = new byte[nth.m_ImageSize];

                #if DEBUG0
        Console.WriteLine("size of image: " + nth.m_ImageSize + Environment.NewLine);
                #endif

        // Ok.  Got all the info.  Ready to read in the sections.
        Debug.Assert(nth.m_FileAlignment >= 512);

        reader.BaseStream.Seek(0, SeekOrigin.Begin);
        reader.Read(buf, 0, (int)nth.m_HeaderSize);             // Read first "nth.m_HeaderSize" bytes.  These are the headers.

        // Read each section from the file.
        for (i = 0; i < nth.m_NumberOfSections; i++)
        {
            fileOffset = sh[i].m_PointerToRawData;
            if (fileOffset != 0)
            {
                reader.BaseStream.Seek(fileOffset, SeekOrigin.Begin);
                reader.Read(buf, (int)sh[i].m_VirtualAddress, (int)sh[i].m_SizeOfRawData);

                                #if DEBUG0
                Console.Write("name of section " + i + ": ");
                for (int j = 0; j < 8; j++)
                {
                    Console.Write((char)sh[i].m_Name[j]);
                }
                Console.WriteLine();
                Console.WriteLine("size of section " + i + ": " + sh[i].m_SizeOfRawData + Environment.NewLine);
                                #endif
            }
        }
        return(buf);
    }
        public async Task <ActionResult> Create()
        {
            List <string> orgNames = new List <string>();

            HierachyNode node = new HierachyNode()
            {
                IncludeAncestors = true, IncludeChildren = true, IncludeDescendents = 0, IncludeSiblings = true, IncludeSubItems = true
            };

            node.ObjectKey = new ObjectKey()
            {
                ObjectType = "ObjectType_OrganisationUnit", Int32Value = 0
            };
            // POST request
            HttpResponseMessage response = await client.PostAsJsonAsync(API_LIST_HIERACHY, node);

            // Check response
            if (!response.IsSuccessStatusCode)
            {
                throw new System.ArgumentException(response.StatusCode.ToString(), "original");
            }
            // Get headers which contain organisation units
            var        responseDetail = response.Content.ReadAsStringAsync().Result;
            AllHeaders netObjects     = JsonConvert.DeserializeObject <AllHeaders>(responseDetail);

            // Get organisation ID from first header for each department
            for (int i = 0; i < netObjects.Headers.Length; i++)
            {
                orgNames.Add(netObjects.Headers[i].ObjectName);
            }
            Updated update = new Updated()
            {
                OrganisationName = GetSelectListItems(orgNames)
            };

            return(View(update));
        }
示例#5
0
        public async System.Threading.Tasks.Task CreateRequestImageUploadAsync()
        {
            var    ACCESS_TOKEN       = WebConfigurationManager.AppSettings["access_token"];
            var    API_LIST_HIERACHY  = WebConfigurationManager.AppSettings["api_list_hierachy"];
            var    API_CREATE_REQUEST = WebConfigurationManager.AppSettings["api_create_request"];
            var    API_ADD_DOCUMENT   = WebConfigurationManager.AppSettings["api_add_document"];
            string responseDetail     = "";

            HttpClient client = new HttpClient
            {
                BaseAddress = new Uri(WebConfigurationManager.AppSettings["apiBaseAddress"])
            };

            // Address SSL and TLS security issue.
            #region
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 |
                                                   SecurityProtocolType.Tls |
                                                   SecurityProtocolType.Tls11 |
                                                   SecurityProtocolType.Tls12;
            #endregion
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ACCESS_TOKEN);

            // Post to get organisation unit to a list of units.
            HierachyNode node = new HierachyNode()
            {
                IncludeAncestors = true, IncludeChildren = true, IncludeDescendents = 0, IncludeSiblings = true, IncludeSubItems = true
            };
            node.ObjectKey = new ObjectKey()
            {
                ObjectType = "ObjectType_OrganisationUnit", Int32Value = 0
            };
            // POST request
            HttpResponseMessage response = await client.PostAsJsonAsync(API_LIST_HIERACHY, node);

            // Check response
            if (!response.IsSuccessStatusCode)
            {
                throw new System.ArgumentException(response.StatusCode.ToString(), "Cannot get organisation units");
            }
            // Get headers which contain organisation units
            responseDetail = response.Content.ReadAsStringAsync().Result;
            AllHeaders netObjects = JsonConvert.DeserializeObject <AllHeaders>(responseDetail);


            // Get organisation ID from first header for each department
            int orgUnitId = netObjects.Headers[0].ObjectKey.Int32Value;
            // Create user object and changing the necessary fields.
            string[] changes = { "RequestDetail", "RequestorName", "OrganisationUnitID" };
            User     user    = new User();
            user.ChangeSet = new ChangeSet()
            {
                Changes = changes
            };
            user.ChangeSet.Updated = new Updated {
                RequestDetail = "This a request" + netObjects.Headers[0].ObjectName, RequestorName = "Jason Lee", OrganisationUnitID = orgUnitId
            };
            // POST request
            HttpResponseMessage createUserResponse = await client.PostAsJsonAsync(API_CREATE_REQUEST, user);

            // Check response
            if (!createUserResponse.IsSuccessStatusCode)
            {
                throw new System.ArgumentException(createUserResponse.StatusCode.ToString(), "Cannot create user");
            }
            // Get request ID
            string requestID = createUserResponse.Content.ReadAsStringAsync().Result;
            Debug.WriteLine(String.Format("Request ID: {0}    |     Organisation Name: {1}", requestID, netObjects.Headers[0].ObjectName));

            // Create Container for chosen Request. Change path to "Request" and use requestID.
            DocContainer con = new DocContainer()
            {
                DocumentDescription = "Request Image", Address = $"file://conquest_documents/Request/{requestID}/JasonTestImage.png", ContentType = "image/png",
            };
            con.ObjectKey = new ObjectKey()
            {
                ObjectType = "ObjectType_Request", Int32Value = int.Parse(requestID)
            };
            HttpResponseMessage createFileResponse = await client.PostAsJsonAsync(API_ADD_DOCUMENT, con);

            // Check response
            if (!createFileResponse.IsSuccessStatusCode)
            {
                throw new System.ArgumentException(createFileResponse.StatusCode.ToString(), "Cannot create file response");
            }
            Debug.WriteLine(createFileResponse.Content.ReadAsStringAsync().Result);

            // Upload image to container
            DocDataObject docDataObject = JsonConvert.DeserializeObject <DocDataObject>(createFileResponse.Content.ReadAsStringAsync().Result);

            var f         = System.IO.File.OpenRead("..\\..\\B.png");
            var content   = new StreamContent(f);
            var mpcontent = new MultipartFormDataContent();
            content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
            mpcontent.Add(content);

            HttpResponseMessage uploadDocumentResponse = await client.PutAsync(docDataObject.UploadUri, mpcontent);

            //Check response
            if (!uploadDocumentResponse.IsSuccessStatusCode)
            {
                throw new System.ArgumentException(uploadDocumentResponse.StatusCode.ToString(), "Cannot upload image");
            }
        }
示例#6
0
    internal MetaDataRoot m_mdr;                                // MetaData Root table

    /*********************************************************************************************************************
    *	1) Parse the MetaData Root table.
    *
    *	2) Initialize the various heaps.
    *
    *	3) Initialize "m_oldNameHash", "m_classTree", and "m_freeSpaceList".
    *
    *	4) Initialize the various BitArrays.
    *
    *	5) Set the total numbers of the elements (types, methods, fields, properties, events, and parameters).
    *********************************************************************************************************************/
    internal MetaData(ref byte[] buf)
    {
        int       i, ptHeapIndex = -1;
        Exception toBeThrown;

        m_buffer = buf;

        // Ok.  Now we can build a reader and a writer over the memory m_buffer.
        MemoryStream memStream = new MemoryStream(m_buffer);
        BinaryReader reader    = new BinaryReader(memStream);
        BinaryWriter writer    = new BinaryWriter(memStream);

        AllHeaders all = new AllHeaders(ref reader);
        NTHeader   nth = all.m_ntHeader;

        SectionHeader[] sh = all.m_sectionHeaders;

                #if DEBUG0
        Console.WriteLine("Runtime Header Data Directory");
        Console.WriteLine("rva: " + nth.m_DataDirectory[14].RVA + "; size: " + nth.m_DataDirectory[14].Size + Environment.NewLine);
        Console.WriteLine("offset to MetaData section: " + (long)(nth.m_DataDirectory[14].RVA + 8));
                #endif

        // Read the RVA of the physical MetaData section from the Runtime Header Data Directory.
        reader.BaseStream.Seek((long)(nth.m_DataDirectory[14].RVA + 8), SeekOrigin.Begin);
        m_startPos = reader.ReadUInt64();

        // Theoretically, startPos can be 64 bit long, but in practice, 32 bits are enough.
        // TODO : Check the docs.  The following assertion will fail.
        // Debug.Assert((startPos >> 32) == 0);
        m_startPos = m_startPos & 0xffffffff;                                                   // use the least significant 4 bytes as the offset

        m_mdr = new MetaDataRoot(ref reader, ref m_buffer, m_startPos);

        // We need to initialize the #Strings heap and the #Blob heap before dealing with the #~ heap.
        for (i = 0; i < m_mdr.m_numStreams; i++)
        {
            if (m_mdr.m_streamHeaders[i].name.Equals("#Strings\0"))
            {
                m_strHeap = new StringHeap(ref m_buffer, m_startPos + m_mdr.m_streamHeaders[i].offset, m_mdr.m_streamHeaders[i].size);
            }

            else if (m_mdr.m_streamHeaders[i].name.Equals("#Blob\0"))
            {
                m_blobHeap = new BlobHeap(ref m_buffer, m_startPos + m_mdr.m_streamHeaders[i].offset, m_mdr.m_streamHeaders[i].size);
            }

            else if (m_mdr.m_streamHeaders[i].name.Equals("#~\0") || m_mdr.m_streamHeaders[i].name.Equals("#-\0"))
            {
                ptHeapIndex = i;
            }
        }

        if (ptHeapIndex != -1)
        {
            m_heap = new PTHeap(ref reader, ref writer, ref m_strHeap, m_startPos + m_mdr.m_streamHeaders[ptHeapIndex].offset);
        }
        else
        {
            toBeThrown = new InvalidFileFormat("Invalid CIL binary file format.");
            throw toBeThrown;
        }
    }