Use dkimpy from command line - python

How to use this python library from the command line?
https://pypi.python.org/pypi/dkimpy
I have a raw email message I want to validate the DKIM signature.. But how.. Can't find any docs about the usage
The raw message is a string/stream

First I needed a test message, this is how I got one:
Opened Gmail and selected an email.
Clicked the vertical 3 dots on the top right side of the message view
Selected Show original
Clicked Download Original and saved it to ~/original_msg.txt
I then opened up a terminal window and made sure I was in my home directory:
$ cd ~
Then I installed dkimpy:
$ pip install dkimpy
Annoyingly, dkimverify --help didn't work, but man came to the rescue:
$ man dkimverify
dkimverify(1)
NAME
dkimverify - Script for DKIM verifying messages on stdin
DESCRIPTION
dkimverify reads an RFC822 message on standard input, and returns with exit code 0
if the signature verifies successfully. Otherwise, it returns with exit code 1.
so I verified my downloaded Gmail message:
$ cat original_msg.txt | dkimverify
signature ok
$ echo $?
0
And just to make sure it failed as expected, I created a bogus message:
$ echo "bogus-header:bogus email" > bogus_msg.txt
And then tried to validate it:
$ cat bogus_msg.txt | dkimverify
signature verification failed
$ echo $?
1

Related

Run local python script on remote server thanks to Posh-SSH module (W10)

I want to run local python script on remote server by using Posh-SSH which is a module in Powerhsell.
This topic mention it with regular ssh by doing this:
Get-Content hello.py | ssh user#192.168.1.101 python -
I would like here to use Posh-SSH module but I don't get how to achieve it...
I tried sth like this but it doesn't work
Invoke-SSHCommand -Index $sessionid.sessionid -Command "$(Get-Content hello.py) | python3 -u -" -ErrorAction Stop
EDIT
No errors shown, only stuck on it and and do nothing...
EDIT2
Ok I get it now why I have this error when I send a multiline py file.
New lines are not retranscripted, look what command will be send to the remote server :
python3 -u - <<"--ENDOFPYTHON--"
chevre="basic" print("Hello, World!")
--ENDOFPYTHON--
and not:
python3 -u - <<"--ENDOFPYTHON--"
chevre="basic"
print("Hello, World!")
--ENDOFPYTHON--
EDIT3
And finally Done !
Thanks to this topic I performed to change the whitespace to newline as it should be.
For doing this just to this
( (Get-Content hello.py) -join "`r`n")
instead of simple
$(Get-Content hello.py)
Finally the line will be :
$cmd = "python3 -u - <<`"--ENDOFPYTHON--`"`n$((Get-Content $pyscript) -join "`r`n")`n--ENDOFPYTHON--"
Invoke-SSHCommand -Index $sessionid.sessionid -Command $cmd -ErrorAction Stop
Also don't forget to remove the line
#!/usr/bin/env python3
if present on top of your py file, otherwise it won't work.
You are currently sending the following to the SSH endpoint:
# Example Python Added: would be replaced with your code
print("Hello, World!") | python3 -u -
Assuming bash as the endpoint shell, the above is invalid and would either produce an error or hang.
You need to encapsulate the code being sent to the server using echo (assuming bash ssh endpoint).
Invoke-SSHCommand -Index $sessionid.sessionid -Command "echo '$((Get-Content hello.py) -join "`r`n")' | python3 -u -" -ErrorAction Stop
The above will now send:
echo 'print("Hello, World!")' | python3 -u -
This works as long as you don't use single quotes. However, if you must use those or other special characters, you may need to use a here document instead:
Invoke-SSHCommand -Index $sessionid.sessionid -Command "python3 -u - <<`"--ENDOFPYTHON--`"`n$((Get-Content hello.py) -join "`r`n")`n--ENDOFPYTHON--" -ErrorAction Stop
The here document will send exactly what it is to the standard input stream of the program: tabs, spaces, quotes and all. Therefore the above will send the following:
python3 -u - <<"--ENDOFPYTHON--"
print("Hello, World!")
--ENDOFPYTHON--
You can replace --ENDOFPYTHON-- with anything as long as it does not appear in your python file.
Reference for here docs
UPDATE:
Added -join "`r`n" as it is needed for newlines to be sent correctly, as pointed out by the asker.

Fabric sudo() not respecting env.password

I'm trying to prefill env.password using --initial-password-prompt, but remote is throwing back some strangeness. Let's say that I'm trying to cat a root-owned file as testuser, with 600 permissions on the file. I'm calling sudo('cat /home/testuser/test.txt'), and getting this back:
[testuser#testserver] sudo: cat /home/testuser/test.txt
[testuser#testserver] out: cat: /home/testuser/test.txt: Permission denied
[testuser#testserver] out:
Fatal error: sudo() received nonzero return code 1 while executing!
Requested: cat /home/testuser/test.txt
Executed: sudo -S -p 'sudo password:' -u "testuser" /bin/bash -l -c "cat /home/testuser/test.txt"
Is that piping the prompt right back into the input? I tried using sudo() with pty=False to see if it was an issue with the pseudoterminal, but to no avail.
Here's the weird part: calling run('sudo cat /home/testuser/test.txt') and invoking fab without --initial-password-prompt passes back a password prompt from remote, and on entering the password, everything works fine.
Naturally, running ssh -t testuser#testserver 'sudo cat /home/user/test.txt' prompts for a password and returns the contents of the file correctly. Do I have an issue with my server's shell config, or is the issue with how I'm using sudo()?
Down the line, I'm likely to set up a deploy user with no-password sudo and restricted commands. That'll probably moot the issue, but I'd like to figure this one out if possible. I'm running an Ubuntu 14.10 VPS, in case that's relevant.
Oh, my mistake. I had foolishly set env.sudo_user to my deploy user testuser, thinking that it was specifying the invoking user on remote. In fact, it was specifying the target user, and I was attempting to sudo into myself. Whoops.

Python script not waiting for user input when ran from piped bash script

I am building an interactive installer using a nifty command line:
curl -L http://install.example.com | bash
The bash script then rapidly delegates to a python script:
# file: install.sh
[...]
echo "-=- Welcome -=-"
[...]
/usr/bin/env python3 deploy_p3k.py
And the python script itself prompts the user for input:
# file: deploy_py3k.py
[...]
input('====> Confirm or enter installation directory [/srv/vhosts/project]: ')
[...]
input('====> Confirm installation [y/n]: ')
[...]
PROBLEM: Because the python script is ran from a bash script itself being piped from curl, when the prompt comes up, it is automatically "skipped" and everything ends like so:
$ curl -L http://install.example.com | bash
-=- Welcome ! -=-
We have detected you have python3 installed.
====> Confirm or enter installation directory [/srv/vhosts/project]: ====> Confirm installation [y/n]: Installation aborted.
As you can see, the script doesn't wait for user input, because of the pipe which ties the input to the curl output. Thus, we have the following problem:
curl [STDOUT]=>[STDIN] BASH (which executes python script)
= the [STDIN] of the python script is the [STDOUT] of curl (which contains at a EOF) !
How can I keep this very useful and short command line (curl -L http://install.example.com | bash) and still be able to prompt the user for input ? I should somehow detach the stdin of python from curl but I didn't find how to do it.
Thanks very much for your help !
Things I have also tried:
Starting the python script in a subshell: $(/usr/bin/env python3 deploy.py)
You can always redirect standard input from the controlling tty, assuming there is one:
/usr/bin/env python3 deploy_p3k.py < /dev/tty
or
/usr/bin/env python3 deploy_p3k.py <&1

Yowsup connection to whatsapp raise Disconnected because close exception

I have installed the yowsub library (the latest version ) on my device ( Ubuntu 12.04 )
also I have obtained the request code by running this command
$ ./yowsup-cli --requestcode sms --config yowsup-cli.config
and then I registered it successfully using this command
$ ./yowsup-cli --register {MY-CODE} --config yowsup-cli.config
after that I entered the returned password in the configuration file.
but when I try to send messages by this command
$ ./yowsup-cli --send {Destination phone with it's country code} "Test message" --wait --config yowsup-cli.config
I have the following exception
Disconnected because close
I tried more than solution related to this issue but the result was the same exception.
I have solved the issue by downloading a new fork of the yowsube library from this link
http://mandroslabs.com/mandrosian/
and it's now working good.

Yowsup WhatsApp get phone number

I'm trying to understand how to use the Yowsup library for WhatsApp. I can send message and I can receive it, but I'm interested to get the phone number to start a new chat.
In other words, will develop a computer app that can interact with WhatsApp users, for now I can do the following:
I got the access to WhatsApp server by using this command: python yowsup-cli -c config.example --requestcode sms and python yowsup-cli -c config.example --register xxx-xxx
I send message by using this command: python yowsup-cli -c config.example -s 39xxxxxxxxxx "!"
I can have an interactive conversation by using this command: python yowsup-cli -c config.example -i 39xxxxxxxxxx
Get all message I received by using this command: python yowsup-cli -c config.example -l
Now when an user send me a message how I can interact with him/her? I guess I should get the phone number form the command python yowsup-cli -c config.example -l and begin a new interactive conversation with this command: python yowsup-cli -c config.example -i 39xxxxxxxxxx in which the 39xxxxxxxxxx is the number of the user I get with the previous command.
I hope you can help me
I don't think you want to be using yowsup-cli for development purposes. I think it's intended to be a simple demo client with very limited functionality.
If you look at the yowsup-cli source code you will see it actually imports the included examples to provide the command line message functionality.
What you see inside this code is that your python yowsup-cli -c config.example -l actually calls
wa = WhatsappListenerClient(args['keepalive'], args['autoack'])
wa.login(login, password)
This example listener client on the other hand has a callback function registered to the message_received signal.
self.signalsInterface.registerListener("message_received", self.onMessageReceived)
Now if you take a closer look at this function
def onMessageReceived(self, messageId, jid, messageContent, timestamp, wantsReceipt, pushName, isBroadCast):
formattedDate = datetime.datetime.fromtimestamp(timestamp).strftime('%d-%m-%Y %H:%M')
print("%s [%s]:%s"%(jid, formattedDate, messageContent))
if wantsReceipt and self.sendReceipts:
self.methodsInterface.call("message_ack", (jid, messageId))
You can se that the jid and therefore the phone number which you say you need is on the parameter list of this signal. If you wish to interact with a user after he has sent you a message my guess would be you should store the jid or phone number in your own subscriber to this signal.
In short - don't use the the yowsup-cli per se for development. Use it as a starting point to build your own app. Good luck!

Categories