Giter Site home page Giter Site logo

Handling Wide strings about jackalope HOT 7 OPEN

MFaisalZaki avatar MFaisalZaki commented on July 25, 2024
Handling Wide strings

from jackalope.

Comments (7)

ifratric avatar ifratric commented on July 25, 2024

Can you post your command line and the exact output you get from Jackalope?

from jackalope.

ifratric avatar ifratric commented on July 25, 2024

Also, which compiler are you using for the target and which Windows version you are running on?

from jackalope.

MFaisalZaki avatar MFaisalZaki commented on July 25, 2024

@ifratric I am using MSVC 17. The command I executed is:

fuzzer.exe -in in -out out -t 1000 -delivery shmem -instrument_module fuzz-example-jackalope.exe -target_module fuzz-example-jackalope.exe -target_method wmain -nargs 1 -iterations 10000 -persist -loop -cmp_coverage -- fuzz-example-jackalope.exe -m

The CLI output:

output

As for the input dir in, it contained one file named in-01.txt

Here is the complete code:

// fuzz-example-jackalope.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>

#include <string>
#include <vector>

using namespace std;

_declspec(dllexport) _declspec(noinline) vector<wchar_t> serialize(const wstring& file) {
	vector<wchar_t> serialized_data;
	serialized_data.resize(file.size() * sizeof(wchar_t));
	file.copy((wchar_t*)&serialized_data, file.size());
	return serialized_data;
};

_declspec(dllexport) _declspec(noinline) wstring deserialize(const std::vector<wchar_t>& serialdata) {
	return  wstring((wchar_t*)&serialdata, (serialdata.size()) / sizeof(wchar_t));
};


int wmain(int argc, wchar_t** argv) {

	if (argc < 2) {
		std::cout << "Usage fuzz-proxy: <WIDE-STRING> " << std::endl;
		return -1;
	}

	wstring output_string;
	output_string = deserialize(serialize(argv[1]));

	auto check = (output_string != argv[1]);
	if (!check) {
		throw "Failed to serialize sequence.";
	}
	return 0;
}

from jackalope.

ifratric avatar ifratric commented on July 25, 2024

I see several issues with your command line

  • you are passing '-m' as the first argument to the target, but the target expects a file there. It should be "fuzz-example-jackalope.exe @@"
  • you are specifying shared memory sample transfer, but your target does not implement support for it.
  • wmain takes 2 arguments, not 1 (-nargs flag)

Additionally, you might encounter some issues because your target function throw()s instead of returning normally.

from jackalope.

MFaisalZaki avatar MFaisalZaki commented on July 25, 2024

@ifratric, thanks for the comments; I have updated the command to be:

fuzzer.exe -in in -out out -t 1000 -instrument_module fuzz-example-jackalope.exe -target_module fuzz-example-jackalope.exe -target_method wmain -nargs 2 -iterations 10000 -persist -loop -cmp_coverage -- fuzz-example-jackalope.exe @@ 

The output is:

Screenshot 2022-12-26 at 9 57 57 PM

So please advise, and if you have an example I can use, this would be great.

And removed the throw, the code is:

#include <iostream>

#include <string>
#include <vector>

using namespace std;

_declspec(dllexport) _declspec(noinline) vector<wchar_t> serialize(const wstring& file) {
	vector<wchar_t> serialized_data;
	serialized_data.resize(file.size() * sizeof(wchar_t));
	file.copy((wchar_t*)&serialized_data, file.size());
	return serialized_data;
};

_declspec(dllexport) _declspec(noinline) wstring deserialize(const std::vector<wchar_t>& serialdata) {
	return  wstring((wchar_t*)&serialdata, (serialdata.size()) / sizeof(wchar_t));
};


int wmain(int argc, wchar_t** argv) {
	if (argc < 2) {
		std::cout << "Usage fuzz-proxy: <WIDE-STRING> " << std::endl;
		return -1;
	}
	return (deserialize(serialize(argv[1])) != argv[1] ? -1 : 0);
}

from jackalope.

ifratric avatar ifratric commented on July 25, 2024

Your code has bugs, this is why Jackalope is reporting all input samples are crashing. Try running it in a debugger first.

if you have an example I can use, this would be great

Sure, there's an example target that comes with Jackalope (test.exe) and an example command to run against it is in the README.

from jackalope.

MFaisalZaki avatar MFaisalZaki commented on July 25, 2024

@ifratric I have modified the test.cpp to deal with wide strings, and I got this issue:
wmain-issue

and here is the modified code for reference:

/*
Copyright 2020 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <inttypes.h>

// shared memory stuff

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
#include <windows.h>
#else
#include <sys/mman.h>
#endif

#define MAX_SAMPLE_SIZE 1000000
#define SHM_SIZE (4 + MAX_SAMPLE_SIZE)
wchar_t* shm_data;

bool use_shared_memory;

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)

int setup_shmem(const wchar_t* name) {
    HANDLE map_file;
    char* shmem_name;
    wcstombs(shmem_name, name, wcslen(name));

    map_file = OpenFileMapping(
        FILE_MAP_ALL_ACCESS,   // read/write access
        FALSE,                 // do not inherit the name
        shmem_name);            // name of mapping object

    if (map_file == NULL) {
        printf("Error accessing shared memory\n");
        return 0;
    }

    shm_data = (wchar_t*)MapViewOfFile(map_file, // handle to map object
        FILE_MAP_ALL_ACCESS,  // read/write permission
        0,
        0,
        SHM_SIZE);

    if (shm_data == NULL) {
        printf("Error accessing shared memory\n");
        return 0;
    }

    return 1;
}

#else

int setup_shmem(const char* name)
{
    int fd;

    // get shared memory file descriptor (NOT a file)
    fd = shm_open(name, O_RDONLY, S_IRUSR | S_IWUSR);
    if (fd == -1)
    {
        printf("Error in shm_open\n");
        return 0;
    }

    // map shared memory to process address space
    shm_data = (unsigned char*)mmap(NULL, SHM_SIZE, PROT_READ, MAP_SHARED, fd, 0);
    if (shm_data == MAP_FAILED)
    {
        printf("Error in mmap\n");
        return 0;
    }

    return 1;
}

#endif

// used to force a crash
char* crash = NULL;

// ensure we can find the target

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
#define FUZZ_TARGET_MODIFIERS __declspec(dllexport)
#else
#define FUZZ_TARGET_MODIFIERS __attribute__ ((noinline))
#endif

// actual target function

void FUZZ_TARGET_MODIFIERS fuzz(wchar_t* name) {
    wchar_t* sample_bytes = NULL;
    uint32_t sample_size = 0;

    // read the sample either from file or
    // shared memory
    if (use_shared_memory) {
        sample_size = *(uint32_t*)(shm_data);
        if (sample_size > MAX_SAMPLE_SIZE) sample_size = MAX_SAMPLE_SIZE;
        sample_bytes = (wchar_t*)malloc(sample_size);
        memcpy(sample_bytes, shm_data + sizeof(uint32_t), sample_size);
    }
    else {
        char* filename;
        wcstombs(filename, name, wcslen(name));

        FILE* fp = fopen(filename, "rb");
        if (!fp) {
            printf("Error opening %s\n", filename);
            return;
        }
        fseek(fp, 0, SEEK_END);
        sample_size = ftell(fp);
        fseek(fp, 0, SEEK_SET);
        sample_bytes = (wchar_t*)malloc(sample_size);
        fread(sample_bytes, 1, sample_size, fp);
        fclose(fp);
    }

    if (sample_size >= 4) {
        // check if the sample spells "test"
        if (*(uint64_t*)(sample_bytes) == 0x7400730065007400) {
            // if so, crash
            crash[0] = 1;
        }
    }

    if (sample_bytes) free(sample_bytes);
}

int wmain(int argc, wchar_t** argv)
{
    if (argc != 3) {
        printf("Usage: %ws <-f|-m> <file or shared memory name>\n", argv[0]);
        return 0;
    }

    if (!wcscmp(argv[1], L"-m")) {
        use_shared_memory = true;
    }
    else if (!wcscmp(argv[1], L"-f")) {
        use_shared_memory = false;
    }
    else {
        printf("Usage: %ws <-f|-m> <file or shared memory name>\n", argv[0]);
        return 0;
    }

    // map shared memory here as we don't want to do it
    // for every operation
    if (use_shared_memory) {
        if (!setup_shmem(argv[2])) {
            printf("Error mapping shared memory\n");
        }
    }

    fuzz(argv[2]);

    return 0;
}

from jackalope.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.