Skip to content

Commit

Permalink
fix #5
Browse files Browse the repository at this point in the history
  • Loading branch information
kaliiiiiiiiii committed Sep 22, 2024
1 parent 48c0865 commit 31ce357
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
yolov8m-seg.pt
downloaded_files/*
.idea/*
*/__pycache__/*
*/__pycache__/*
node_modules/*
package-lock.json
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ see [`Event.isTrusted`](https://developer.mozilla.org/en-US/docs/Web/API/Event/i
#### stack.signature
detects injected javascript based on the stack trace in hooks

----
#### pdfStyle
a detection regarding PDF rendering, specific to puppeteer [github issue](https://github.com/kaliiiiiiiiii/brotector/issues/5)

## Contribution
feel free to
- open `[feature request]`s for driver detections
Expand Down
Binary file added assets/test.pdf
Binary file not shown.
30 changes: 28 additions & 2 deletions brotector.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const chromedriverSourceMatches = [

const stackScriptInjectionMatches = {
"pyppeteer":" at [\\s\\S]* \\(__pyppeteer_evaluation_script__:[0-9]+:[0-9]+\\)",
"puppeteer":" at [\\s\\S]* \\(__puppeteer_evaluation_script__:[0-9]+:[0-9]+\\)"
"puppeteer":" at [\\s\\S]* \\(__puppeteer_evaluation_script__:[0-9]+:[0-9]+\\)",
"puppeteer":" at pptr:evaluate;file%3A%2F%2F%2F[\\s\\S]*%3A[0-9]+%3A[0-9]+:[0-9]+:[0-9]+"
}

const hookers = [
Expand Down Expand Up @@ -125,6 +126,7 @@ class Brotector {
this.hook_mouseEvents()
this.hook_canvasVisualize()
this.hook_SeleniumScriptInjection()
await this.test_pdfStyle()

for (const [obj, func] of hookers){
this.hookFunc(obj, func, ()=>{})
Expand Down Expand Up @@ -170,7 +172,7 @@ class Brotector {
for (const line of stack.split("\n")){
for (const [type, regex] of Object.entries(stackScriptInjectionMatches)){
if(line.match(regex)){
this.log({detection:"stack.signature", type:type, score:0.9, data:{stack:stack, hook:hook}})
this.log({detection:"stack.signature", type:type, score:1, data:{stack:stack, hook:hook}})
}
}
}
Expand Down Expand Up @@ -270,6 +272,30 @@ class Brotector {
this.log({"detection":"UA_Override", "type":"HighEntropyValues.empty", score:0.9})
}
}
async test_pdfStyle(){
const iframe = document.createElement("iframe")
iframe.style.height = 0
iframe.style.width = 0
iframe.style.position = "absolute"
iframe.style.x = 0
iframe.style.y = 0
iframe.style.opacity = 0
iframe.src = "assets/test.pdf";
document.body.appendChild(iframe);
const style = await new Promise((resolve, reject)=>{
iframe.onload = ()=>{
try{
if(iframe.contentDocument === null) {console.error("Could not load PDF iframe propperly, possibly running on file: url"); resolve(undefined)}
const result = iframe.contentDocument.querySelector('style')?.textContent||false
document.body.removeChild(iframe)
resolve(result)
}catch(e){reject(e)}
}
})
if(style){
this.log({detection:"pdfStyle",type:"puppeteer", score:0.9, data:{style:style}})
}
}
hook_mouseEvents() {
if (!this._isMouseHooked){
for (let event of ["mousedown", "mouseup", "mousemove", "pointermove", "click", "touchstart", "touchend", "touchmove", "touch", "wheel"]){
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "module",
"dependencies": {
"puppeteer": "^23.4.0",
"static-server": "^3.0.0"
}
}
22 changes: 22 additions & 0 deletions tests_nodejs/test_puppeteer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import puppeteer from 'puppeteer';
import {__server_url__, sleep} from './utils.js'

const script = async ()=>{
await brotector.init_done
return brotector.detections
}

const browser = await puppeteer.launch({ headless: false });
try{
const page = await browser.newPage();

// Navigate the page to a URL.
await page.goto(__server_url__);
await sleep(500)
await page.click("#clickHere")
const detections = await page.evaluate(script)
if(detections.length == 0){throw Error("Not detected")}
console.log(detections)
}finally{
await browser.close();
}
35 changes: 35 additions & 0 deletions tests_nodejs/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import net from "net";
import StaticServer from 'static-server'

const __dirname = dirname(fileURLToPath(import.meta.url));
const __main_dir__ = dirname(__dirname)

function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}

const port = await new Promise( res => {
const srv = net.createServer();
srv.listen(0, () => {
const port = srv.address().port
srv.close((err) => res(port))
});
})

const __server_url__ = `http://localhost:${port}`

const server = new StaticServer({
rootPath: __main_dir__, // required, the root of the server file tree
port: port, // required, the port to listen
host: 'localhost', // optional, defaults to any interface
});

await new Promise((resolve, reject)=>{
try{
server.start(resolve)
}catch(e){reject(e)}
})

export { __main_dir__, __server_url__, sleep}

0 comments on commit 31ce357

Please sign in to comment.