Giter VIP home page Giter VIP logo

base64-search's Introduction

base64-search

JavaScript library for searching for strings in base64-encoded blobs.

Illustration of a large pink glowing crystal in a cave system.

I sometimes need to check whether a blob of data with both plain and base64-encoded text contains a certain string. This may happen when analysing the traffic dump of a program or app for security or data protection research, for example.

However, depending on the offset of the search string in the source string, it can be base64-encoded in different ways. Take a look at this JSON blob as an example:

{
    "attr1": "Hello, world!",
    "attr2": "WW91IGNhbiBnZW5lcmF0ZSBHRFBSIHJlcXVlc3RzIGF0IGRhdGFyZXF1ZXN0cy5vcmchIENoZWNrIGl0IG91dDogaHR0cHM6Ly93d3cuZGF0YXJlcXVlc3RzLm9yZy9nZW5lcmF0b3I=",
    "attr3": "base64-search",
    "attr4": "SGVsbG8sIHdvcmxkIQ=="
}

Imagine I want to check whether it contains the string datarequests.org anywhere. If we base64-encode that, we get ZGF0YXJlcXVlc3RzLm9yZw==. This is not contained anywhere in the JSON. But concluding that the JSON doesn't contain datarequests.org based on that would be wrong.

Using base64-search, we find that there are in fact three different ways that datarequests.org could be encoded that we all need to check (to learn more about why that is, have a look at this great blog post, which this library is inspired by):

> base64Encodings('datarequests.org')
[
  'ZGF0YXJlcXVlc3RzLm9yZ',
  'RhdGFyZXF1ZXN0cy5vcm',
  'YXRhcmVxdWVzdHMub3Jn'
]

And indeed we can see that both ZGF0YXJlcXVlc3RzLm9yZ and RhdGFyZXF1ZXN0cy5vcm are present in the JSON blob, so it does contain datarequests.org, twice even.

Now, in this case, I could of course have just decoded both base64 strings and then performed string matching on the results. But this method is for when you want to search arbitrary data that you don't know the shape of and without manual work.

Installation

You can install base64-search using yarn or npm:

yarn add base64-search
# or `npm i base64-search`

Example usage

import { base64Regex, base64Encodings } from 'base64-search';

console.log(base64Regex('ea70edc1'));
// (VhNzBlZGMx|YTcwZWRjM|ZWE3MGVkYz)

console.log(base64Encodings('ea70edc1'));
// [ 'ZWE3MGVkYz', 'VhNzBlZGMx', 'YTcwZWRjM' ]

API

This library provides the following two functions:

base64Regex(input)

Parameters:

  • input (required) – The string or other data that you want to search for. Can be a string or Buffer.

returns: string – A regex that matches all possible base64 encodings of input in a single capture group.

base64Encodings(input)

Parameters:

  • input (required) – The string or other data that you want to search for. Can be a string or Buffer.

returns: string[] – An array of all possible base64 encodings of input.

License

base64-search is licensed under the MIT license, see the LICENSE file for details. It was inspired by this blog post, which contains an alternative implementation of the method for PowerShell.

Issues and pull requests are welcome!

base64-search's People

Contributors

baltpeter avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.