Skip to content

Random() is not secure because it provides insufficient entropy. CWE ID 331. #536

Closed

Description

@tdhintz

Steps to reproduce

  1. Static code security scan. Example:
		private static void WriteEncryptionHeader(Stream stream, long crcValue)
		{
			byte[] cryptBuffer = new byte[ZipConstants.CryptoHeaderSize];
			var rnd = new Random();
			rnd.NextBytes(cryptBuffer);
			cryptBuffer[11] = (byte)(crcValue >> 24);
			stream.Write(cryptBuffer, 0, cryptBuffer.Length);
		}

Expected behavior

Replace all uses of System.Random() with a cryptographic version such as that provided by RNGCryptoServiceProvider.

Actual behavior

SharpZipLib fails security scans.

Version of SharpZipLib

1.3.0

Obtained from (only keep the relevant lines)

  • Package installed using NuGet

Activity

  1. piksel commented on Nov 10, 2020

    @piksel
    Member

    The referenced code is this:

    private static void WriteEncryptionHeader(Stream stream, long crcValue)

    But the same method is also used in:

    private void WriteEncryptionHeader(long crcValue)

  2. tdhintz commented on Nov 10, 2020

    @tdhintz
    Author

    There are possibly 3 uses of the insecure Random() found throughout the codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions