USB drive "outruns" python script - python

I am looking to create a usb drive that when inserted into a laptop will automatically run a python file (without any user interaction) which grabs their ip address and hostname and emails it to us in a text file. I know this seems sketchy, but it is for a pen test (I'm sure all of you are familiar with these kinds of tests)
I have the script already written, I just need to find a way to automatically run the script (regardless of what op system their running when somebody is dumb enough to put a usb drive into their computer... I have scavenged the web and found the "outrun.inf" ideas, but I know this does not work with all op systems...
My idea is to have it run command line commands when plugged into the laptop (which i can figure out later which command lines will work), but I am unsure if this is possible.. any ideas?
If their is another way you guys think could work to achieve what I am looking for, I am all ears!
Thanks all

Related

Crontab for Script in venv

My python environment and the script I want to run are located at:
/home/pi/webscrap/bin
/home/pi/webscrap/news/news.py
In the news directory I created a newscrap.sh file containing this:
/home/pi/webscrap/bin/python /home/pi/webscrap/news/news.py
I did this just with nano newscrap.sh.
then I created a crontab and added the line:
* * * * * /home/pi/webscrap/news/newscrap.sh >> /home/mypc/logs/cronlogs.log 2>&1
I checked this by looking at the size of some files that should change when I run the script. This does not happen, so what am I missing here?
Edit: I also did
sudo chmod a+x news.py
Is the destination /home/mypc/logs/cronlogs.log on the raspberry pi or is it on a different system? In other words, if you type ls /home/mypc from the command line on the raspberry pi, does that folder exist?
I'm going to answer as though your intent is to copy files from one system (the raspberry pi) to another (mypc), but you may be confusing filesystem paths with network locations. If that's the case, then there are several good options available to you. I'll give you a quick summary of two.
Solution 1: Use scp
Write to a file on the raspberry pi.
Use scp to copy the file to a location on mypc.
You will want to set up SSH keys so that it does not prompt you for a password each time.
This won't append to a file on the mypc, it will copy the whole thing, so you will have to come up with a solution to that problem. This solution feels a little hacky to me, and I don't love it.
Solution 2: Use a queueing system
This is a lot more involved, but much cleaner than scp, because it was designed for exactly this. You'll have to learn about networking and sockets, but you like that sort of thing or you wouldn't be here. I like ZMQ because it is easy to get started and is well supported. You will have to write a server on the raspberry pi and a client on mypc. This can be a bit tricky if there are firewalls, but there is help for that. I would start with client/server on one machine, and then figure out how to get it across the network.

Running python in QwikLabs Linux VM through Windows PuTTY doesn't work but through mac it does. Is there a way to make it work on Windows?

I'm taking this course which requires us to ssh into a Qwiklabs Linux VM and write a python script to manipulate files and stuff on the VM. When I try ssh using PuTTY in windows 10, everything goes fine up to the point I have to run the script, then it returns invalid syntax errors. As far as I know, nothing is wrong with the code. The images linked will also show that the syntaxerrors point to stuff like valid variable names and even colons after for loops. I tested the code by trying to ssh into the VM through a mac and the script worked. Is there any way to fix this and make it run on Windows? Switching back and forth between computers is less than ideal for me and I also find it really intriguing because as far as I'm aware, it shouldn't matter which platform I'm connecting to the VM from because the python script is running on the VM. Hope someone can help, I'm a beginner and am really new to coding and ssh.python error img. Another python error img
There is nothing wrong with the VM. It is a problem with the code. From what I see in your error there is probably a unclosed parentheses above that line causing it to fail. I would need to see the code for more information.

How can I sniff packets using Scapy without any CMD opened?

I'm trying to write a script to sniff some packets and send others according to the sniff.
I also want that script to be completely invisible to the user (so it could only be seen from Task Manager).
I tried changing the script extension to .pyw so it would run with pythonw.exe, and it kind of works. However a PowerShell window opens up and when closed, shuts off the pythonw process as well.
I read some on the web and understood Scapy creates it in order to get access to low level networking requests. I also found code to change it into a regular CMD. However I don't want it to change to another shell type, I want this shell to disappear completely so there's no indication om the desktop that the script is running.
How do I achieve this? Is there a way to request scapy to create this shell as a background process? Or is there a way to move an already running CMD to the background?
Thanks in advance!
PS:
The code to change the PowerShell to CMD is:
POWERSHELL_PROCESS.close()
conf.prog.powershell = None
POWERSHELL_PROCESS.__init__()
After the import, and was found here.
I don’t really understand what you are trying to do
This was a well-known bug in scapy, that opened in some IDE (e.g IDLE), the powershell would be visible. As shown in the link you provided, it has been fixed https://github.com/secdev/scapy/issues/1387 so that the shell now is invisible.
If this is your issue, use the latest github version https://github.com/secdev/scapy/archive/master.zip

How do I access a remote filesystem using Python on Windows?

I'm writing a Python script to access all computers on the network, log in to them and read some log files. I don't want to use something as low-level as socket, but I can if I must. I realize that my problem is similar to this question, but not the same.
Are there any modules for accessing external Windows machines?
Has anyone done anything like this before?
I'm specifically looking to log into Windows 7 machines, not unix.
Let's also assume that each computer I want to log into has Remote Desktop installed and enabled. I'm also not worried about network security or encryption because these files are not confidential. Windows machines don't have SSH installed on the by default do they?
There has to be something on the other side for you to talk to. This limits you to either setting up a "server" on each machine, installing a real server (i.e. sshd), building a "server" yourself and installing it, or using a built in and active feature of the OS.
Based upon this, what kind of system do you want to set up on these machines? What does it need to do? Just read the contents of a prespecified file list? Will that list change?
One solution is to turn on telnet, and use paramiko or twisted to
talk across it. This isn't very secure of course
Next up, set up a samba share, and access the folder remotely. This
is also insecure, though less so than telnet
You could find a ssh daemon port and run that, if you are so inclined
Psexec from sysinternals might work
Use twisted to build a server app with the features you need
Use ncat to listen on a port and spawn a cmd prompt
Be aware that most of the solutions for accessing windows remotely are... poor. The best solution is probably to roll your own, but that is hard work and you will probably make mistakes.
Also, Windows 7 is not exactly multi-user friendly. Individual processes can run as separate users, but the OS does not support having multiple users logged in at the same time. Someone is going to be the "user" and everyone else is just a process with a different credential set.
This is more an artificial limitation on M$'s part than anything technical. To see this in action, try to log in with RDP while a user is logged in locally. Fun times.
Per your edit, the easiest thing to do is just set up a samba share on the box.
After this share is set up:
with open(r'\\myCompNameOrIP\C\windows\logs\logfile.txt','rb') as logfile:
loglines = logfile.readlines()
Or you can use the gencat sample found here. Just give it r'\\myCompNameOrIP\C\windows\logs\*.txt' as the search path and watch the magic.
From Ubuntu I use samba:
In Bash:
gvfs-mount smb://them/folder
Here I give name, domain and password
Then in python:
folder = '/home/me/.gvfs/folder on them'
using the os module I read folders and files inside.
I am working in a small business environment.
Why not have each of the computers send the log file to the central computer?

How do I schedule a Python script to run as long as Windows XP is running?

I wrote a temperature logger Python script and entered it as a scheduled task in Windows XP. It has the following command line:
C:\Python26\pythonw.exe "C:\path\to\templogger.py"
It writes data to a file in local public folder (e.g. fully accessible by all who login locally).
So far, I was able to achieve this objective:
1. Get the task to run even before anyone logs in (i.e. at the "Press Ctrl+Alt+Del" screen)
But I'm having problems with these:
1. When I log in, log out, then log back in, the scheduled task is no longer active. I can no longer see it in the Task Manager's Processes tab. I suspect it closes when I log out.
2. I tried to set the task's "Run As..." property to DOMAIN\my-username and also tried SYSTEM, but problem #1 above still persists.
SUMMARY:
I want my program to be running as long as Windows is active. It should not matter whether anyone has logged in or out.
P.S.
I asked the same question in Super User, and I was advised to write it as a service, which I know nothing about (except starting and stopping them). I hope to reach a wider audience here at SO.
Is it possible to run a Python script as a service in Windows? If possible, how?
http://agiletesting.blogspot.com/2005/09/running-python-script-as-windows.html
Your scenario is exactly the required use case for a service, unfortunately tasks are ill suited for what you are looking to do. That said writing services in python is not a walk in the park either, to ease the pain here is a few links I have perused in the past:
http://agiletesting.blogspot.com/2005/09/running-python-script-as-windows.html
http://mail.python.org/pipermail/python-win32/2008-April/007298.html
I used the second link in particular to create a windows scripts that was then compiled to a executable service with py2exe and installed with SrvAny.

Categories