connect to gmail in python via imaplib - python

I have a python script that downloads daily attachments from a specific email address from gmail.
Everything works great locally. The problem occurs when I try to upload it to the server.
Code below:
import email
import os
import imaplib
import socket
def download_attachments():
print('Connect with IMAP')
imap_server = imaplib.IMAP4_SSL('imap.gmail.com')
print('Login details')
username = "email#gmail.com"
password = "password"
print('Login to mail')
try:
imap_server.login(username, password)
except imaplib.IMAP4.error as error:
return (f'Error while login: {error}')
after running the code on the server, it shows me in the console
Connect with IMAP
and nothing else happens.
Server details
Production Python scripts server.
Updated : Sat 14 Jan 2023 02:55:01 PM CET Hostname
: xxxxxxx IP address : xxxxxxx System type : Debian
GNU/Linux 11 (bullseye) Load : 0.02 0.06 0.07 1/114
59615 HW resources : 2 CPU | 3931 MB RAM
debug2: client_check_window_change: changed debug2: channel 0: request
window-change confirm 0 debug3: send packet: type 98 Last login: Sat
Jan 14 14:19:25 2023 from xxxxxxx This system is managed by Ansible,
all local changes will be lost.
Production Python scripts server.
Updated : Sat 14 Jan 2023 02:55:01 PM CET Hostname
: xxxxxxx IP address : xxxxxxx System type : Debian
GNU/Linux 11 (bullseye) Load : 0.02 0.06 0.07 1/114
59615 HW resources : 2 CPU | 3931 MB RAM

Related

Connect to FTP server through same network

I'm hosting my FTP through XAMPP, and I'm trying to access the FTP from my other PC, but I cant seem to connect. I wrote a python script, which works on the PC hosting the FTP, but not on my other PC.
# Import Module
import ftplib
# Fill Required Information
HOSTNAME = "10.224.18.9"
USERNAME = "z2067"
PASSWORD = "password"
# Connect FTP Server
ftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD)
# force UTF-8 encoding
ftp_server.encoding = "utf-8"
# Get list of files
ftp_server.dir()
OUTPUT:
drwxr-xr-x 1 ftp ftp 0 Jan 07 2022 36890
drwxr-xr-x 1 ftp ftp 0 Jan 06 2022 37064
drwxr-xr-x 1 ftp ftp 0 Jan 06 2022 37072
-rw-r--r-- 1 ftp ftp 116 Jan 18 2022 desktop.ini
When I run it on my other PC i get the following error:
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I was wondering what settings I have to change on my FTP server hosted by XAMPP, to be able to access this FTP from my other PC?
I have some settings pics here:
It looks like you are using the public IP address for the server. If both devices are in the same network I suggest trying the IP address visible inside that network instead.
If you follow this tutorial: https://lazyadmin.nl/it/ipconfig-command/ you can find out what that IP address is for the computer you are running XAMPP on. It should look something like 192.168.x.x. Try using that in your python code instead '10.224.18.9'.

sslv3 alert bad certificate with mosquitto broker and using tls certificates

I have in a Rpi a mosquitto broker with a server TLS certificate signed by a self-signed CA located in the Rpi. I am trying to connect to this broker from a Parrot virtualbox machine using a python script with the following commands:
TLS_CERT_PATH = "/etc/mosquitto/ca.pem"
client_crt = "/etc/mosquitto/VM.pem"
client_key = "/etc/mosquitto/parrot.key"
client.tls_set(ca_certs=TLS_CERT_PATH, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
client.tls_insecure_set(False)
And the following error appears in the broker:
sslv3 alert bad certificate
And in the virtual machine:
certificate verify failed: IP address mismatch
I don't understand the error because if I run in the Virtual machine the following, where 192.168.1.254 is the IP of the Rpi:
mosquitto_pub -h 192.168.1.254 -p 2259 --tls-version tlsv1.2 --cafile /etc/mosquitto/ca.crt --cert /etc/mosquitto/VM.crt --key /etc/mosquitto/parrot.key -t Injecction_moulding/pressure -q 0 -m trying
It doesn't give me any error, even though I am using the same certificate files.
I though that maybe it was something related to an intermediate certificate signing my Virtual machine's client certificate, but it is issued by the same CA that the broker uses. Moreover, I have also added in /etc/ssl/certs, the certificates that I am using just in case the CA was not recognising them as valid certificates.
Any idea of the error? I would appreciate your help because I have spent two days with this and I'm kind of stuck.
What I have in the broker's certificate is:
Certificate:
Data:
Version: 1 (0x0)
Serial Number:
37:4c:1f:f9:cd:80:c7:f4:82:82:04:69:15:5f:25:de:09:60:ae:b9
Signature Algorithm: sha256WithRSAEncryption
Issuer: C = ES, ST = Galicia, L = Vigo, O = TFG, OU = TFG, CN = tfg, emailAddress = myemail
Validity
Not Before: May 25 17:26:29 2022 GMT
Not After : Jul 9 17:26:29 2023 GMT
Subject: C = UK, ST = Galicia, L = Pontevedra, O = Universidad de Vigo, OU = test, CN = 192.168.1.254, emailAddress = myemail
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public-Key: (2048 bit)
Modulus:
00:b4:85:b7:9b:1e:70:51:9a:0e:af:99:08:26:6f:
55:8e:3a:99:ce:32:c6:8e:23:1b:d4:39:58:75:cc:
c7:9c:5f:3d:a9:f1:f8:85:db:91:9f:25:24:f1:e8:
89:4f:ea:7e:71:23:21:f5:c5:59:77:a6:07:77:53:
e0:9d:12:39:4b:67:b0:10:e0:61:07:d9:fc:fc:f6:
3c:c5:98:f0:70:dd:d1:93:a0:10:64:73:09:8c:63:
de:07:d4:8f:81:18:23:cf:67:60:d6:dd:97:6f:62:
14:ec:0b:20:63:0b:cf:54:ad:78:2d:4d:de:be:29:
01:c1:c1:0f:cb:86:40:e5:2f:0b:29:a3:89:62:03:
6f:f0:ce:b2:43:f6:60:7c:10:46:ea:fd:0e:b4:4b:
ba:64:29:dc:39:50:3d:96:cf:04:c0:43:fa:a6:f3:
bd:e1:da:37:df:19:70:a2:dc:d7:04:2e:2d:b8:27:
97:9c:a3:08:84:be:c6:cf:d6:b6:82:d3:eb:8f:98:
13:eb:39:8b:33:1e:98:72:24:c2:3d:cb:0c:06:3d:
6e:99:fd:a9:f9:02:25:c5:3d:30:76:58:da:33:fa:
6a:33:d1:31:79:15:09:d4:a0:15:df:f2:9a:b9:7e:
68:08:8f:01:73:b7:b5:ac:24:da:bc:0e:d3:0f:b8:
d4:bf
Exponent: 65537 (0x10001)
Signature Algorithm: sha256WithRSAEncryption
25:87:fd:af:41:5a:b2:65:fa:6f:95:12:7f:72:92:ed:ef:8d:
1e:4d:e9:81:ca:75:47:17:a0:b0:0e:20:9e:3e:1a:bf:27:a1:
2f:b4:1f:2b:03:fa:21:ad:2c:da:87:d1:c0:01:12:b4:a0:ce:
29:2e:15:35:50:79:e1:84:3f:05:14:41:f7:e2:17:ee:3c:f7:
6d:32:b7:89:b4:1f:86:e8:89:69:97:a5:33:f3:cd:b5:51:88:
88:41:a5:95:e6:39:44:d4:16:c7:73:19:e1:c5:f1:5e:a7:b3:
a6:d9:9b:16:25:60:6f:ca:3c:0f:1f:85:47:eb:34:24:ad:0b:
74:bb:6b:6f:7a:f1:be:28:4a:fe:6b:55:4b:bd:40:d0:e3:fc:
f4:b7:02:75:86:87:6e:65:f4:91:a4:07:0b:34:a2:f7:88:6a:
3c:d2:67:85:e4:a2:29:5d:02:a3:72:9f:d7:7b:57:8b:c9:ed:
f4:c9:17:00:5b:3d:bc:d4:65:b4:5f:30:12:e1:cb:e2:44:23:
12:05:6f:d4:dd:15:be:56:55:99:d1:7c:f8:8f:34:4e:be:ca:
35:c4:60:03:51:41:ce:98:9a:f4:52:76:b2:69:31:db:3c:1b:
85:b2:04:0b:d8:3d

How to avoid these keepalive#openssh.com log messages when Paramiko Python module is used?

I am using Python Paramiko module to do download files from SFTP server.
But it is displaying the following the log messages continuously.
Wed Dec 20 06:51:00 2017 transport line:1572 22192 /PoolWorker-13 139861396514560 Thread-138 DEBUG Received global request "keepalive#openssh.com"
Wed Dec 20 06:51:00 2017 transport line:1572 22192 /PoolWorker-13 139861396514560 Thread-138 DEBUG Rejecting "keepalive#openssh.com" global request from server.
Wed Dec 20 06:51:00 2017 transport line:1572 22192 /PoolWorker-13 139861395986176 Thread-140 DEBUG Received global request "keepalive#openssh.com"
Wed Dec 20 06:51:00 2017 transport line:1572 22192 /PoolWorker-13 139861395986176 Thread-140 DEBUG Rejecting "keepalive#openssh.com" global request from server.
What is the change required to avoid the above lines to be displayed in logs for Paramiko module? I am using Paramiko 2.1.2 module on Python 2.7.6. Will those log lines will be gone if we use pysftp module instead of Paramiko?
Filter them out using Logger.addFilter:
class KeepalivesFilter (object):
def filter(self, record):
return record.msg.find('keepalive#openssh.com') < 0
paramiko.util.get_logger('paramiko.transport').addFilter(KeepalivesFilter())
(using pysftp won't help as it uses Paramiko internally)

Django configuration to send email on server

I really need your help. About a week I'm trying to configure mail on my server. I use nginx -> uwsgi -> django application.
The problem is that the mail server works only with the following backend:
class SSLEmailBackend(EmailBackend):
def open(self):
if self.connection:
return False
try:
self.connection = smtplib.SMTP_SSL(
self.host, self.port, local_hostname=DNS_NAME.get_fqdn())
if self.username and self.password:
self.connection.ehlo()
# Remove CRAM-MD5 authentication method
self.connection.esmtp_features['auth'] = 'PLAIN LOGIN'
self.connection.login(self.username, self.password)
return True
except:
if not self.fail_silently:
raise
My settings for it:
EMAIL_BACKEND = "my_app.backends.SSLEmailBackend"
EMAIL_HOST = "mail.my_mail_server.com"
EMAIL_PORT = 465
EMAIL_HOST_USER = "host#myhost.com"
EMAIL_HOST_PASSWORD = "my_pass"
DEFAULT_FROM_EMAIL = "host#myhost.com"
Locally, it works completely well!
The strangest thing that a message can be sent without any errors in the server console:
>> from django.core.mail import send_mail
>> send_mail('test email', 'hello world', '', ['my_test#gmail.com'])
The problem comes when I try to register a new user. I use Django-userena backend for registration. Registration is successful, but the emails come only on the server to /var/mail/user1
Text of email:
rom MAILER-DAEMON Sun Oct 26 02:06:01 2014
Return-path: <>
Envelope-to: webmaster#localhost
Delivery-date: Sun, 26 Oct 2014 02:06:01 +0400
Received: from Debian-exim by mail.my_mail_server.com with local (Exim 4.80)
id 1Xi9TF-0002Wp-Ml
for webmaster#localhost; Sun, 26 Oct 2014 02:06:01 +0400
X-Failed-Recipients: test#test.com
Auto-Submitted: auto-replied
From: Mail Delivery System <Mailer-Daemon#elib.rshu.ru>
To: webmaster#localhost
Subject: Mail delivery failed: returning message to sender
Message-Id: <E1Xi9TF-0002Wp-Ml#mail.my_mail_server.com>
Date: Sun, 26 Oct 2014 02:06:01 +0400
Status: O
This message was created automatically by mail delivery software.
A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:
test#test.com
Mailing to remote domains not supported
------ This is a copy of the message, including all the headers. ------
Return-path: <webmaster#localhost>
Received: from localhost ([::1] helo=mail.my_mail_server.com)
by mail.my_mail_server.com with esmtp (Exim 4.80)
(envelope-from <webmaster#localhost>)
id 1Xi9TF-0002Wm-Lv
for test#test.com; Sun, 26 Oct 2014 02:06:01 +0400
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Subject: =?utf-8?b?0KDQtdCz0LjRgdGC0YDQsNGG0LjRjyDQsiDRjdC70LXQutGC0YDQvtC90L0=?=
=?utf-8?b?0L7QuSDQsdC40LHQu9C40L7RgtC10LrQtSDQoNCT0JPQnNCj?=
From: webmaster#localhost
To: test#test.com
Date: Sat, 25 Oct 2014 22:06:01 -0000
....
I've been checking userena, nginx, etc. I can not understand what could be the problem. Why it works locally and on the server console messages can be sent. Please help.
Your mail server is not set up to accept remote mail forwarding. The error message from the server is 'Mailing to remote domains not supported.' So, whatever mail server you are connecting to is not recognizing that your web server is a 'trusted user' who's mail should be accepted and forwarded. I would try to get your mail service provider to whitelist your server for sending email.

Hide my IP address from gmail headers when sending mails via python SMTP library?

I am currently using python SMTP library to send e-mails to certain clients.
I am using the smtp.gmail.com:587 server.
I noticed that in the original headers of any received e-mails sent by this code, the IP address of my own server which sent the e-mail appears.
How can I hide this information ?
Thanks in advance.
server = smtplib.SMTP('smtp.gmail.com', 587)
#server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login("my gmail account", "gmail account passwd")
server.sendmail(source, [destination], message)
server.quit()
Headers received by such an email.
Delivered-To: XXXXXXXXXXX#XXXXXXXXX.com
Received: by 10.52.117.49 with SMTP id kb17csp32285vdb;
Thu, 5 Dec 2013 09:23:19 -0800 (PST)
X-Received: by 10.66.235.106 with SMTP id ul10mr89187198pac.19.1386264198756;
Thu, 05 Dec 2013 09:23:18 -0800 (PST)
Return-Path: <noreply#XXXXXXX.com>
Received: from mail-pd0-f172.google.com (mail-pd0-f172.google.com [209.85.192.172])
by mx.google.com with ESMTPS id v7si58653928pbi.128.2013.12.05.09.23.18
for <XXXXXXXXXXXX#XXXXXXX.com>
(version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
Thu, 05 Dec 2013 09:23:18 -0800 (PST)
Received-SPF: neutral (google.com: 209.85.192.172 is
neither permitted nor denied by
best guess record for domain of
noreply#XXXXXXXXX.com) client-ip=209.85.192.172;
Authentication-Results: mx.google.com;
spf=neutral (google.com: 209.85.192.172 is neither
permitted nor denied by best guess
record for domain of noreply#XXXXXXX.com)
smtp.mail=noreply#XXXXXXXXXXXX.com
Received: by mail-pd0-f172.google.com with SMTP id g10so25045148pdj.31
for <XXXXXXXXXXX#XXXXXXXXX.com>; Thu, 05 Dec 2013 09:23:18 -0800 (PST)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20130820;
h=x-gm-message-state:message-id:date:from:to:cc:subject;
bh=q/9aftTaFh6ryLKbZzIDy6Hfz1R4BSYhG2tvHwxCBLU=;
b=C7Ke+Q6gVau5OxK9BZTuFx2ny0lO35WRjgsWoGyjlbQ0hlTRyQbD18ALNnlbdowUzR
JHO8Smvr2EpgTFQ6h9gsLx6V8fmrfpFNWyQWOFgs6h46d9b1TTW7LWQZfVOIfWD6CfgG
7hUTl7/YFcLbuUQpcOMUDJ/LK7AN4Yp6J6n2nzA6m46QOKKSP7t62OCUTlCd9JoLg4D3
zPkF7oFptlyHWwpZCN5FozbqjuLx6rQfaZpKKMd2q4OXsPd0/CwtOOpBaf1BNVF7HOnD
VJR8YrpFI/gpUOfJJz9R5l8DXE8KAkMCW+10OAupdTzwP9gtSk2coHBA+N05Q2ezzDuK
Np3w==
X-Gm-Message-State: ALoCoQn41Nai7QBm96wqd4aNJPrBfx2AlYr+PlZzQ
wAxujazDPTnRQG80l4v/Oy35W/3ZIz6jCIa
X-Received: by 10.68.66.103 with SMTP id e7mr53292154pbt.120.1386264197850;
Thu, 05 Dec 2013 09:23:17 -0800 (PST)
Return-Path: <noreply#XXXXXXXXX.com>
Received: from [127.0.0.1] ([xx.xxx.xxx.xx])
by mx.google.com with ESMTPSA id er3sm145968195pbb.40.2013.12.05.09.23.15
for <XXXXXXXXXXX#XXXXXXXXXx.com>
(version=TLSv1 cipher=RC4-SHA bits=128/128);
Thu, 05 Dec 2013 09:23:16 -0800 (PST)
Message-ID: <52a0b684.a363440a.5bbc.ffff8e2e#mx.google.com>
Date: Thu, 05 Dec 2013 09:23:16 -0800 (PST)
From: noreply#XXXXXXXXXX.com
To: XXXXXXXX#XXXXXXXXX.com
Cc:
Subject: FTP Credentials
Your FTP account credentials are:
User Name = xxxxxxxxxxx
Password = xxxxxx
Those headers are added by the gmail SMTP server. There is no way to hide them.
Have you tried using VPN service like HIDE-MY-IP. They allow you to use different IP from proxies, and not use your own. This way you mask your IP with another one...
I could swear I just checked and gmail was still forwarding x-originating-ip - if not that's great. In the name pf privacy yeah right its in the name of that's our marketing information but anyways - If they do not forward your IP then you are still at risk of having your IP located with many other simple techniques.
Don't kid yourself - Google does track IP addresses there is simply a reason its not in certain cases based on sender and receiver control/ownership.
From here if you decide its not safe enough to simply trust the next email will not contain x originating or that its not tracked in a easy to retrieve way then you can move up the scale:
Public Proxy Configuration and Testing/Validation Cheap, fast, pretty easy but so many way for this to be useless its barely better than going to the library except that a free proxy outta russia may make your email look suspicious or end up in junkmail and many proxy's proxy your IP as well lol
Paid Premium Proxy Services or "Endpoint Protection" such as Anonymizer, Hushmail (server enforced encryption signing and anonymity) , Symantec, or DELL... From a few dollars a month to buckets of cash as joe trucker and the president are both clients to somebody that offers this service at different scales.
Enter open source peer to peer encrypted onion like puzzles over the Tor protocol. Tor is what makes wikileaks work. It's also the entire darknet and is useful in a context of freedom and privacy. The Tor Browser Bundle (TBB) is ok for short term usage
The only serious solution is Tor on a custom live OS that has all the caveats taken care of.
US DoD Anonymous Live Distribution:
http://spi.dod.mil/lipose.htm
And Whonix is a good implementation of Tor that appears to solve a lot of accidental leakages of IP address since it forces all internet through Tor no matter what. Compare this one to others however its probably sufficient:
https://www.whonix.org/

Categories