Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mrozio13pl committed Mar 17, 2024
1 parent bd60853 commit 0c3914a
Show file tree
Hide file tree
Showing 22 changed files with 149 additions and 312 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
license
test.mjs
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@mrozio/eslint-config"]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 mrozio13pl
Copyright (c) 2024 mrozio13pl

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
95 changes: 31 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,85 +1,52 @@
# HTML Obfuscator [![Version](https://img.shields.io/static/v1?label=Version&message=1.0.0&color=blue)](https://www.npmjs.com/package/html-obfuscator) [![License](https://img.shields.io/badge/licence-MIT-green)](https://github.com/mrozio13pl/html-obfuscator/blob/main/LICENSE) [![Build Passing](https://img.shields.io/badge/build-passing-success)](https://www.npmjs.com/package/html-obfuscator)
# html-obfuscator

##### A simple html obfuscator that makes code harder to read and understand
Extremely simple HTML obfuscator.

#### BEFORE:
Inspired by [snapbuilder.com](http://snapbuilder.com/code_snippet_generator/obfuscate_html_source_code/).

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello</title>
</head>
<body>
<div>
<p style="text-align: center">Hello world!</p>
</div>
</body>
</html>
```
> [!WARNING]
> Unless you are working with just plain HTML files, you probably want to use something more advanced instead.
## Example

#### AFTER:
`demo.html`

```html
<script>document.write(unescape('%3C%21%44%4F%43%54%59%50%45%20%68%74%6D%6C%3E%0D%0A%3C%68%74%6D%6C%20%6C%61%6E%67%3D%22%65%6E%22%3E%0D%0A%3C%68%65%61%64%3E%0D%0A%20%20%20%20%3C%6D%65%74%61%20%63%68%61%72%73%65%74%3D%22%55%54%46%2D%38%22%3E%0D%0A%20%20%20%20%3C%6D%65%74%61%20%68%74%74%70%2D%65%71%75%69%76%3D%22%58%2D%55%41%2D%43%6F%6D%70%61%74%69%62%6C%65%22%20%63%6F%6E%74%65%6E%74%3D%22%49%45%3D%65%64%67%65%22%3E%0D%0A%20%20%20%20%3C%6D%65%74%61%20%6E%61%6D%65%3D%22%76%69%65%77%70%6F%72%74%22%20%63%6F%6E%74%65%6E%74%3D%22%77%69%64%74%68%3D%64%65%76%69%63%65%2D%77%69%64%74%68%2C%20%69%6E%69%74%69%61%6C%2D%73%63%61%6C%65%3D%31%2E%30%22%3E%0D%0A%20%20%20%20%3C%74%69%74%6C%65%3E%44%6F%63%75%6D%65%6E%74%3C%2F%74%69%74%6C%65%3E%0D%0A%3C%2F%68%65%61%64%3E%0D%0A%3C%62%6F%64%79%3E%0D%0A%20%20%20%20%68%65%6C%6C%6F%0D%0A%3C%2F%62%6F%64%79%3E%0D%0A%3C%2F%68%74%6D%6C%3E'))</script>
<div>
<h1>Hello World!</h1>
</div>
<style>
div {
display: flex;
justify-content: center;
}
</style>
```

## Installation
Read `demo.html` file and obfuscate it.

##### Before installing, [download NodeJS](https://nodejs.org). v14.18.0 and higher are recommended.
####
```bash
npm i html-obfuscator
```
## Examples
```js
var obfuscate = require('html-obfuscator')

// index.html is <p>Hello World</p>
const { obfuscate } = require('html-obfuscator');
const fs = require('fs');
const source = fs.readFileSync('demo.html');

console.log(obfuscate('index.html'))

// <script>document.write(unescape('%3C%70%3E%48%65%6C%6C%6F%20%57%6F%72%6C%64%3C%2F%70%3E'))</script>
const out = obfuscate(source);
```
NOTE: You have to type a file in the function which you want to obfuscate, you can't put a code inside a function yet.

### Express

```js
var obfuscate = require('html-obfuscator')

// Express
Result:

const express = require('express')
const app = express()

app.get('/', function(req,res){
res.send(obfuscate('index.html')) // Unfortunately, you can't use res.render() yet.
})
app.listen(4000) // Listening on localhost:4000
```html
<script>document.write(unescape('%3C%64%69%76%3E%0A%20%20%20%20%3C%68%31%3E%48%65%6C%6C%6F%20%57%6F%72%6C%64%21%3C%2F%68%31%3E%0A%3C%2F%64%69%76%3E%0A%3C%73%74%79%6C%65%3E%0A%20%20%20%20%64%69%76%20%7B%0A%20%20%20%20%20%20%20%20%64%69%73%70%6C%61%79%3A%20%66%6C%65%78%3B%0A%20%20%20%20%20%20%20%20%6A%75%73%74%69%66%79%2D%63%6F%6E%74%65%6E%74%3A%20%63%65%6E%74%65%72%3B%0A%20%20%20%20%7D%0A%3C%2F%73%74%79%6C%65%3E'))</script>
```
NOTE: It only obfuscates **client-side**, the code in your workspace stays the same.

### fs

```js
var obfuscate = require('html-obfuscator')

// fs
## CLI Usage

var fs = require('fs')
```
html-obfuscator <filepath|text>
```

// Creates new .html file which works the same as index.html but this time it is obfuscated
If the argument is a path and exists, it will be used, otherwise the arguments themselves will be treated as HTML and obfuscated.

fs.writeFileSync('obfuscated.html', obfuscate('index.html'))
```
## License

#### [MIT](https://en.wikipedia.org/wiki/MIT_License#License_terms)

### Inspiration
##### [Inspiration](http://snapbuilder.com/code_snippet_generator/obfuscate_html_source_code/)
#### Made by some random beginner
MIT
12 changes: 12 additions & 0 deletions bin/html-obfuscator
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env node

const { obfuscate } = require('..');
const fs = require('fs');

let src = process.argv.slice(2).join(' ');

if (fs.existsSync(src)) src = fs.readFileSync(src, 'utf8');

process.stdout.write(
obfuscate(src)
);
9 changes: 9 additions & 0 deletions demo/demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div>
<h1>Hello World!</h1>
</div>
<style>
div {
display: flex;
justify-content: center;
}
</style>
9 changes: 9 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const HTMLObfuscator = require('../');
const fs = require('fs');
const path = require('path');

const content = fs.readFileSync(path.join(__dirname, 'demo.html'), 'utf8');

console.log(
HTMLObfuscator.obfuscate(content)
);
67 changes: 0 additions & 67 deletions e.html

This file was deleted.

57 changes: 0 additions & 57 deletions html-obfuscator/main.js

This file was deleted.

27 changes: 0 additions & 27 deletions html-obfuscator/package.json

This file was deleted.

1 change: 0 additions & 1 deletion i.htm

This file was deleted.

3 changes: 0 additions & 3 deletions index copy.js

This file was deleted.

1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function obfuscate(src: string): string;
42 changes: 40 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,40 @@
var o = require('html-obfuscator')
console.log(o('i.htm'))
function convert(num) {
if (num < 10) return num;

switch (num) {
case 10: return 'A';
case 11: return 'B';
case 12: return 'C';
case 13: return 'D';
case 14: return 'E';
case 15: return 'F';
}

return num;
}

function hexFromDec(num) {
if (num > 65_535) throw new Error(`${num} is can't be converted to hex`);
const first = Math.round(num / 4096 - .5);
const temp1 = num - first * 4096;
const second = Math.round(temp1 / 256 - .5);
const temp2 = temp1 - second * 256;
const third = Math.round(temp2 / 16 - .5);
const fourth = temp2 - third * 16;
return '' + convert(third) + convert(fourth);
}

function obfuscate(src) {
let out = '';

for (let i = 0; i < src.length; i++) {
out += '%' + hexFromDec(src.codePointAt(i));
}

// wrap into script tag
out = `<script>document.write(unescape('${out}'))</script>`;

return out;
}

exports.obfuscate = obfuscate;
Loading

0 comments on commit 0c3914a

Please sign in to comment.