Pages

Showing posts with label win7. Show all posts
Showing posts with label win7. Show all posts

Tuesday, March 31, 2015

Compiling and using PyNaCl on Windows 7

Step-by-step how to compile PyNaCl Python package on win32.

Requirements:
Python 2.7.x for Windows - download it from here.
libsodium-1.0.2-msvc - download precompiled binaries from here.
Microsoft Visual C++ Compiler for Python 2.7 - you can download MS C++ compiler for Pyhton from here.

This guide is based on https://github.com/pyca/pynacl/issues/100 and and is a focused reiteration of it.

Python for Windows is compiled with MSVC and because of that it is not possible to compile extensions with MinGW/MSYS. The main problem is with CFFI (Common Foreign Function Interface) and most probably if you succeed with compiling, the lib will not work (will hang if trying to use its functions)

1. Download and install Python for Windows

2. Download and install Microsoft Visual C++ Compiler for Python 2.7 

3. Install python setuptools (needed for MSVC++ Python) with pip:
C:> pip install setuptools
4. Download libsodium latest release with -msvc at the end. Unzip it in C:\work\libsodium-1.0.2-msvc
5. Go to C:\work\libsodium-1.0.2-msvc\Win32\Release\v120\dynamic and rename libsodium.lib to sodium.lib. On 64bit Windows use this dir: C:\work\libsodium-1.0.2-msvc\x64\Release\v120\dynamic

6. Download PyNaCl source and unzip it to C:\work\PyNaCl-0.3.0

7. Start the MSVC++ Python shell: Start -> All Programs -> Microsoft Visual C++ Compiler Package for Python 2.7 -> Visual C++ 2008 32-bit Command Prompt

8. Set these 3 variables in cmd prompt:
set INCLUDE=%INCLUDE%C:\work\libsodium-1.0.2-msvc\include
set LIB=%LIB%C:\work\libsodium-1.0.2-msvc\Win32\Release\v120\dynamic
set SODIUM_INSTALL=system


on 64bit Windows change:
set LIB=%LIB%C:\work\libsodium-1.0.2-msvc\Win32\Release\v120\dynamic
to:
set LIB=%LIB%C:\work\libsodium-1.0.2-msvc\x64\Release\v120\dynamic
9. Probably you will need these two files when compiling:
Download and copy them to: C:\work\libsodium-1.0.2-msvc\include

10. Start the building process:
C:\>cd C:\work\PyNaCl-0.3.0
C:\work\PyNaCl-0.3.0>python setup.py build
11. If everything is ok, then install it:
C:\work\PyNaCl-0.3.0>python setup.py install
12. Finally, copy the original libsodium.dll in PyNaCl install dir:
C:>copy C:\work\libsodium-1.0.2-msvc\Win32\Release\v120\dynamic\libsodium.dll C:\Python27\Lib\site-packages\PyNaCl-0.3.0-py2.7-win32.egg\nacl\_lib
Here is a test program (from doc examples https://pynacl.readthedocs.org/en/latest/public/) slightly modified:
import nacl.utils
from
nacl.public import PrivateKey, Box


skbob = PrivateKey.generate()
pkbob = skbob.public_key  
skalice = PrivateKey.generate()
pkalice = skalice.public_key 
bob_box = Box(skbob, pkalice)
message = b"Kill all humans"
nonce = nacl.utils.random(Box.NONCE_SIZE)

encrypted = bob_box.encrypt(message, nonce)
print "Encrypted Message:", encrypted
alice_box = Box(skalice, pkbob)

plaintext = alice_box.decrypt(encrypted)
print "Plaintext Message:", plaintext

If it works, you should see something like:
C:\work>python nacltest.py
Encrypted Message: ₧╫fαIé├l(α▀W¬½♥↔≈‼╟  üRδD≈é☻'^∞v√oòΣls╣8,ƒ   ↓ü↓╓+ô╓è╣=§╣
Plaintext Message: Kill all humans
C:\work>

Thursday, March 22, 2012

Windows DNS tools и как да ги използваме.

DNS тип заявки
А - на кое IP отговаря www.google.com.
AAAA - IPv6 версията на A.
MX - кои сървъри отговарят за e-mail-ите на google.com.
NS - кой сървъри отговарят за DNS записите на google.com.
SOA - Start of Authorite - пъртият DNS сървър отговарящ за зоната google.com.
PTR - обратен запис - на кой хост отговаря 195.177.249.170
TXT - текстов запис, може да се ползва за всичко, най-използвани са SPF TXT записи за емейл SPAM защита.

Windows nslookup.exe (start->run->cmd.exe)

Nslookup е инструмент за диагностика на DNS сървър. Освен основното което всеки администратор е ползвал C:>nslookup www.dir.bg, програмката предлага и други опции които са полезни за диагностика. Без допълнително опции nslookup изпраща запитване за A запис.

Какъв е SOA записът за зоната google.com?
C:\>nslookup -type=soa google.com
Server:  UnKnown
Address:  10.1.42.1

Non-authoritative answer:
google.com
        primary name server = ns1.google.com
        responsible mail addr = dns-admin.google.com (dns-admin@google.com)
        serial  = 2012010300
        refresh = 7200 (2 hours)
        retry   = 1800 (30 mins)
        expire  = 1209600 (14 days)
        default TTL = 300 (5 mins)

Кои DNS сървъри отговарят за зоната google.com?

C:\>nslookup -type=ns google.com
Server:  UnKnown
Address:  10.1.42.1

Non-authoritative answer:
google.com      nameserver = ns3.google.com
google.com      nameserver = ns1.google.com
google.com      nameserver = ns2.google.com
google.com      nameserver = ns4.google.com
Кой сървъри отговарят за емейла @google.com?
C:\>nslookup -type=mx google.com
Server:  UnKnown
Address:  10.1.42.1

Non-authoritative answer:
google.com      MX preference = 30, mail exchanger = alt2.aspmx.l.google.com
google.com      MX preference = 50, mail exchanger = alt4.aspmx.l.google.com
google.com      MX preference = 10, mail exchanger = aspmx.l.google.com
google.com      MX preference = 40, mail exchanger = alt3.aspmx.l.google.com
google.com      MX preference = 20, mail exchanger = alt1.aspmx.l.google.com

На кой host отговаря 216.239.32.10?
C:\>nslookup -type=ptr 216.239.32.10
Server:  UnKnown
Address:  10.1.42.1

Non-authoritative answer:
10.32.239.216.in-addr.arpa      name = ns1.google.com
IPv6 запис за google.com?
C:\>nslookup -type=aaaa google.com
Server:  UnKnown
Address:  10.1.42.1

*** No IPv6 address (AAAA) records available for google.com

Както виждаме google.com нямат IPv6 запис. (Всъщност google има AAAA запис, но връща отговор за такъв само когато е достъпван през IPv6 мрежа: http://www.google.com/intl/en/ipv6/)

IPv6 за www.netbsd.org?

C:\>nslookup -type=aaaa www.netbsd.org
Server:  UnKnown
Address:  10.1.42.1

Non-authoritative answer:
Name:    www.netbsd.org
Address:  2001:4f8:3:7:2e0:81ff:fe52:9a6b
Какъв е TXT записът за google.com? (ползва се предимно за SPF)
C:\>nslookup -type=txt google.com
Server:  UnKnown
Address:  10.1.42.1

Non-authoritative answer:
google.com      text =

"v=spf1 include:_netblocks.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all"
TXT записът е всъщност SPF запис който показва кой сървъри могат да изпращат емейли ОТ @google.com.

Non-authoritative DNS answer:
Този тип отговори идват от DNS cache сървъри.
В случая 10.1.42.1 е DNS сървър въведен в мрежовите настройки (или /etc/resolv.conf).

* клиента пита своят DNS cache server: кои сървъри отговарят за e-mail-ите на google.com.
* DNS cache сървърът намира NS записите на google.com и пита authoritative сървъра кои сървъри отговарят за e-mail-ите на google.com.
* DNS cache сървърът отговаря на клиента.
* В случая огтоворът е Non-authoritative.

C:\>nslookup -type=mx google.com
Server:  UnKnown
Address:  10.1.42.1

Non-authoritative answer:
google.com      MX preference = 50, mail exchanger = alt4.aspmx.l.google.com
google.com      MX preference = 10, mail exchanger = aspmx.l.google.com
google.com      MX preference = 40, mail exchanger = alt3.aspmx.l.google.com
google.com      MX preference = 30, mail exchanger = alt2.aspmx.l.google.com
google.com      MX preference = 20, mail exchanger = alt1.aspmx.l.google.com
Тоест отговорът не е получен от DNS сървър отговарящ за зоната google.com.

Authoritative answer:
Отговорът идва от DNS сървър отговарящ за зоната google.com. За целта предварително намираме кои DNS сървъри отговарят за google.com.

* клиента пита директно сървър отговарящ за зоната google.com
* отговорът е Authoritative
C:\>nslookup -type=ns google.com
Server:  UnKnown
Address:  10.1.42.1

Non-authoritative answer:
google.com      nameserver = ns2.google.com
google.com      nameserver = ns1.google.com
google.com      nameserver = ns3.google.com
google.com      nameserver = ns4.google.com

C:\>nslookup -type=mx google.com ns1.google.com
Server:  ns1.google.com
Address:  216.239.32.10

google.com      MX preference = 30, mail exchanger = alt2.aspmx.l.google.com
google.com      MX preference = 50, mail exchanger = alt4.aspmx.l.google.com
google.com      MX preference = 20, mail exchanger = alt1.aspmx.l.google.com
google.com      MX preference = 10, mail exchanger = aspmx.l.google.com
google.com      MX preference = 40, mail exchanger = alt3.aspmx.l.google.com
alt2.aspmx.l.google.com internet address = 74.125.127.27
alt4.aspmx.l.google.com internet address = 209.85.225.27
alt1.aspmx.l.google.com internet address = 209.85.173.27
aspmx.l.google.com      internet address = 74.125.79.27
alt3.aspmx.l.google.com internet address = 74.125.81.27

DNS client service:
Windows предлага dns client service който кешира локално dns заявките. Не е много ясно точно как работи но някои неща все пак можем да видим и сами.

C:\>ipconfig/displaydns

Windows IP Configuration

         1.0.0.127.in-addr.arpa
         ----------------------------------------
         Record Name . . . . . : 1.0.0.127.in-addr.arpa.
         Record Type . . . . . : 12
         Time To Live  . . . . : 424617
         Data Length . . . . . : 4
         Section . . . . . . . : Answer
         PTR Record  . . . . . : localhost

         vbox7.com
         ----------------------------------------
         Record Name . . . . . : vbox7.com
         Record Type . . . . . : 1
         Time To Live  . . . . : 24180
         Data Length . . . . . : 4
         Section . . . . . . . : Answer
         A (Host) Record . . . : 87.121.59.2

         www.samedeutz-fahr.com
         ----------------------------------------
         Record Name . . . . . : www.samedeutz-fahr.com
         Record Type . . . . . : 1
         Time To Live  . . . . : 86375
         Data Length . . . . . : 4
         Section . . . . . . . : Answer
         A (Host) Record . . . : 84.33.3.3


         en.wikipedia.org
         ----------------------------------------
         Record Name . . . . . : en.wikipedia.org
         Record Type . . . . . : 5
         Time To Live  . . . . : 233
         Data Length . . . . . : 4
         Section . . . . . . . : Answer
         CNAME Record  . . . . : wikipedia-lb.wikimedia.org
         .......
         .......
Това са кешираните заявки от DNS Client service.

ipconfig/flushdns

Това изтрива локалният кеш на dns client service. Понякога този service прави проблеми. Ако правите някакви промени по DNS записи, най-добре е този service да бъде спрян докато правите тестовете.

При спян DNS Client service не се кешира нищо на локалния компютър а за всяко DNS запитване се пита dns cache сървъра. При пуснат service, веднъж получен отговор от DNS cache сървъра, записът се кешира локално както се вижда и по-горе (Time To Live  . . . . : 233 примерно).

Парадоксът: Нямам интернет, но имам Skype!

Когато това нещо се получи вероятността проблемът да е в DNS сървърите които ползвате е около 99%. Как можем да го тестваме? Аз използвам публчните DNS cache сървъри на google който са достъпни за всички и най-вече се помнят много лесно. Ето ги:

google public dns cache 1: 8.8.8.8
google public dns cache 2: 8.8.4.4

Ако имате Skype, но "нямате интернет" винаги може да въведете един от тези DNS сървъри и да пробвате дали вече "имате интернет".