infinite-omicron / pentesting-vm

Penetration Testing Virtual Machine
GNU General Public License v3.0
2 stars 2 forks source link

[Packer] Unable to provision due to permissions error #18

Closed oxr463 closed 2 years ago

oxr463 commented 2 years ago
pentesting-vm.vagrant.alpine: output will be in this color.

==> pentesting-vm.vagrant.alpine: Creating a Vagrantfile in the build directory...
==> pentesting-vm.vagrant.alpine: Adding box using vagrant box add ...
    pentesting-vm.vagrant.alpine: (this can take some time if we need to download the box)
==> pentesting-vm.vagrant.alpine: Calling Vagrant Up (this can take some time)...
==> pentesting-vm.vagrant.alpine: Using SSH communicator to connect: 127.0.0.1
==> pentesting-vm.vagrant.alpine: Waiting for SSH to become available...
==> pentesting-vm.vagrant.alpine: Connected to SSH!
==> pentesting-vm.vagrant.alpine: Provisioning with shell script: scripts/dependencies.sh
==> pentesting-vm.vagrant.alpine: ERROR: Unable to lock database: Permission denied
==> pentesting-vm.vagrant.alpine: ERROR: Failed to open apk database: Permission denied
==> pentesting-vm.vagrant.alpine: /tmp/script_2164.sh: line 34: go: not found
==> pentesting-vm.vagrant.alpine: Provisioning step had errors: Running the cleanup provisioner, if present...
==> pentesting-vm.vagrant.alpine: destroying Vagrant box...
==> pentesting-vm.vagrant.alpine: Deleting output directory...
Build 'pentesting-vm.vagrant.alpine' errored after 1 minute 5 seconds: Script exited with non-zero exit status: 127.Allowed exit codes are: [0]

==> Wait completed after 1 minute 5 seconds

==> Some builds didn't complete successfully and had errors:
--> pentesting-vm.vagrant.alpine: Script exited with non-zero exit status: 127.Allowed exit codes are: [0]

==> Builds finished but no artifacts were created.

Originally posted by @outzhu in https://github.com/infinite-omicron/pentesting-vm/issues/17#issuecomment-1134702705

oxr463 commented 2 years ago

In https://github.com/infinite-omicron/pentesting-vm/blob/master/packer/scripts/dependencies.sh, we are assuming the commands will run as root. However, with the vagrant box, we are running as the vagrant user.

We could either do sudo su before installing the packages, or we could see about modifying the vagrant source to change the user before we provision.

outzhu commented 2 years ago

Does packer run the code as a step-by-step or does it try to run it all?

does this part run after the scripts part?

Do I change Vagrant's user in build.alpine.pkr.hcl or source.vagrant.pkr.hcl?

oxr463 commented 2 years ago

Does packer run the code as a step-by-step or does it try to run it all?

Packer runs each of the scripts, one at a time.

does this part run after the scripts part?

That is the part that runs each of the scripts.

Do I change Vagrant's user in build.alpine.pkr.hcl or source.vagrant.pkr.hcl?

I'm thinking it would be in source.vagrant.pkr.hcl but you'll need to check.

outzhu commented 2 years ago

sorry meant https://github.com/infinite-omicron/pentesting-vm/blob/master/packer/build.alpine.pkr.hcl#L22

does this part run after the scripts part?

oxr463 commented 2 years ago

sorry meant https://github.com/infinite-omicron/pentesting-vm/blob/master/packer/build.alpine.pkr.hcl#L22

The first shell provisioner runs before the second one.

outzhu commented 2 years ago

I added execute_command to the script part execute_command = "echo 'vagrant' | sudo -S -E sh -c '{{ .Vars }} {{ .Path }}'"

Looks like it might have worked but it's taking a while to install go can you try testing it on your end?

My forked

I removed the Docker source since it also has permissions errors (denied connection to Docker daemon socket.) Will work on fixing those after Vagrant is fixed.

oxr463 commented 2 years ago

I removed the Docker source since it also has permissions errors (denied connection to Docker daemon socket.) Will work on fixing those after Vagrant is fixed.

This fix isn't going to work once you put Docker back in because it there is no Vagrant user in the Docker image.

We need to run as root while provisioning Vagrant, like we do with Docker.

outzhu commented 2 years ago

I know but from what I found Packer doesn't have an option to run as root for everything so I'm doing one thing at a time.

outzhu commented 2 years ago

I decided to separate the sources into their own build. Trying to use one build is problematic since I can't find a solution that solves both Vagrant & Docker permission errors.

Testing new file structure, using sudo packer build . to fix Docker.

build {
  name        = "pentesting-vm"
  description = "Penetration Testing Virtual Machine"

  sources = [
    "source.vagrant.alpine"
  ]

  provisioner "shell" {
    scripts = [
      "scripts/dependencies.sh",
      "scripts/apktool.sh",
      "scripts/dex2jar.sh",
      "scripts/jd_cmd.sh",
      "scripts/mobsf.sh"
      "scripts/theharvester.sh",
      "scripts/zaproxy.sh"
    ]
    execute_command = "echo 'vagrant' | sudo -S -E sh -c '{{ .Vars }} {{ .Path }}'"
  }

  provisioner "shell" {
    /* Fix permissions */
    inline = ["chown -R root:root /opt"]
  }
}

build {
  name        = "pentesting-vm"
  description = "Penetration Testing Virtual Machine"

  sources = [
    "source.docker.alpine"
  ]

  provisioner "shell" {
    scripts = [
      "scripts/dependencies.sh",
      "scripts/apktool.sh",
      "scripts/dex2jar.sh",
      "scripts/jd_cmd.sh",
      "scripts/mobsf.sh",
      "scripts/theharvester.sh",
      "scripts/zaproxy.sh"
    ]
  }

  provisioner "shell" {
    /* Fix permissions */
    inline = ["chown -R root:root /opt"]
  }
}

Look likes the errors are now from the scripts which have their own open issues to solve.

oxr463 commented 2 years ago

That's awesome! Can you create a PR? Also, should we change the names of the buillds, e.g., pentesting-docker and pentesting-vagrant?