Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for code templates #2930

Merged
merged 61 commits into from
Jun 9, 2023
Merged

Adding support for code templates #2930

merged 61 commits into from
Jun 9, 2023

Conversation

Mzack9999
Copy link
Member

@Mzack9999 Mzack9999 commented Nov 30, 2022

Proposed changes

This PR adds support for code templates. Uses stdin and stdout for in/out operation within the script context. The engine accepts two options:

  • engine: any interpreter installed on the target machine (py, bash, powershell)
  • source: code snippet or existing source file

Notes:

  • Code templates requires a valid signature in order to be executed
  • This PR also disables file, offlinehttp and code protocols when cloud is enabled
  • The implementation is an experimental prototype (not optimized for performances and not supporting additional functionalities already available or in todo for gozero, such as sandbox or virtual environment)
  • tests and docs will be extended and updated after pre-review

Checklist

  • Pull request is created against the dev branch
  • All checks passed (lint, unit/integration/regression tests etc.) with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate) => Adding code template docs nuclei-docs#149

Example

# Print the template content
$ cat test.yaml
id: testcode

info:
  name: testcode
  author: testcode
  severity: info
  tags: code
  description: |
    testcode

code:
  - engine:
      - py
      - python3
    # Code Snippet
    source: |
      import sys
      print("hello " + sys.stdin.read() + ' ' + os.getenv('test'))
    # Code File
    # source: test.py

    matchers:
      - type: word
        name: testname
        words:
          - "hello"
    extractors:
      - type: dsl
        name: pyout # pyout will contain "hello test testvar
        dsl:
          - body
# Execute unsigned template => Rejected
$ echo "test" | ./nuclei -t test.yaml -v -debug -duc -no-interactsh -V test=testvar
...
[WRN] The template is not verified: 'C:\Users\marco\go\src\github.com\projectdiscovery\nuclei\v2\cmd\nuclei\testcode.yaml'
...
[FTL] Could not run nuclei: no valid templates were found
exit status 1
# Generate private key - go prefers prime256v1 (Ref: https://groups.google.com/g/Golang-nuts/c/Mbkug5t3ZYA)
$ openssl ecparam -name prime256v1 -genkey -noout -out priv-key.pem
# Extract public key
$ openssl ec -in priv-key.pem -pubout > pub-key.pem
read EC key
writing EC key

# Sign the template
$ cd v2/cmd/sign-templates
$ ./sign-templates -t ..\nuclei\test.yaml -prk ..\nuclei\priv-key.pem -puk ..\nuclei\pub-key.pem -a ecdsa

# Configure ENV variables - windows (NUCLEI_SIGNATURE_PUBLIC_KEY and NUCLEI_SIGNATURE_ALGORITHM)
# export NUCLEI_SIGNATURE_PUBLIC_KEY='C:\Users\marco\go\src\github.com\projectdiscovery\nuclei\v2\cmd\nuclei\pub-key.pem' # unix
$ $env:NUCLEI_SIGNATURE_PUBLIC_KEY='C:\Users\marco\go\src\github.com\projectdiscovery\nuclei\v2\cmd\nuclei\pub-key.pem' # windows           
# export NUCLEI_SIGNATURE_ALGORITHM='ecdsa' # unix
$ $env:NUCLEI_SIGNATURE_ALGORITHM='ecdsa' # windows                                
# Execute the signed template
$ echo "test" | ./nuclei -t test.yaml -v -debug -duc -no-interactsh -V test=testvar
...
[DBG] [testcode] Dumped Code Execution for test
hello test testvar
[testcode:testname] [code] [info] test [hello test testvar]

@Mzack9999 Mzack9999 added the Type: Enhancement Most issues will probably ask for additions or changes. label Nov 30, 2022
@Mzack9999 Mzack9999 self-assigned this Nov 30, 2022
@Mzack9999 Mzack9999 marked this pull request as draft November 30, 2022 20:11
v2/cmd/nuclei/main.go Outdated Show resolved Hide resolved
v2/pkg/protocols/code/code.go Outdated Show resolved Hide resolved
v2/pkg/protocols/code/engines.go Outdated Show resolved Hide resolved
@tarunKoyalwar
Copy link
Member

@Mzack9999

I was thinking it would be better if we only expose gozero.Gozero and gozero.Source. and dissolve gozero.command and let it be handled by gozero.Gozero (with sh as engine ) . Its early we would possibly need to change many things in gozero
ex:

  1. context with timeout
  2. support for bash script
  3. temp file permissions

I think we will face more problems with sh or bash . if we do not expose gozero.command . there will be less changes in nuclei and even if we improve gozero and it will be a black box .

@tarunKoyalwar
Copy link
Member

Also we already have logic about sh and ps that we implemented earlier in executils .

https://github.com/projectdiscovery/utils/blob/471a54f0a0712b263dea919b4c7388cbfcf88cf5/exec/executil.go#L176-L253

Note:
we might need to update them with regard to stdin and stdout

@JaneX8
Copy link
Contributor

JaneX8 commented Dec 8, 2022

I just want to mention that including support for specific 3rd party tools is a great idea when they get specific implementations. But using code directly inside a YAML file in my opinion opens up so many risks that's just far from it's indented use, of both YAML-format and nuclei. If someone really wants to do something like this I would recommend just creating a bash script and supporting running bash scripts with some parameters instead.

Base automatically changed from dev to main May 3, 2023 20:26
@ehsandeep ehsandeep changed the base branch from main to dev May 3, 2023 20:27
@Mzack9999
Copy link
Member Author

Mzack9999 commented May 23, 2023

Notes:

  • Interactsh urls placeholders suffers of a per template-protocol scope, alas, the logic is not centralized but scattered in different incompatible implementations across major protocols, and fully absent in others (such as whois, dns, etc). As a result implementing interactsh url in code templates has such urls accessible only within their own scope due to various abstraction layers. In short referring to the same variable in multiple protocol will create new interactsh url in their own layered implementation as there is no concept of template context execution. It would be necessary to refactor the core to transport these urls through metainput.
  • Embedding -sign via env variables is not recommended, and generally it's use discouraged as it potentially enlarges attack surface

@Mzack9999 Mzack9999 requested a review from ehsandeep May 23, 2023 09:26
Copy link
Member

@ehsandeep ehsandeep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Adding default algorithm to sign (rsa)
  • Adding docs to nuclei-docs for code protocol and template sign option

@Mzack9999 Mzack9999 requested a review from ehsandeep May 30, 2023 09:16
@ehsandeep ehsandeep added this to the nuclei v3 (beta) milestone Jun 6, 2023
@ehsandeep ehsandeep added the Status: Review Needed The issue has a PR attached to it which needs to be reviewed label Jun 7, 2023
@ehsandeep ehsandeep changed the base branch from dev to v3-beta June 9, 2023 00:09
Copy link
Member

@ehsandeep ehsandeep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mzack9999 Merge conflict ^

ShubhamRasal and others added 6 commits June 9, 2023 05:50
* Basic headless fuzzing

* Remove debug statements

* Add integration tests

* Update template

* Fix recognize payload value in matcher

* Update tempalte

* use req.SetURL()

---------

Co-authored-by: Tarun Koyalwar <tarun@projectdiscovery.io>
* add headless header and status matchers

* rename headers as header

* add integration test for header+status

* fix typo
@ehsandeep ehsandeep removed the Status: Review Needed The issue has a PR attached to it which needs to be reviewed label Jun 9, 2023
@ehsandeep ehsandeep merged commit a7fb15d into v3-beta Jun 9, 2023
11 checks passed
@ehsandeep ehsandeep deleted the issue-549-gozero branch June 9, 2023 15:24
@ehsandeep ehsandeep removed this from the nuclei v3 (beta) milestone Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Most issues will probably ask for additions or changes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement code protocol to run system cmd / tool into nuclei
7 participants